Support » Plugin: Contextual Related Posts » show products in stock only

  • Resolved multisite

    (@multisite)


    hello, any way to not show related products that are out of stock on woocommerce?

    i think a simple code can solve this.

    thank you on advance for the good work and great plugin.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Ajay

    (@ajay)

    I’m not as close to woocommerce. How do you determine which related product is out of stock. I mean where is this stored exactly?

    The code would require a bit of work.

    Thread Starter multisite

    (@multisite)

    function yarpp_custom_wp_query($query) {
        if(isset($query->yarpp_cache_type)){
            $query->set('post_type', 'product');
            $meta_query = [
                'relation' => 'AND',
                [
                    'key' => '_stock_status',
                    'value' => ['instock','onbackorder'],
                    'compare' => 'IN',
                ]
            ];
            $query->set('meta_query', $meta_query);
        }
    
        return $query;
    }
    add_filter('pre_get_posts', 'yarpp_custom_wp_query', 100);
    Thread Starter multisite

    (@multisite)

    I determine if it’s in stock or not when I create or edit a product, it’s the same as a post but it’s a woocommerce product.

    maybe this code above, from another related plugin can help

    Plugin Author Ajay

    (@ajay)

    Can you please try this and let me know if this works?

    function crp_custom_wp_query($query) {
        if(is_singular('product') && $query->get( 'crp_query'){
            $query->set('post_type', 'product');
            $meta_query = [
                'relation' => 'AND',
                [
                    'key' => '_stock_status',
                    'value' => ['instock','onbackorder'],
                    'compare' => 'IN',
                ]
            ];
            $query->set('meta_query', $meta_query);
        }
    
        return $query;
    }
    add_filter('pre_get_posts', 'crp_custom_wp_query', 100);

    This will check if this is a product view and if that is the case it should limit the products

    Thread Starter multisite

    (@multisite)

    hello, yes, it totally worked, I just had to add a “)” in the second line of code, it was like this:

    function crp_custom_wp_query($query) {
        if(is_singular('product') && $query->get( 'crp_query')){
            $query->set('post_type', 'product');
            $meta_query = [
                'relation' => 'AND',
                [
                    'key' => '_stock_status',
                    'value' => ['instock','onbackorder'],
                    'compare' => 'IN',
                ]
            ];
            $query->set('meta_query', $meta_query);
        }
    
        return $query;
    }
    add_filter('pre_get_posts', 'crp_custom_wp_query', 100);

    Thank you very much 😀

    Plugin Author Ajay

    (@ajay)

    You’re welcome! Sorry I missed that bracket. Always hard to see it here in the forum.

    Do consider a review of the plugin.

    https://wordpress.org/support/plugin/contextual-related-posts/reviews/

    Thread Starter multisite

    (@multisite)

    yes, done 😉

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.