1
votes

I'm using a plugin called "one click chat to order" to order products via whatsapp and I've hided the add to cart button on single product page for all products. I've to show add to cart button only if the product weight is lesser than 50kg.

sample output

need to show add to cart button after the quote button here. I've used this snippet to hide add to cart button based on my condition. But it hides the whatsapp quote button as well. I don't know why?

add_action( 'woocommerce_single_product_summary', 'action_single_product_summary_callback', 1 );
function action_single_product_summary_callback() {
global $product;

$weight = $product->get_weight();
preg_replace('/\D/', '', $weight);

if ( $weight > 50 ) {

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

I'm new to woocommerce. Kindly help to solve this.

1
does your product type is variable?Bhautik
@Bhautik yes, its variable.Phoenix shadez
Check my answer below.Bhautik

1 Answers

2
votes

You are removing woocommerce_single_product_summary. and your used plugin one click chat to order that used woocommerce_after_add_to_cart_button and woocommerce_after_add_to_cart_form to display whatsapp quote button.

these are below conditions they are using to display whatsapp quote button

if ( get_option('wa_order_single_product_button_position') == 'after_atc') {
    add_action( 'woocommerce_after_add_to_cart_button', 'wa_order_add_button_plugin', 5 );
} elseif ( get_option('wa_order_single_product_button_position') == 'under_atc') {
    add_action( 'woocommerce_after_add_to_cart_form', 'wa_order_add_button_plugin', 5 );
} elseif (get_option('wa_order_single_product_button_position') == 'after_shortdesc') {
    add_action( 'woocommerce_before_add_to_cart_form', 'wa_order_add_button_plugin', 5 );
} else {
    add_action( 'woocommerce_after_add_to_cart_button', 'wa_order_add_button_plugin', 5 );
}

You can display whatsapp quote button button by their settings. there are three ways to display. default is After Add to Cart Button (Default). that you removed by woocommerce_single_product_summary so you can use Under Add to Cart Button. option.

After Add to Cart Button (Default).
Under Add to Cart Button.
After Short Description.

enter image description here

Or you can hide by CSS.

add_action( 'woocommerce_single_product_summary', 'action_single_product_summary_callback', 1 );
function action_single_product_summary_callback() {
    global $product;

    $weight = $product->get_weight();
    preg_replace('/\D/', '', $weight);

    if ( $weight > 50 ) {
        ?>
        <style type="text/css">
            .single_add_to_cart_button{display: none;}
        </style>
        <?php
    }
}

Tested and works

enter image description here