woocommerce-cart-stock-reducer icon indicating copy to clipboard operation
woocommerce-cart-stock-reducer copied to clipboard

Add option to hide 'Add to cart' button if unavailable

Open jamesgol opened this issue 8 years ago • 3 comments

The javascript from WooCommerce marks the 'Add to cart' button disabled when an item is unavailable.

Requested via WP forum: https://wordpress.org/support/topic/hiding-add-to-cart-button?replies=1

jamesgol avatar Mar 21 '16 18:03 jamesgol

Hi, James.

Could you make any improvements regarding this problem?

Thanks for your support.

Gabrielaortiz avatar Jun 14 '20 23:06 Gabrielaortiz

Happy to accept a code contribution to do it, but I haven't had a lot of available free time to work on this lately.

jamesgol avatar Jun 14 '20 23:06 jamesgol

I am a beginner and I am trying to learn from this world but tryed to give a solution to this issue, in my case I wanted that when a client added the product in their session to their cart the button changed to "purchased", and in another client's session the button changed to "not available" on the shop page. If they want to hide the button they must change the html code. Maybe it is not suitable or there may be a better version, but this worked for me.

Added in:

public function __construct() { if ( 'yes' === $this->cart_stock_reducer ) {

This code:

add_action( 'woocommerce_loop_add_to_cart_link',array( $this, 'button_unavailable'), 10,2 ); 

And outside the function this:

 public function button_unavailable($is_purchasable, $product){ 
  $virtual_stock = $this->get_virtual_stock_available( $product); 
  if ( isset( $virtual_stock ) && $virtual_stock <= 0 ) { 
       foreach ( WC()->cart->get_cart() as $cart_item ){ 
           $product_id = $cart_item['product_id']; 
           if( $product_id == $product->get_id() ) { 
               //Html code for the Buyed Button 
               $client_buy = sprintf( '<a style="background-color:#229954;" class="button disabled" data-product-id="%s" href="javascript:void(0)">' . "¡Comprado!" . '</a>', 
                   esc_attr( $product->get_id() )); 
               return $client_buy; 
           } 
       } 
         //Html code for the unavailable Button 
       $other_client_buy = sprintf( '<a style="background-color:#B03A2E;" class="button  disabled" data-product-id="%s" href="javascript:void(0)">' . "¡No disponible!" . '</a>', 
                     esc_attr( $product->get_id() )); 
      return $other_client_buy; 
  } 
  else return $is_purchasable;  } 

Gabrielaortiz avatar Jun 15 '20 03:06 Gabrielaortiz