Time based Enable/Disable WooCommerce add to cart functionality - php

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.

i'm trying to write my own snippet and this is what I achieved so far:
add_action( 'wp_head', 'disable_checkout_sunday' );
function disable_checkout_sunday(){
if (date(D) == "Sun"){
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');
}
}

Related

I'm trying to disable the buy buttons on my WordPress site for products that are virtual

as the title states, I'm trying to remove the buy button for virtual products. I am using the Astra theme in WordPress alongside Woocommerce, and Elementor.
Now my issue is that with my current implementation, it does remove the button from the individual product page. However it still displays the button on the categories page, which is a pain. I have tried setting the price to nothing, and while that does work. It doesn't help as I still want the products to have their prices listed.
I have added the following code to the functions.php section of my duplicated theme file;
function buy_filter()
{
if ( ! is_product() ) return;
$product = get_product();
if ($product->is_virtual('yes'))
{
//$product->is_purchasable('false')
//remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
//remove_action('woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30);
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', 30);
remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30);
remove_action( 'woocommerce_grouped_add_to_cart', 'woocommerce_grouped_add_to_cart', 30);
remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30);
remove_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30);
}
}
add_action ('wp', 'buy_filter');
Any help would be greatly appreciated, and I'm happy to give any more details I can to aid in the matter.
Thank you kindly!
You need to get the specific product by it's ID, I have modified a few part of your code and removed few of the hooks you used. Please add those if required.
function woocommerce_remove_cart()
{
if ( ! is_product() ) return;
$product_id=get_the_ID();
$product = wc_get_product($product_id);
if ($product->is_virtual('yes'))
{
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 );
}
}//end of function
add_action('wp_head','woocommerce_remove_cart');
Hope it works.
add_filter( 'woocommerce_is_purchasable', 'woocommerce_isproduct_purchasable', 10, 2 );
function woocommerce_isproduct_purchasable( $purchasable, $product ) {
return ($product->is_virtual( 'yes' ) ) ? false : $purchasable;
}
This is the hook you are looking for

How i can change default add to cart button in WooCommerce?

This is how it all looks
I want to add a new "add_to_cart" button template for the outer product card without affecting the inner one. but when I add, the old one remains. What's wrong with my code?
it is my code in functions.php
function loop_custom_variable_products() {
global $product;
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 30 ); // Remove price
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 30); // remove add to cart
remove_action( 'woocommerce_after_shop_loop_item', 'loop_variations_custom_buttons_and_prices', 30 ); // Add prices with custom buttons
}
add_action( 'init', 'loop_custom_variable_products' );
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_variable_add_to_cart1', 30 );
//add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_variable_add_to_cart1', 30);
function woocommerce_variable_add_to_cart1(){
//... some code for new add to card style
}

Change Woocommerce single product templates order based on product tag

I have two templates, one for Panorama & Multi-Panel photographs, the other for Normal ones.
I'm trying to code a conditional statement that checks the product tag in order to select the relevant template to be displayed.
Panorama photos have the product tag 'panorama' or 'multi-panel'.
If the 'panorama' or 'multi-panel' tag is true >> perform some remove_action and add_action functionality on the Panorama product page...
...otherwise do nothing to the Normal product page...
This is what I have been dappling with to no affect!!
function robhemphill_panorama_layout () {
if ( has_tag( array( 'panorama', 'multi-panel'))) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_template_single_title', 5 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_template_single_excerpt', 10 );
} else {
// Do nothing
}
}
add_action( 'woocommerce_single_product_summary', 'robhemphill_panorama_layout' );
Any thoughts or ideas would be great, can't find anything online!
As you are targeting Woocommerce product tags (but not WordPress tags) try this instead:
add_action( 'woocommerce_single_product_summary', 'robhemphill_panorama_layout', 1 );
function robhemphill_panorama_layout () {
global $product;
if ( has_term( array( 'panorama', 'multi-panel' ), 'product_tag', $product->get_id() ) ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_template_single_title', 5 );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_template_single_excerpt', 10 );
}
}
Code goes in function.php file of your active child theme (or theme).
Tested and works.

Arranging Woocommerce Variable Product Data

I am designing Woocommerce single product page,
I have designed product page for simple product
now I am designing for variable product for that I have created this function
to arrange "Choose variation" selection and "Add to cart" button
function filter_grouped_cart(){
global $post;
if( function_exists('get_product') ){
$product = get_product( $post->ID );
if( $product->is_type( 'variable' ) ){
remove_action( 'woocommerce_variable_add_to_cart',
'woocommerce_variable_add_to_cart', 30 );
add_action( 'woocommerce_before_single_product_summary',
'woocommerce_variable_add_to_cart', 45 );
remove_action( 'woocommerce_single_variation',
'woocommerce_single_variation_add_to_cart_button', 20 );
add_action( 'woocommerce_before_single_product_summary',
'woocommerce_single_variation_add_to_cart_button', 60 );
}
}
}
I used conditional logic because without it my simple product page show nothing.
This code is working fine but I have these actions and their priority
remove_action( 'woocommerce_before_single_product_summary',
'woocommerce_show_product_images', 20 );
add_action( 'woocommerce_single_product_summary',
'woocommerce_show_product_images', 20 );
remove_action( 'woocommerce_single_product_summary',
'woocommerce_template_single_title', 5 );
add_action( 'woocommerce_before_single_product_summary',
'woocommerce_template_single_title', 30 );
remove_action( 'woocommerce_after_single_product_summary',
'woocommerce_output_product_data_tabs', 10 );
add_action( 'woocommerce_before_single_product_summary',
'the_content', 40 );
remove_action( 'woocommerce_single_product_summary',
'woocommerce_template_single_price', 10 );
add_action( 'woocommerce_before_single_product_summary',
'woocommerce_template_single_price', 50 );
remove_action( 'woocommerce_single_product_summary',
'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_before_single_product_summary',
'woocommerce_template_single_add_to_cart', 60 );
I want "Choose Option" selection before price i.e.
add_action( 'woocommerce_before_single_product_summary',
'woocommerce_template_single_price', 50 );
Its priority is 50
so i have added priority 45 for
add_action( 'woocommerce_before_single_product_summary',
'woocommerce_variable_add_to_cart', 45 );
But still woocommerce_variable_add_to_cart is showing below
woocommerce_template_single_price
In the above picture if I have 45-50-60 then 60 and 50 will come in single line, and 45 above 50, that is my requirement.
I tried you code but it broke the structure of the page for me here.
Then ultimately it lead me to research through the hooks and tried some permutation and combination which resulted in this code.
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
add_action( 'woocommerce_single_variation', 'woocommerce_template_single_price', 19 );
Hope that works for you. Have not added any conditional logic here to restrict it from getting applied on other types of products(I think that you can handle very well).

WooCommerce Hide Add To Cart Button on Loop & Product Page

I'm working on a woocommerce theme and I'm required to hide the add to cart button for products that have 0 as price, as these products may only be inquired and not added to cart. I have successfully hidden the 'add to cart' button on the product page however, I am having a hard time doing so on the shop page/category page.
Below is my code for filtering the add to cart as well as changing the default 'Free!' message.
add_filter( 'woocommerce_variable_free_price_html', 'hide_free_price_notice' );
add_filter( 'woocommerce_free_price_html', 'hide_free_price_notice' );
add_filter( 'woocommerce_variation_free_price_html', 'hide_free_price_notice' );
/**
* Changes woocommerce default 'Free!' to return message
*/
function hide_free_price_notice( $price ) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 30 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
return 'Please inquire for pricing';
}
I have also tried filtering all add to cart buttons on loop, however this did not work either.
add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 );
function remove_add_to_cart_buttons() {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
}
Any suggestions? I'm looking to see if I can hide it with CSS...
This is what I use to change the 'Free!' message to 'POA' and also hide the cart.
Note: There seems to be an issue with this on WooCommerce version V2.1?
* Swop the 'Free!' price notice and hide the cart with 'POA' in WooCommerce
*/
add_filter( 'woocommerce_variable_free_price_html', 'hide_free_price_notice' );
add_filter( 'woocommerce_free_price_html', 'hide_free_price_notice' );
add_filter( 'woocommerce_variation_free_price_html', 'hide_free_price_notice' );
function hide_free_price_notice( $price ) {
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 );
return 'POA';
}

Categories