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

ACF - filter relationship query to display WooCommerce product available in this category

我正在尝试使用高级自定义字段在 WooCommerce 中创建自定义类别产品排序顺序。 我找到了一个 great guide 来设置它。做法是:

  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;
}

有什么建议吗?

您想按产品类别字词过滤,因此您应该使用:

[tag_id]

而是使用:

[term_id]

您按照教程进行操作,但过滤功能不起作用。您已经创建了自定义字段并插入了创建自定义产品订单的功能。如果我以正确的方式回答您的问题,那么不起作用的是获取特定类别中所有产品 ID 的数组的函数。因此,您将获得所有可用产品,而不是您选择的类别中的产品。

这应该是您问题的一部分,以便其他人可以找到解决方案:

这是您使用的教程中的代码:https://www.igoo.co.uk/2016/10/setting-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 并使用它进行 post 查询并保存税务查询的结果 ID。


这是实际答案,可能对您有帮助:

如果您想找出它不起作用的原因,您应该首先查看 $category_ id 变量,因为这个值是使其起作用的最重要因素。所以你可以在设置变量后查看变量中是否有数据 var_dump 在你的函数中:

var_dump($category_id)

变量设置是否正确,调用“my_custom_product_order”函数时是否有数据?您应该获取当前查看类别的 id。

如果你没有在里面获取id my_custom_product_order

你应该看看“get_queried_object_id()”函数。可以通过其他方式获取此值。 “get_queried_object()”函数 returns 一个 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;
}

如果你在里面获取一个id my_custom_product_order

那你应该看看“get_category_product_ids”函数。参数有用吗?在这个函数里面试试var_dump,看看有没有值交给函数。如果没有,您可以通过其他方式获取变量。这似乎是多余的并且使参数无用,但这样您可以确保您的税务查询有一个类别 ID。把这个放在你的“my_category_product_ids”的开头:

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

希望这能帮助您找出它不起作用的原因。不知道很难解决问题。如果我是对的,问题出在你的 $category_id 没有使函数工作的价值。