admin管理员组文章数量:1130349
When you run get_post_meta( get_the_ID(), '_crosssell_ids',true); you get an array of all the products that the current product cross sells to.
How do I reverse that? To get all the products that cross sells to the current item?
Example:
Not this: Product 1 -> Cross-sell: Product 2, Product 3
But this: Product 3 <- Cross-sell: Product 1
When you run get_post_meta( get_the_ID(), '_crosssell_ids',true); you get an array of all the products that the current product cross sells to.
How do I reverse that? To get all the products that cross sells to the current item?
Example:
Not this: Product 1 -> Cross-sell: Product 2, Product 3
But this: Product 3 <- Cross-sell: Product 1
Share Improve this question asked Oct 31, 2018 at 8:40 Gustav GesarGustav Gesar 11 bronze badge1 Answer
Reset to default 0You could do it with a meta query
$current_post_id = 'YOUR_POST_ID';
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_crosssell_ids',
'value' => $current_post_id,
'compare' => 'LIKE'
)
)
);
$query = new WP_Query($args);
if($query->have_posts()){
while($query->have_posts()): $query->the_post();
$product = wc_get_product(get_the_ID());
// Do what you want with the product.
endwhile;
}
This query will return all products that have the $current_post_id as a cross sell id.
Hope this helps.
When you run get_post_meta( get_the_ID(), '_crosssell_ids',true); you get an array of all the products that the current product cross sells to.
How do I reverse that? To get all the products that cross sells to the current item?
Example:
Not this: Product 1 -> Cross-sell: Product 2, Product 3
But this: Product 3 <- Cross-sell: Product 1
When you run get_post_meta( get_the_ID(), '_crosssell_ids',true); you get an array of all the products that the current product cross sells to.
How do I reverse that? To get all the products that cross sells to the current item?
Example:
Not this: Product 1 -> Cross-sell: Product 2, Product 3
But this: Product 3 <- Cross-sell: Product 1
Share Improve this question asked Oct 31, 2018 at 8:40 Gustav GesarGustav Gesar 11 bronze badge1 Answer
Reset to default 0You could do it with a meta query
$current_post_id = 'YOUR_POST_ID';
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_crosssell_ids',
'value' => $current_post_id,
'compare' => 'LIKE'
)
)
);
$query = new WP_Query($args);
if($query->have_posts()){
while($query->have_posts()): $query->the_post();
$product = wc_get_product(get_the_ID());
// Do what you want with the product.
endwhile;
}
This query will return all products that have the $current_post_id as a cross sell id.
Hope this helps.
本文标签: phpReverse CrossSells (WooCommerce)
版权声明:本文标题:php - Reverse Cross-Sells (WooCommerce) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749210914a2333344.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论