0

In WooCommerce, I would like to open sales for limited time, like add to cart button will show only for 3 hours and be hidden for all other hours.

I now I can remove add to cart button using remove_action(), this way:

 remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );

But how to hide it during an hour range? Is this possible?

Any help will be appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
geniuscapri
  • 94
  • 3
  • 12

1 Answers1

3

The following code will enable add-to-cart buttons during hours range to be defined in the first function. You will have to define also the timezone of your shop location. To get a specific timezone.

Outside that hours range:

  • In shop and archives pages, the add-to-cart button will be replaced by a linked button to the product single page.
  • in single product pages the add-to-cart button will be replaced by an inactive custom button.

The code:

// Conditional time function (Define: start and end time / Timezone)
function conditional_hours_range(){
    // HERE below, define start / end hours range and time zone (default is 'UTC').
    $start_hour = 13;
    $end_hour = 16;
    date_default_timezone_set ('Europe/Paris');

    $now = strtotime("now"); // Now time
    $today_time = strtotime(date("Y-m-d")); // Today time at 00:00
    $starting_time = $today_time + ( $start_hour * 3600 );
    $ending_time = $today_time + ( $end_hour * 3600 );
    // return true or false
    return $now >= $starting_time && $now <= $ending_time ? false : true;
}

// Replacing the button add to cart by a link to the product in Shop and archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product  ) {
    // Only on custom hour range
    if( conditional_hours_range() ){
        $button_text = __( "View product", "woocommerce" );
        $button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
    }
    return $button;
}


// Replacing the button add to cart by an inactive custom button
add_action( 'woocommerce_single_product_summary', 'replace_single_add_to_cart_button', 1 );
function replace_single_add_to_cart_button() {
    // Only on custom hour range
    if( conditional_hours_range() ){
        global $product;

        // For variable product types (keeping attribute select fields)
        if( $product->is_type( 'variable' ) ) {
            remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
            add_action( 'woocommerce_single_variation', 'custom_disabled_button', 20 );
        }
        // For all other product types
        else {
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
            add_action( 'woocommerce_single_product_summary', 'custom_disabled_button', 30 );
        }
    }
}

// The custom replacement inactive button
function custom_disabled_button(){
    $button_text = __( "Soon enabled", "woocommerce" );
    $style = 'style="background:Silver !important; color:white !important; cursor: not-allowed !important;"';
    echo '<a class="button" '.$style.'>' . $button_text . '</a>';
}

Code goes in function.php file of the active child theme (or active theme).

Tested and works

enter image description here

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thank you very much, your solution solved my problem. Now i have another problem. i want to use address from own membership plugin fields on woocommerce. – geniuscapri Jan 17 '18 at 19:14
  • I have question about this answer. https://stackoverflow.com/questions/45806249/change-product-variation-prices-via-a-hook-in-woocommerce-3 How can i get custom field value from variation and then plus in price? – geniuscapri Jan 28 '18 at 22:04