it-swarm-ja.com

特定のカテゴリのすべての商品をループしないコード

Wp eコマースプラグインによって管理されている特定のカテゴリのすべての商品を取得しようとしましたが、使用するコードが返されたすべての商品をループしていません。すべてのコードは、製品を探して後でそれらをJSONを使用して外部アプリケーションに返すための一種のAPIです。

まず、これが私が使用するすべてのコードです。

$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;

     }
    }
}

Foreachとwhileループがすべての製品をループしていない理由はわかりません。返品数を確認すると約60項目ですが、最初の7〜10項目しかループしていません。私の質問は、すべての製品をループするようにコードを修正する方法です。助けてください、そして事前に感謝します。

1
Malloc

WP_Query のデフォルトの投稿数は、1ページあたり 10件です

'posts_per_page' => -1$wpec_argsを追加してみてください。

$wpec_args = array(
    'post_status' => 'publish',
    'post_type'   => 'wpsc-product',
    'posts_per_page' => -1,
    'wpsc_product_category' => $wpec_term_slug
);
1
montrealist