ACF - 过滤关系查询,以显示该类别中可用的WooCommerce产品。

问题描述 投票:0回答:1

我试图在WooCommerce中使用高级自定义字段创建一个自定义类别产品排序顺序。大导 来设置。方法是。

  1. 将ACF关系字段添加到分类页中。
  2. 配置成只显示产品。
  3. 添加一个函数,根据你添加的产品对分类页进行排序。

很好用!我的问题是,它总是显示所有可用的产品,而不是只显示这个类别中可用的产品。

我试图使用这个函数,但tag_id不起作用。

add_filter('acf/fields/relationship/query', 'my_acf_fields_relationship_query', 10, 3);
function my_acf_fields_relationship_query( $args, $field, $post_id ) {

$args['tag_id'] = $category_id;

return $args;
}

有什么建议吗?

wordpress woocommerce advanced-custom-fields
1个回答
0
投票

你想通过产品类别术语进行过滤,所以你应 用。

[tag_id]

但是要用:

[term_id]

你按照教程做了,但过滤功能没有用。你已经创建了自定义字段,并插入了创建自定义产品订单的函数。如果我对你的问题理解正确的话,不工作的是获取特定类别内所有产品ID数组的函数。所以您得到的是所有可用的产品,而不是您选择的类别的产品。

这应该是你问题的一部分,所以其他人可以找到解决方案。

这是你使用的教程中的代码。https:/www.igoo.co.uk201610setting-a-custom-category-specific-product-sort-order-in-woocommerce-using-advanced-custom-fields

function my_custom_product_order($q) {
  if(!is_admin())
  {
    // fetch current category id from active query
    $category_id = $q->get_queried_object_id();

    // get array of all product IDs in current category
    $product_ids = get_category_product_ids($category_id);

    // get preferred order from ACF field
    $product_ids_order_preferred = get_field('product_order', 'product_cat_' . $category_id);

    // if we have some product sort order set…
    if($product_ids_order_preferred)
    {
        // merge our preferred category ids array with the array of all products ids, and remove duplicates
        $product_ids = array_unique(array_merge($product_ids_order_preferred, $product_ids));
    }

    // set the 'posts__in' argument to the new array of post IDs (unfortunately wordpress doesn’t let you just pass an array of IDs straight in here)
    $q->set('post__in', $product_ids);

    // set the query orderby value to observe the posts__in field
    $q->set('orderby', 'post__in');
  }

  remove_action('woocommerce_product_query', 'custom_pre_get_posts_query');
}
add_action('woocommerce_product_query', ‘my_custom_product_order’);

在上面代码的第7行,你使用了 "get_category_product_ids($category_id) "函数,并给出$category_id作为参数。

你的教程中的 "get_category_product_ids "函数是这样的。

// helper function to fetch all product IDs from a specific category ID

function get_category_product_ids($category_id) {
    $args = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'fields' => 'ids',
        'posts_per_page' => -1,
        'tax_query' => array(
            array(
                'taxonomy' => 'product_cat',
                'field' => 'term_id',
                'terms' => $category_id,
                'operator' => 'IN'
            )
        )
    );

    $ids = get_posts($args);

    return $ids;
}

这个函数得到参数$category_id 然后用它来做一个职位查询 并把结果保存在税费查询的id中。


这就是实际的答案,也许能帮到你。

如果你想知道为什么它不能工作, 你应该先看看$category_id变量, 因为这个值是使它工作的最重要的。所以你可以在设置变量后,用var_dump在你的函数里面看一下变量里面是否有数据。

var_dump($category_id)

变量的设置是否正确,当 "my_custom_product_order "函数被调用时是否有数据?你应该得到当前浏览的类别的id。

如果你在函数my_custom_product_order中没有得到一个id,那么你就会得到当前浏览的分类的id。

你应该看看 "get_queried_object_id() "函数。也可以用另一种方法来获得这个值。get_queried_object() "函数返回一个WP_Term对象。所以你也可以这样做。

$category_id = $q->get_queried_object();
$category_id = $category_id->term_id;

也许你现在已经有了你需要的id。

如果没有,你应该检查一下参数$q,它是干什么的?也许这可能会造成问题。你可以试试这样的方法。

if (is_product_category()) {
    $q = get_queried_object();
    $term_id = $q->term_id;
}

如果你在my_custom_product_order中得到一个id的话

那么你应该看看 "get_category_product_ids "函数。参数能用吗?在这个函数里面试试var_dump,看看是否有值交给函数。如果没有,你可以换一种方式获取变量。这似乎是多余的,使得参数没有用处,但这种方式可以确保你的税务查询有一个类别ID。把这个放在你的 "my_category_product_ids "的开头。

$category_id = get_queried_object();
$category_id = $category_id->term_id;

希望这能帮你找到为什么不能用的原因。如果不知道,就很难解决这个问题。如果我没说错的话,问题出在你的$category_id没有一个让函数工作的值。

© www.soinside.com 2019 - 2024. All rights reserved.