admin管理员组文章数量:1426790
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.
本文标签: custom fieldHow to get customfield value by woocommerce product ID
版权声明:本文标题:custom field - How to get customfield value by woocommerce product ID 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745477208a2660014.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论