WooCommerce-基于自定义现场价值(ACF)禁用或隐藏产品



我试图基于其自定义字段值(由高级自定义字段插件提供的逻辑条件 - acf (。

我已经引用了许多有关根据某些标准排除产品的文章,并使用pre_get_postswoocommerce_product_query_tax_querywoocommerce_product_query挂钩进行测试。所有这些都不能让我访问ACF值,以便我可以过滤$tax_query项。

我的最终目标是隐藏在其自定义字段中设置日期的特定产品,而该日期是过去。我使用get_field('event_date');函数访问自定义字段。

我正在寻找可以帮助我开发此代码以根据其高级自定义字段价值过滤产品的代码。

到目前为止,我已经尝试了很多尝试,我已经看了查看var_dumps,看看我可以使用什么,但事实是我仍然在Square One。这是我可笑的进度:

add_action('woocommerce_product_query', 'filter_past_events');
function filter_past_events($q)
{
    // Return is event_date variable is not set
    if (get_field('event_date')) {
        $eventDate = new DateTime(get_field('event_date')); // Format ISO-8601 (YYYY-MM-DD)
        $now = new DateTime();
        if ($eventDate < $now) {
            // event is in the past
        }
    }
}

到目前为止,我已经引用了:

•从商店页面上的特定类别中排除产品

•基于滤波器 - 基于custom-ustom-product-attribute-value

function custom_pre_get_posts_query($q) {
    $meta_query   = $q->get('meta_query');
    $meta_query[] = array(
        'key'     => 'key',
        'value'   => 'value',
        'compare' => '!='
    );
    $q->set('meta_query', $meta_query);
}
add_action('woocommerce_product_query', 'custom_pre_get_posts_query');

最新更新