I try to get the value of WooCommerce custom field from an array of product ID. No luck, I'm on the custom page not on the woocommerce loop.
tried this: $productArray1
is a list of product ID (and it's working)
global $wpdb;
global $product;
foreach ($productArray1 as $value)
{
$querystr = "
SELECT meta_value
FROM $wpdb->postmeta.meta_key
WHERE $wpdb->postmeta.meta_key = 'product_cip'
AND $wpdb->posts.$product->ID=$value
ORDER BY meta_value DESC
";
$productsCIP = $wpdb->get_results($querystr, OBJECT);
if ( ! $productsCIP ) {
$wpdb->print_error();
}
else {
echo $productsCIP;
}
};
I can get all product with the same customfield like this :
$products = wc_get_products( array( 'product_cip' => '3337875548519' ) );
echo 'PRODUCT WITH SAME CIP (TOTAL : '.count($products).')<br>';
But I need to find 'product_cip' by product ID. Any clue?
Thanks for the help.
I try to get the value of WooCommerce custom field from an array of product ID. No luck, I'm on the custom page not on the woocommerce loop.
tried this: $productArray1
is a list of product ID (and it's working)
global $wpdb;
global $product;
foreach ($productArray1 as $value)
{
$querystr = "
SELECT meta_value
FROM $wpdb->postmeta.meta_key
WHERE $wpdb->postmeta.meta_key = 'product_cip'
AND $wpdb->posts.$product->ID=$value
ORDER BY meta_value DESC
";
$productsCIP = $wpdb->get_results($querystr, OBJECT);
if ( ! $productsCIP ) {
$wpdb->print_error();
}
else {
echo $productsCIP;
}
};
I can get all product with the same customfield like this :
$products = wc_get_products( array( 'product_cip' => '3337875548519' ) );
echo 'PRODUCT WITH SAME CIP (TOTAL : '.count($products).')<br>';
But I need to find 'product_cip' by product ID. Any clue?
Thanks for the help.
Share Improve this question edited May 21, 2019 at 14:16 Sami Ahmed Siddiqui 1293 bronze badges asked Jan 13, 2018 at 15:45 ilanbilanb 933 silver badges12 bronze badges2 Answers
Reset to default 1If you look for all products with same 'product_cip' value try this:
$a=array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => - 1, 'meta_query' => array( array( 'key' => 'product_cip', 'value' => 'some value', 'compare'=> '=' ) ), 'fields' => 'ids' ); $b=get_posts($a); echo count($b);
if you have get all products data
$products_array=array(); foreach($b as $v){ $_product=wc_get_product($v); echo $_product->get_name().','; $products_array[]=$_product; }
Try this:
$product_obj=array(); foreach ($productArray1 as $value) { $product_obj['id']=$value; $product_obj['product_cip']=get_post_meta($value,'product_cip'); }
or
foreach ($productArray1 as $value) { $product_obj['id']=$value; $product_obj['product_cip']=get_post_meta($value,'product_cip',true); } echo 'PRODUCT WITH SAME CIP (TOTAL : '.count( $product_obj).')
';
if meta value not array.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745477208a4629398.html
评论列表(0条)