仅在特定的一天在WooCommerce中出售某些产品 [英] Sell some products only on a specific day in WooCommerce

查看:30
本文介绍了仅在特定的一天在WooCommerce中出售某些产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望通过Woocommerce仅在周日出售某些定义的商品.如果有人尝试在周日之外进行购买,应该输入错误.商店中的剩余物品可以随时购买.

这可能吗?任何曲目都非常感激.

示例-我有一个名为"Sunday Tapas"的产品,产品代码为"2795".我希望该产品仅在周日提供.商店中的其他所有物品都应每天正常提供.

解决方案

基于"

With Woocommerce I would like to sell certain defined items on Sunday only. If anyone tries to purchase outside of Sunday, it should give an error. The remaining items in the shop can be purchase anytime.

Is this possible? Any track is really appreciated.

Example - I have a product called "Sunday Tapas" which is product code "2795". I want this product available on Sunday’s only. Everything else in the store should be available everyday as normal.

解决方案

Based on "Enable Woocommerce shop purchases only during a daily time range" answer code, changed to handle "days of the week" (So "Sunday" for you).

The following will enable purchases for a specific item (the product Id 2795) on sundays only.

If the item is in cart but not purchasable anymore, It's auto removed (on cart or checkout) displaying an error message.

Outside sundays, an error message is displayed on the product page, indicating that the product is only purchasable on sundays.

You will have to set your time zone in the first function and your specific products in the second function.

The code:

// Utility conditional function that check if day is sunday (returns boolean)
function is_sunday() {
    // Set Your shop time zone (http://php.net/manual/en/timezones.php)
    date_default_timezone_set('Europe/London');

    // If the current day is "sunday" return true (else retun false)
    return ( date('w') == 0 ) ? true : false;
}

// Utility function (setting your product IDS in the array)
function sunday_products() {
    // HERE your product IDs in the array (need to be coma separated)
    return array( 37 );
}

// Enable purchases for specific items on sundays only
add_filter( 'woocommerce_variation_is_purchasable', 'enable_specific_products_on_sundays', 10, 2 );
add_filter( 'woocommerce_is_purchasable', 'enable_specific_products_on_sundays', 10, 2 );
function enable_specific_products_on_sundays( $purchasable, $product ) {
    // Enable purchases for specific defined item only on sunday
    if( ! is_sunday() && in_array( $product->get_id(), sunday_products() ) )
        $purchasable = false;

    return $purchasable;
}

// Add a notice in specific products outside sundays
add_action( 'woocommerce_before_single_product', 'filter_before_single_product' );
function filter_before_single_product() {
    global $product;

    if( ! is_sunday() && in_array( $product->get_id(), sunday_products() ) ) {
        wc_print_notice( __( 'This product is only purchasable on sundays', 'woocommerce' ), 'error' );
    }
}

// IN CASE OF (but not needed): Cart and checkout validation + error message
add_action( 'woocommerce_check_cart_items', 'conditionally_allowing_checkout' );
add_action( 'woocommerce_checkout_process', 'conditionally_allowing_checkout' );
function conditionally_allowing_checkout() {
    if ( ! is_sunday() ) {
        // Loop through cart items
        foreach( WC()->cart->get_cart() as $cart_item ){
            // Check cart items
            if( in_array( $cart_item['data']->get_id(), sunday_products() ) ){
                wc_add_notice( sprintf(__("%s can be only purchase on sundays.", "woocommerce"), $cart_item['data']->get_name() ), 'error' );
                break;
            }
        }
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于仅在特定的一天在WooCommerce中出售某些产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆