gpt4 book ai didi

php - 带有 Woocommerce 中产品类别的产品变体 WP_Query

转载 作者:行者123 更新时间:2023-12-04 18:30:15 24 4
gpt4 key购买 nike

使用 Woocommerce,我正在尝试为产品类别为“Apple”的产品变体帖子类型制作 WP_Query。

$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' ,查询有效。为什么?

最佳答案

这个WP_Query有很多错误关于 Woocommerce 产品:

  • 对于 产品类别 产品属性你最好正常使用 tax_query反而。
  • 对于 产品知名度 , 自 Woocommerce 3 起,由 product_visibility 处理'exclude-from-search' 的分类法和 'exclude-from-catalog'条款。

  • 关于产品变体的重要说明:
  • 产品类别(或产品标签)是 未处理 按产品变体,但按父变量产品
  • 变体的产品属性被处理为 发布元数据 meta_key以“attribute_”和met_value开头那就是 术语 slug .
  • 产品变体中不处理产品可见性 ,因为它们不会显示在文件页面中。
  • 它们不会显示在存档页面中(如前所述)。

  • 所以当使用 WP_Query ,你 无法查询 同时“product”帖子类型和“product_variation”帖子类型 因为它们真的很不一样 .

    为了使您的查询适用于“product_variation”帖子类型,您需要一个小实用函数来获取产品类别的父变量产品(或任何自定义分类法作为产品标签......):
    // 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下面

    这里 WP_Query代码 用于产品变体 (仅)与特定产品类别和特定变体属性值相关:
    // 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);

    测试和工作。

    关于php - 带有 Woocommerce 中产品类别的产品变体 WP_Query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49342704/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com