【问题标题】:Sell some products only on a specific day in WooCommerce仅在 WooCommerce 中的特定日期销售某些产品
【发布时间】:2019-02-25 13:12:51
【问题描述】:

通过 Woocommerce,我想仅在周日销售某些已定义的商品。如果有人试图在周日以外购买,应该会出错。商店中剩余的物品可以随时购买。

这可能吗?任何曲目都非常感谢。

示例 - 我有一个名为“Sunday Tapas”的产品,产品代码为“2795”。我希望这个产品只在周日有售。商店里的其他东西应该每天都可以正常使用。

【问题讨论】:

    标签: php wordpress date woocommerce product


    【解决方案1】:

    基于Enable Woocommerce shop purchases only during a daily time range 答案代码,改为处理“星期几” (所以“星期日” 给你)

    以下内容将允许购买特定商品(产品 ID 2795,仅限周日。

    如果商品在购物车中但无法再购买,它会自动删除(在购物车或结账时)并显示错误消息。

    周日以外,产品页面会显示错误信息,提示该产品只能在周日购买。

    你必须在第一个函数中set your time zone,在第二个函数中你的特定产品。

    代码:

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

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 谢谢。我再次修改了问题以更清楚。这是我想在一周中的哪一天限制的某些项。其余的商店物品可以在任何一天购买。
    • 我有一个名为“Sunday Tapas”的产品,产品 ID 为“2795”。我希望这个产品只在周日有售。但是商店里的其他东西应该每天都可以正常使用。
    • @Zed0121 我已经更改了您的产品 ID“2795”的代码,该代码只能在周日购买……
    • 这行得通(它基本上删除了产品页面上的添加到购物车按钮)。但是它并没有告诉用户为什么按钮不存在 - 即当有人在规定的日期之外尝试购买时,它不会“给出错误”。
    • @Zed0121 周日以外,指定的产品 ID 不可购买,如果它在购物车中,则将其删除。所以它可以做的是在周日以外的特定产品页面上显示消息(当添加到购物车按钮被隐藏时)。
    【解决方案2】:

    此代码 sn-p 将阻止周日产品添加到购物车。

    add_filter( 'woocommerce_variation_is_purchasable', 'restrict_products_on_sunday', 10, 2 );
    add_filter( 'woocommerce_is_purchasable', 'restrict_products_on_sunday', 10, 2 );
    
    function restrict_products_on_sunday(  $is_purchasable, $product ){
        $sunday_products = array( 'sunday_product', 'product2' ); //Give here SKUs of the product waht to sell on sundays only
    
        date_default_timezone_set("Asia/Bangkok"); // give here the time zone of your country
        $sku = $product->get_sku();
    
        $today = date('w');
        if( (int)$today==0 && in_array( $sku, $sunday_products ) ){
            return true;
        }
        return false;
    }
    

    由于您的要求是从结帐开始,您可以使用以下代码 sn-p。如果购物车商品包含无效产品,它将在结帐时显示一些消息。

    add_action( 'woocommerce_check_cart_items', 'prevent_chckout_for_sunday_products' );
    function prevent_chckout_for_sunday_products(){
        $sunday_products = array( 'sunday_product', 'product2' ); //Give here SKUs of the product which will sell on Sundays only
        global $woocmmerce;
    
        date_default_timezone_set("Asia/Bangkok");
        $today = date('w');
    
        $items = WC()->cart->get_cart();
        foreach( $items as $key => $item ){
            $sku = $item['data']->get_sku();
            if( (int)$today==0 && in_array( $sku, $sunday_products ) ){
                wc_add_notice( sprintf( 'Look like your cart contains product which is not eligible to checkout today, Consider removing those.' ), 'error' );
                break;
            }
        }
    }
    

    【讨论】:

    • 这有助于隐藏添加到购物车功能。但它并没有解决“它应该给出错误”的问题。因此,当用户尝试在规定日期之外购买时,它应该返回结帐错误。
    • 我将修改 sn-p 以防止继续结帐。给我一些时间。
    • @Zed0121 ,请看修改后的答案,可以继续第二个sn-p。
    猜你喜欢
    • 1970-01-01
    • 2019-08-27
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多