dongmi3203 2019-02-03 17:06
浏览 75
已采纳

仅在每日时间范围内启用Woocommerce商店购物

I am trying to create a shut down between two times, but it's not working. Nothing is happening.

Here's the code I'm using:

add_action( 'woocommerce_before_single_product_summary', 'shop_closed' );
add_action( 'woocommerce_before_shop_loop', 'shop_closed' );
function shop_closed() {
global $woocommerce;
date_default_timezone_set('Europe/London');
$start_time    = mktime('16', '59', '59', date('m'), date('d'), date('Y'));
$end_time      = mktime('08', '59', '59', date('m'), date('d'), date('Y'));
$now_time      = time();

if ( $now_time > $start_time && $now_time < $end_time ) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart');
remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_login_form');
remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form');
remove_action( 'woocommerce_checkout_order_review', 'woocommerce_order_review');
remove_action( 'woocommerce_checkout_order_review', 'woocommerce_checkout_payment');
$closed = esc_html__(' We are currently closed. Welcome back tomorrow morning at 09.00 AM. ', 'woocommerce' );
echo '<span class="shop-closed">' . $closed . '</span>'; } }

Any ideas on where I'm going wrong with this?

  • 写回答

1条回答 默认 最新

  • donqo88682 2019-02-03 22:06
    关注

    Try the following that should work in a much more better way:

    // Utility conditional funtion for store open hours (returns boolean true when store is open)
    function is_store_open() {
        // Set Your shop time zone (http://php.net/manual/en/timezones.php)
        date_default_timezone_set('Europe/London');
    
        // Below your shop time and dates settings
        $open_time = mktime('09', '00', '00', date('m'), date('d'), date('Y')); // 09:00:00
        $end_time  = mktime('17', '00', '00', date('m'), date('d'), date('Y')); // 17:00:00
        $now       = time(); // Current timestamp in seconds
    
        return ( $now >= $start_time && $now <= $end_time ) ? true : false;
    }
    
    // Disable purchases on closing shop time
    add_filter( 'woocommerce_variation_is_purchasable', 'disable_purchases_on_shop', 10, 2 );
    add_filter( 'woocommerce_is_purchasable', 'disable_purchases_on_shop', 10, 2 );
    function disable_purchases_on_shop( $purchasable, $product ) {
        // Disable purchases on closing shop time
        if( ! is_store_open() )
            $purchasable = false;
    
        return $purchasable;
    }
    
    // Cart and checkout validation
    add_action( 'woocommerce_check_cart_items', 'conditionally_allowing_checkout' );
    add_action( 'woocommerce_checkout_process', 'conditionally_allowing_checkout' );
    function conditionally_allowing_checkout() {
        if ( ! is_store_open() ) {
            // Store closed
            wc_add_notice( __("The Store is Closed… Purchases are allowed from 12:00 AM to 22:00 PM"), 'error' );
        }
    }
    
    add_action( 'template_redirect', 'closing_shop_notice' );
    function closing_shop_notice(){
        if ( ! ( is_cart() || is_checkout() ) && ! is_store_open() ) {
            // Store closed notice
            $message = esc_html__(' We are currently closed. Welcome back tomorrow morning at 09.00 AM. ', 'woocommerce' );
            wc_add_notice( '<span class="shop-closed">' . $message . '</span>', 'notice' );
        }
    }
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MySQL创建时出现1064以下情况怎么办?
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重