Woocommerce中带有产品类别的产品变体WP_Query [英] Product variation WP_Query with a product category in Woocommerce

查看:182
本文介绍了Woocommerce中带有产品类别的产品变体WP_Query的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Woocommerce,我试图为产品类别为"Apple"的产品变型发布类型创建WP_Query.

With Woocommerce, I am trying to make a WP_Query for product variations post type with a product category 'Apple'.

$args = array(
    'product_cat'    => 'Apple',
    'post_type'      => array('product', 'product_variation'),
    'post_status'    => 'publish',
    'key'            => '_visibility',
    'value'          => 'visible',
    'posts_per_page' => 100,
    'taxonomy'       => 'pa_size',
    'meta_value'     => '39',
    'meta_query'     => array(
        array(
            'key'         => '_stock',
            'value'       => 0,
            'compare'     => '>'
        )
    )
);

但是我无法在此查询中使用它.如果删除'product_cat' => 'Apple',则查询有效.为什么?

But I can't get it work in this query. If I remove 'product_cat' => 'Apple', the query works. Why?

推荐答案

WP_Query中有关Woocommerce产品的许多错误:

There is many mistakes in this WP_Query regarding Woocommerce products:

  • 对于产品类别产品属性,您最好改用tax_query.
  • 对于产品可见性,自Woocommerce 3起,它由product_visibility分类法针对'exclude-from-search''exclude-from-catalog'术语进行处理.
  • For product category and product attribute you should better use normally a tax_query instead.
  • For Product visibility, since Woocommerce 3, it's handled by product_visibility taxonomy for 'exclude-from-search' and 'exclude-from-catalog' terms.

有关产品变体的重要说明:

  • 产品类别(或产品标签) 不是由产品变体来处理,而是由父变量产品
  • 处理
  • 用于变体形式的产品属性被处理为发布元数据,其中带有meta_key并以"attribute_"开头,而met_value词条.
  • 在产品变体中不处理产品可见性,因为它们不显示在存档页面中.
  • 它们未显示在存档页面中(如前所述).
  • Product categories (or product tags) are not handled by Product variations, but by the parent Variable product
  • Product attributes for variations are handled as post meta data with a meta_key prepended by "attribute_" and a met_value that is the term slug.
  • Product visibility is not handled in product variations, as they are not displayed in archives pages.
  • They are not displayed in archive pages (as mentioned before).

因此,使用WP_Query时,您不能同时查询产品"帖子类型和"product_variation"帖子类型,因为它们确实不同.

So when using a WP_Query, you can NOT query at the same time "product" post type and "product_variation" post type as they are really different.

要使您的查询适用于"product_variation"帖子类型,您需要一个小的实用程序函数,该函数将获取产品类别(或任何自定义分类法作为Product标签…)的父变量product :

To make your query work for "product_variation" post type, you need a little utility function that will get the parent variable product for a product category (or any custom taxonomy as Product tags…):

// Utility function to get the parent variable product IDs for a any term of a taxonomy
function get_variation_parent_ids_from_term( $term, $taxonomy, $type ){
    global $wpdb;

    return $wpdb->get_col( "
        SELECT DISTINCT p.ID
        FROM {$wpdb->prefix}posts as p
        INNER JOIN {$wpdb->prefix}posts as p2 ON p2.post_parent = p.ID
        INNER JOIN {$wpdb->prefix}term_relationships as tr ON p.ID = tr.object_id
        INNER JOIN {$wpdb->prefix}term_taxonomy as tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
        INNER JOIN {$wpdb->prefix}terms as t ON tt.term_id = t.term_id
        WHERE p.post_type = 'product'
        AND p.post_status = 'publish'
        AND p2.post_status = 'publish'
        AND tt.taxonomy = '$taxonomy'
        AND t.$type = '$term'
    " );
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试和工作. 以下WP_Query的必要

Code goes in function.php file of your active child theme (or active theme). Tested and works. Necessary for the WP_Query below

此处WP_Query代码用于产品变体 (仅)与特定产品类别和特定变体属性值相关:

Here WP_Query code for Product variations (only) related to a specific product category and specific variation attribute values:

// Settings
$cat_name = 'Apple'; // Product category name
$attr_taxonomy = 'pa_size'; // Product attribute
$attribute_term_slugs = array('39'); // <== Need to be term SLUGs

$query = new WP_Query( array(
    'post_type'       => 'product_variation',
    'post_status'     => 'publish',
    'posts_per_page'  => 100,
    'post_parent__in' => get_variation_parent_ids_from_term( $cat_name, 'product_cat', 'name' ), // Variations
    'meta_query'      => array(
        'relation'    => 'AND',
        array(
            'key'     => '_stock',
            'value'   => 0,
            'compare' => '>'
        ),
        array( 
            'key'     => 'attribute_'.$attr_taxonomy, // Product variation attribute
            'value'   => $attribute_term_slugs, // Term slugs only
            'compare' => 'IN',
        ),
    ),
) );

// Display the queried products count
echo '<p>Product count: ' . $query->post_count . '<p>';

// Displaying raw output for posts
print_pr($query->posts);

经过测试,可以正常工作.

Tested and works.

这篇关于Woocommerce中带有产品类别的产品变体WP_Query的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆