Time based Enable/Disable Add to Cart in Woocommerce

依然范特西╮ 提交于 2021-02-04 20:50:50

问题


I would like my online shop to be deactivated or activated for a certain period of time. When the shop is deactivated, you can see the products, but can't put them in the shopping cart. If you already have things in your shopping cart, you can't checkout and pay. Despite all this, you can see the products, but you can't buy them.

Shop time would be as follows:

  • Online: Monday to Sunday from 12 am to 22 pm
  • Offline: the remaining time is to be deactivated

Is that possible?

I've already found a thread here, but it doesn't explain exactly how to do all the customizing.

I hope you can help me.


回答1:


The following code will restrict your shop opening hours from 12 AM to 22 PM disabling add to cart and checkout process during closing hours.

You will have to set your timezone in the first function settings:

// 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/Paris');

    // Below your shop time and dates settings
    $start_time = mktime('12', '00', '00', date('m'), date('d'), date('Y')); // 12:00:00
    $end_time   = mktime('22', '00', '00', date('m'), date('d'), date('Y')); // 22: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
        wc_add_notice( __("The Store is Closed… Purchases are allowed from 12:00 AM to 22:00 PM"), 'notice' );
    }
}

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




回答2:


It was a bit more complicated with the opening hours, so I looked for a plugin again. Opening hours were: Mo,We,Th 12-14 o'clock and 16:45-21:45 Tu: close Fri,Sat,Su: 12-21:45

I could have programmed it myself with the example of LoicTheAztec, but I don't have the time. The plugin I found now is called Woocommerce Store Closing by Ozibal https://codecanyon. net/item/woocommerce-store-closing/19398781

it costs 19 dollar and is it verry good. Thank you a lot <3




回答3:


Your solutions lies within this plugin: Woocommerce Open Close

This plugin does exactly what you need. If you don't want to use a plugin, I would recommend tearing down the plugin files and looking at how they have developed it. You will find how they are managing the timezones.



来源:https://stackoverflow.com/questions/54369492/time-based-enable-disable-add-to-cart-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!