简体   繁体   English

日期产品自定义字段在 Woocommerce WC_Product_Query 中的使用

[英]Date product custom field usage in Woocommerce WC_Product_Query

I have a woocommerce site that I am currently adding functionality to, I have products which have expiry dates and would like to display all products which have an expiry date of today on a particular page.我有一个 woocommerce 网站,我目前正在向该网站添加功能,我的产品有到期日期,并希望在特定页面上显示所有到期日期为今天的产品。 In addition to this I would also like to have all products which have an expiry date of tomorrow displayed on another page.除此之外,我还希望将所有到期日期为明天的产品显示在另一个页面上。

I found the following documentation online: Adding Custom Parameter Support我在网上找到了以下文档: 添加自定义参数支持

I have added this to my site via the snippets plugin and modified it for my expiry date attribute我已通过片段插件将此添加到我的网站,并针对我的到期日期属性对其进行了修改

/** * Handle a custom 'expirydate' query var to get products with the 'expirydate' meta.
 * @param array $query - Args for WP_Query.
 * @param array $query_vars - Query vars from WC_Product_Query.
 * @return array modified $query
 */

function handle_custom_query_var( $query, $query_vars ) {
if ( ! empty( $query_vars['expirydate'] ) ) {
    $query['meta_query'][] = array(
        'key' => 'expirydate',
        'value' => esc_attr( $query_vars['expirydate'] ),
    );
}

return $query;
}
add_filter( 'woocommerce_product_data_store_cpt_get_products_query', 'handle_custom_query_var', 10, 2 );

I then have another snippet added which is to create the function to call for the products which have the expiry date of today and to this I've added a shortcode to easily call this on my site on the pages I want it to be displayed.然后,我添加了另一个片段,即创建 function 来调用具有今天到期日期的产品,为此我添加了一个简码,以便在我希望它显示的页面上轻松地在我的网站上调用它。

// Display products with expiry date today

function expiry_Date_Today()
{
$todaysdate = date(d/m/Y);

$products = wc_get_products( array( 'expirydate' => 'todaysdate' ) );
}
add_shortcode( 'expirydatetoday', 'expiry_Date_Today');

Now my issue here is that when I use the shortcode on my page I get nothing in return.现在我的问题是,当我在页面上使用简码时,我什么也得不到。 Just a blank empty space.只是一片空白的空间。 I assume this is because I need to request the output in a format that would show the products.我认为这是因为我需要以显示产品的格式请求 output。 I would preferably want this to appear as an image, title and the add to cart button similar to what you you would generally see when viewing the contents of a category page.我希望它显示为图像、标题和添加到购物车按钮,类似于您在查看类别页面的内容时通常会看到的内容。

I have searched online but cannot find this information so could someone point me in the right direction here or if there appears to be something obviously wrong with my code could you assist?我在网上搜索过,但找不到这些信息,所以有人可以在这里指出我正确的方向,或者如果我的代码似乎有明显问题,你能帮忙吗?

Many thanks, TMS.非常感谢,TMS。

Update更新

There are multiple mistakes in your code and your expired date should be always set in 'Ym-d' compatible date format instead, to be able to compare dates if required.您的代码中有多个错误,您的过期日期应始终设置为'Ym-d'兼容的日期格式,以便能够在需要时比较日期。

Try the following, that will get expired products list from today date:尝试以下操作,将从今天开始获得过期产品列表:

add_filter( 'woocommerce_product_data_store_cpt_get_products_query', 'handle_custom_query_var', 10, 2 );
function handle_custom_query_var( $query, $query_vars ) {
    if ( ! empty( $query_vars['expirydate'] ) ) {
        $query['meta_query'][] = array(
            'key'       => 'expirydate',
            'value'     => esc_attr( $query_vars['expirydate'] ),
            'compare'   => '=',
            'type'    => 'DATE'
        );
    }
    return $query;
}

function get_products_expired_today($atts ) {
    // Extract shortcode attributes
    extract( shortcode_atts( array(
        'date' => date('Y-m-d'), // default date is current date in 'd/m/Y' format
    ), $atts, 'expirydatetoday' ) );

    $products = wc_get_products(  array(
        'status'      => 'publish',
        'limit'       => -1,
        'expirydate'  => $date,
    ) );

    $html = '<ul class="expired-products">';

    // Products loop
    foreach( $products as $product ) {
        $html .= '<li><a href="' . $product->get_permalink() . '">' . $product->get_name() . '</a></li>';
    }

    return $html . '</ul>';
}
add_shortcode( 'expirydatetoday', 'get_products_expired_today');

Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 Tested and works.测试和工作。


Now if you want to get all expired products instead of the "today" products, you will need first to change the date format on your expirydate custom field value for all products to "YYYY`MM-DD".现在,如果您想获取所有过期产品而不是“今天”产品,您需要首先将所有产品的过期日期自定义字段值的日期格式更改为“ expirydate -DD”。

You will have to change:你将不得不改变:

'compare'   => '=',

to:至:

'compare'   => '<=',

It also works.它也有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM