Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I try to fetch all products for a given category managed by the wp e-commerce plugin, but the code I use is not looping all the products returned. All the code is a sort of API to look for products and return them later to an extern application with JSON.

First , this is all the code I use:

$catid = $_REQUEST['catid'];//get the category id from the url
$term1 = get_term($catid,'wpsc_product_category');
getdetails($term1->slug,$term1->term_id,$host,$term1->parent);
echo '----------------------------'.'<br />';
echo $term1->slug .'<br />';
echo $term1->term_id.'<br />';
echo $term1->parent.'<br />';
echo '----------------------------'.'<br />';
function getdetails($id,$tid,$host,$cid)
{
     $wpec_product_categories = get_terms( 'wpsc_product_category', 'hide_empty=0&slug='.$id);
    echo '-------this is a stack of the product category array--------<br />';
    var_dump($wpec_product_categories).'<br />';
    echo '-------------------------------------------<br />';

    if($wpec_product_categories[0]->count != 0)
    {

    echo 'count is '.$wpec_product_categories[0]->count.'<br />';//here the correct number of products. So, so far all is going good.
     foreach($wpec_product_categories as $wpec_categories){
           $wpec_term_id = $wpec_categories->term_id;
            $wpec_term_name = $wpec_categories->name;
            $wpec_term_slug = $wpec_categories->slug;
            $wpec_term_parent = $wpec_categories->parent;
            echo '<br />--------------------<br />';
            echo  $wpec_term_name.'<br />';
            echo  $wpec_term_slug.'<br />';
            echo  $wpec_term_parent.'<br />';
            echo '<br />--------------------<br />';


                                    $wpec_args = array(
                                   'post_status' => 'publish',
                                   'post_type'   => 'wpsc-product',
                                   'wpsc_product_category' => $wpec_term_slug
                                  );
                                  $wpec_categoryProducts = new WP_Query($wpec_args);

        while ($wpec_categoryProducts->have_posts()) : $wpec_categoryProducts->the_post();
             echo '<br />---------------------||||||--------------------<br />';
             echo 'Content:'.$wpec_categoryProducts->the_post().'<br />';   
             echo '<br />---------------------||||||--------------------<br />';

             echo 'Product wholesale price: '.wpsc_the_product_price().'<br />';

           $selthumb ="SELECT p.*, pm.* FROM wp_posts as p, wp_postmeta as pm WHERE  pm.meta_key = '_thumbnail_id' AND pm.meta_value = p.ID AND p.post_parent = '".wpsc_the_product_id()."' AND p.post_type = 'attachment'";
         $resthumb = mysql_query($selthumb);
         $rowthumb = mysql_fetch_array($resthumb);
          $img = stripslashes($rowthumb['guid']) ;
              if($img != '')
              {
                  $path =  $img;
              }
              else
              {
                  $selimg = "SELECT * FROM wp_wpsc_meta WHERE meta_key = 'image' AND object_id = ".$tid;
                  $resimg = mysql_query($selimg);
                  $rowimg = mysql_fetch_array($resimg);
                  if($rowimg != '')
                  {
                      $path = "http://".stripcslashes($host."/wp-content/uploads/wpsc/category_images/".$rowimg['meta_value']);

                  }
                  else
                  {
                       $selimg = "SELECT * FROM wp_wpsc_meta WHERE meta_key = 'image' AND object_id = ".$cid;
                       $resimg = mysql_query($selimg);
                        $rowimg = mysql_fetch_array($resimg);
                        if($rowimg != '')
                        {
                            $path = "http://".stripcslashes($host."/wp-content/uploads/wpsc/category_images/".$rowimg['meta_value']);
                        }
                        else
                        {
                             $selpimg = "SELECT tx.*,m.* FROM wp_term_taxonomy tx,wp_wpsc_meta m WHERE tx.term_id = '".$cid."' AND m.meta_key = 'image' AND m.object_id = tx.parent";
                            $respimg = mysql_query($selpimg);
                            $rowpimg = mysql_fetch_array($respimg);
                            if($rowpimg != '')
                            {
                                $path = "http://".stripcslashes($host."/wp-content/uploads/wpsc/category_images/".$rowpimg['meta_value']);
                            }
                            else
                            {
                                $path= "false";
                            }
                        }
                  }
              }

              $selcolor = "SELECT * FROM wp_postmeta WHERE post_id = '".wpsc_the_product_id()."' AND meta_key = '_wpsc_color'";
              $rescolor = mysql_query($selcolor);
              $rowcolor = mysql_fetch_array($rescolor);

              $selsize = "SELECT * FROM wp_postmeta WHERE post_id = '".wpsc_the_product_id()."' AND meta_key = '_wpsc_size'";
              $ressize = mysql_query($selsize);
              $rowsize = mysql_fetch_array($ressize);

              $selprice = "SELECT * FROM wp_postmeta WHERE post_id = '".wpsc_the_product_id()."' AND meta_key = '_wpsc_special_price'";
              $resprice = mysql_query($selprice);
              $rowprice = mysql_fetch_array($resprice);

              $selmanu = "SELECT * FROM wp_postmeta WHERE post_id = '".wpsc_the_product_id()."' AND meta_key = '_wpsc_manufacturer'";
              $resmanu = mysql_query($selmanu);
              $rowmenu = mysql_fetch_array($resmanu);

              $product_data = get_post_custom( wpsc_the_product_id() );
              $product_data['meta'] = maybe_unserialize( $product_data );

              $image = stripslashes(sb_get_images_for_product( wpsc_the_product_id() ));

              $list = explode(",",$image);


             $arr[] = array("Product ID" => wpsc_the_product_id(),"Product Title" => wpsc_the_product_title(), "Product Description" => wpsc_the_product_description(), "Product Wholesale Price" => wpsc_the_product_price(), "Product Sale Price" => '$'.$rowprice['meta_value'], "Category Id" => $wpec_term_id,"Category Name" => $wpec_term_name,"Product Image" => $path, "Product Sub Images" => $list , "SKU" => wpsc_product_sku(), "Quantity" => $product_data['meta']['_wpsc_stock'][0], "Size" => $rowsize['meta_value'], "Color" => $rowcolor['meta_value'], "Manufacturer" => $rowmenu['meta_value'] );
    $i++;           
    endwhile;

     }
    }
}

I don't understand why foreach and while loop are not looping all the products. They are looping only the first 7-10 items, although when I check the returned number of products, it's around 60 items. My questions is, how to fix my code to loop over all the products? please help, and thanx in advance.

share|improve this question
2  
The default is 10 posts per page. Try adding 'posts_per_page' => -1 to your $wpec_args. –  dalbaeb Apr 6 '13 at 1:28
    
Thanx, please make it as answer so I can upvote it :) –  Malloc Apr 6 '13 at 17:11
    
C'est fait, merci! –  dalbaeb Apr 7 '13 at 0:39

1 Answer 1

up vote 1 down vote accepted

The default number of posts for WP_Query is ten posts per page.

Try adding 'posts_per_page' => -1 to your $wpec_args:

$wpec_args = array(
    'post_status' => 'publish',
    'post_type'   => 'wpsc-product',
    'posts_per_page' => -1,
    'wpsc_product_category' => $wpec_term_slug
);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.