最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

woocommerce offtopic - WordPress query through Products variation stock

programmeradmin2浏览0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

I have WooCommerce variable products and a filter which is filtering products based on variation attributes. I have variation attribute depth which has numeric value (from 1 to 20) and the filter is working fine. But I want to display In Stock variations only On the other hand it is displaying all products including out of stock selected depth value (variation). So I want to hide product which will have selected depth value but no stock.

Can I query through Products variation stock ?

Here how it is working right now.

$args = array(
'post_type' => array('product'),
        'meta_query' => array(
            array(
                'key' => '_stock_status',
                'value' => 'instock',
                'compare' => '=',
            ),
        ),

        'tax_query' => array(
            array(
                'taxonomy' => 'pa_depth',
                'field' => 'slug',
                'terms' => $wccaf_depth,
                'operator'  => $wccaf_depth ? 'IN' : 'NOT IN'
            )
        ),                  
);
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

I have WooCommerce variable products and a filter which is filtering products based on variation attributes. I have variation attribute depth which has numeric value (from 1 to 20) and the filter is working fine. But I want to display In Stock variations only On the other hand it is displaying all products including out of stock selected depth value (variation). So I want to hide product which will have selected depth value but no stock.

Can I query through Products variation stock ?

Here how it is working right now.

$args = array(
'post_type' => array('product'),
        'meta_query' => array(
            array(
                'key' => '_stock_status',
                'value' => 'instock',
                'compare' => '=',
            ),
        ),

        'tax_query' => array(
            array(
                'taxonomy' => 'pa_depth',
                'field' => 'slug',
                'terms' => $wccaf_depth,
                'operator'  => $wccaf_depth ? 'IN' : 'NOT IN'
            )
        ),                  
);
Share Improve this question edited Jun 12, 2019 at 3:13 wplearner asked Jun 12, 2019 at 3:05 wplearnerwplearner 4892 gold badges9 silver badges27 bronze badges 5
  • But that $wccaf_depth ? 'IN' : 'NOT IN' is confusing.. – Sally CJ Commented Jun 12, 2019 at 4:16
  • As I mention that I have filter on this page and $wccaf_depth is a selected depth value by the user. I have few other option in my filter for example price and these are not required so that is why I added this condition $wccaf_depth ? 'IN' : 'NOT IN'. But I can also tell you that the above code is not working for variation stock and this is what I wanted to know. As it displays products with 0 stock as well which I do not want to. – wplearner Commented Jun 12, 2019 at 7:30
  • This 0 stock is of variation level as I am managing stock at variation level and want to query through variations stock. – wplearner Commented Jun 12, 2019 at 8:00
  • Ok, please ignore my previous comments and check my answer. And do let me know how it goes.. – Sally CJ Commented Jun 12, 2019 at 10:25
  • I am not sure if you have tried this or not but changing your meta query to this should help: 'meta_query' => array( array( 'key' => '_stock', 'value' => '0', 'compare' => '>', ), ), – Faham Shaikh Commented Jun 13, 2019 at 14:09
Add a comment  | 

1 Answer 1

Reset to default 2

(Note to other readers: The question author and I have already discussed via chat, so I'm going straight to the code.)

I think for performance reason, you should do it this way:

// FIRST PART: Get the variations which are in stock.

$paged = max( 1, get_query_var( 'paged' ) );
$wccaf_depth = array( '19', '1' ); // example
$args = array(
    'post_type'     => 'product_variation',
    'meta_query'    => array(
        'relation' => 'AND',
        array(
            'key'     => '_stock_status',
            'value'   => 'instock',
            'compare' => '=',
        ),
        array(
            'key'     => 'attribute_pa_tread-depth',
            'value'   => $wccaf_depth,
            'compare' => 'IN',
        ),
    ),
    'fields'         => 'id=>parent',
    'paged'          => $paged,
    'posts_per_page' => 1, // example
    'groupby'        => 'post_parent', // *this is a custom query arg
);

$q = new WP_Query( $args );
$parent_ids = wp_list_pluck( $q->posts, 'post_parent' );

// SECOND PART: Get the parent products. Here you don't need the above
// meta_query, but you can of course make other meta queries.

$args = array(
    'post_type' => 'product',
    'post__in'  => $parent_ids,
);

$loop = new WP_Query( $args );
// Run your loop here.

// Paginate the first part.
echo paginate_links( array(
    'total'   => $q->max_num_pages,
    'current' => $paged,
    //...
) );

And be sure to add this filter to your functions file:

add_filter( 'posts_groupby', 'custom_posts_groupby', 10, 2 );
function custom_posts_groupby( $groupby, $query ) {
    if ( 'post_parent' === $query->get( 'groupby' ) ) {
        global $wpdb;
        return "$wpdb->posts.post_parent";
    }
    return $groupby;
}

However, if this approach (where we paginate the first part) doesn't work for you, let me know. But you can actually always view the answer's revisions..

发布评论

评论列表(0)

  1. 暂无评论