繁体   English   中英

WooCommerce | 使类别的产品仅在一周的某一天可用

[英]WooCommerce | Make products of category only available on one day of the week

我是 PHP 和 WooCommerce 的绝对初学者。我们的商店向企业销售产品,但每个类别只在一周的一天销售。 所以我有“星期一”、“星期二”等类别。 问题是:如果有人将星期一的产品放入他们的购物车,他可以在星期二购买。
我想要做的是在给定的日期仅将可用性更改为“可用”,并在第二天将其更改回“不可用”。

我也不知道如何获得一个类别的所有产品。 这是我为星期一类别尝试的内容。 请不要杀我……

非常感谢任何帮助! <3

function weekday_products() {
    $product_args = array(
        'post_status' => 'publish',
        'limit' => -1,
        'category' => 'Monday',
        //more options according to wc_get_products() docs
    );
    $products_monday = wc_get_products($product_args);

    if(date('D', $timestamp) === 'Mon') {
        foreach ($products_monday as $product) {
            if ( !$product->is_in_stock() ) {
                wc_update_product_stock_status( $product, 'instock' );
            }
        }
    }
    else {
        foreach ($products_monday as $product) {
            if ( $product->is_in_stock() ) {
                wc_update_product_stock_status( $product, 'outofstock' );
            }
        }
    }
}
add_action( 'only_on_weekday_products', 'weekday_products' );

目前我无法尝试代码,因为商店正在生产中。 但我很确定,无论如何它都行不通......

编辑:另一种解决方案是在午夜清理购物车,比如:


add_action( 'woocommerce_add_cart_item_data', 'woocommerce_clear_cart_url' );

function woocommerce_clear_cart_url() {
    $now = strtotime("now");
    $midnight = strtotime("00:00:00");
    if ( $now = $midnight ) {
        // Empty cart
        WC()->cart->empty_cart(true);
        WC()->session->set('cart', array());
    }

} 

这段代码有效吗?

这是我将如何处理这个问题。 将以下功能放在您的活动主题功能中。php

如果您想出于 SEO 目的或其他原因继续显示所有产品,请跳过 hide_products_per_day function。只检查您的购物车中添加了什么。

//Show products only from specific category
function hide_products_per_day( $q ) {
    $day = date('l');
    $tax_query = $q->get( 'tax_query' );
    $tax_query[] = array(
        'taxonomy' => 'product_cat',
        'field'    => 'slug', // Query by term slug
        'terms'    => array( $day ), // In our case its current day
        'operator' => 'IN',
    );
    $q->set( 'tax_query', $tax_query );

}
add_action( 'woocommerce_product_query', 'hide_products_per_day' );

function check_products_in_cart() {
    if ( WC()->cart->is_empty() ) return; // Skip if cart is empty
    $day = date('l');
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        if ( !has_term( $day, 'product_cat', $cart_item['product_id'] ) ) {
            WC()->cart->remove_cart_item( $cart_item_key );
            wc_print_notice( $cart_item['data']->get_name().' cant be purchased today!', 'error' ); // Change type of notice if you want either error, success or notice (or custome).
        }
    }
}
add_action( 'woocommerce_before_checkout', 'check_products_in_cart' ); // In case we go straight to checkout
add_action( 'woocommerce_before_cart', 'check_products_in_cart' ); // We want to check what is in cart

暂无
暂无

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

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