我尝试获取由wp电子商务插件管理的给定类别的所有产品,但我使用的代码并没有循环返回的所有产品。所有代码都是一种API,用于查找产品,并在稍后使用JSON将其返回给外部应用程序。
首先,这是我使用的所有代码:
$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循环没有循环所有的产品。他们只循环了前7-10个项目,尽管当我检查退回的产品数量时,大约是60个项目。我的问题是,如何修复代码以循环所有产品?请帮忙,提前谢谢。