基于时间的启用/禁用 WooCommerce 添加到购物车功能 [英] Time based Enable/Disable WooCommerce add to cart functionality

查看:34
本文介绍了基于时间的启用/禁用 WooCommerce 添加到购物车功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来做到这一点:

  • 从周一到周日下午 5 点到下午 2 点启用添加到购物车,在此时间之前或之后,需要禁用添加到购物车按钮.

  • 在周二全天,商店关门,因此添加到购物车被禁用.

我发现此代码段可禁用添加到购物车但没有指定时间和日期:

/*** @snippet WooCommerce 假期/暂停模式* @how-to 观看教程 @ https://businessbloomer.com/?p=19055* @sourcecode https://businessbloomer.com/?p=20862* @author Rodolfo Melogli* @testedwith WooCommerce 2.6.4*///触发假期模式add_action ('init', 'bbloomer_woocommerce_holiday_mode');//禁用购物车、结帐、添加购物车函数 bbloomer_woocommerce_holiday_mode() {remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);remove_action('woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20);remove_action('woocommerce_checkout_order_review', 'woocommerce_checkout_payment', 20);add_action( 'woocommerce_before_main_content', 'bbloomer_wc_shop_disabled', 5 );add_action('woocommerce_before_cart', 'bbloomer_wc_shop_disabled', 5);add_action( 'woocommerce_before_checkout_form', 'bbloomer_wc_shop_disabled', 5 );}//显示假期通知函数 bbloomer_wc_shop_disabled() {wc_print_notice( '我们的网上商店今天关门了:)', '错误');}

我的英语不好,但希望你能帮帮我.

解决方案

这里有一些东西可以帮助您入门,这段代码将检查今天是否是星期二,如果是,则添加到购物车功能将被禁用.

>

function bbloomer_woocommerce_holiday_mode() {$disable_cart = my_wc_disable_add_to_cart();如果($disable_cart){remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);remove_action('woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20);remove_action('woocommerce_checkout_order_review', 'woocommerce_checkout_payment', 20);add_action( 'woocommerce_before_main_content', 'bbloomer_wc_shop_disabled', 5 );add_action('woocommerce_before_cart', 'bbloomer_wc_shop_disabled', 5);add_action('woocommerce_before_checkout_form', 'bbloomer_wc_shop_disabled', 5);}}函数 my_wc_disable_add_to_cart() {//星期二是一周的第 2 天$day_of_week = date("w", current_time());如果( $day_of_week === 2 )返回真;返回假;}

这将是一个类似的过程,添加启用/禁用购物车的其他时间条件.

I'm searching for a method to make this:

  • From Monday - Sunday from 5pm to 2pm the add to cart is enabled, before or after this time, the add to cart button needs to be disabled.

  • On Tuesday the complete day, the Shop is closed, so the add to cart is disabled.

I found this snippet to disable the add to cart but without the time and day specification:

/**
 * @snippet       WooCommerce Holiday/Pause Mode
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=20862
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 2.6.4
 */

// Trigger Holiday Mode
add_action ('init', 'bbloomer_woocommerce_holiday_mode');

// Disable Cart, Checkout, Add Cart
function bbloomer_woocommerce_holiday_mode() {
    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
    remove_action( 'woocommerce_checkout_order_review', 'woocommerce_checkout_payment', 20 );
    add_action( 'woocommerce_before_main_content', 'bbloomer_wc_shop_disabled', 5 );
    add_action( 'woocommerce_before_cart', 'bbloomer_wc_shop_disabled', 5 );
    add_action( 'woocommerce_before_checkout_form', 'bbloomer_wc_shop_disabled', 5 );
}

// Show Holiday Notice
function bbloomer_wc_shop_disabled() {
    wc_print_notice( 'Our Online Shop is Closed Today :)', 'error');
} 

My English is not good, but I hope you can help me.

解决方案

Here's something to get you started, this piece of code will check if today is Tuesday and if it is then the add to cart functionality will be disabled.

function bbloomer_woocommerce_holiday_mode() {

    $disable_cart = my_wc_disable_add_to_cart();

    if( $disable_cart ) {
        remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
        remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
        remove_action( 'woocommerce_checkout_order_review', 'woocommerce_checkout_payment', 20 );
        add_action( 'woocommerce_before_main_content', 'bbloomer_wc_shop_disabled', 5 );
        add_action( 'woocommerce_before_cart', 'bbloomer_wc_shop_disabled', 5 );
        add_action( 'woocommerce_before_checkout_form', 'bbloomer_wc_shop_disabled', 5 );
    }
}

function my_wc_disable_add_to_cart() {
    // Tuesday is day number 2 of the week
    $day_of_week = date("w", current_time());

    if( $day_of_week === 2 )
        return true;

    return false;
}

It will be a similar process adding the other time criteria for enabling/disabling the cart.

这篇关于基于时间的启用/禁用 WooCommerce 添加到购物车功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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