$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Reverse Cross-Sells (WooCommerce)|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Reverse Cross-Sells (WooCommerce)

matteradmin10PV0评论

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 badge
Add a comment  | 

1 Answer 1

Reset to default 0

You 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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far