add to cart to Added when product is added and back to add to cart when removed
-
i am trying to use the below code to show added if the product is added to cart
but problem is when i remove the product from it showing as removed but when i reload the page it still showing the product in the cart
when i remove from cart it is also not changing text back to add to cart/**
- @snippet Change “Add to Cart” Button Label if Product Already @ Cart
- @how-to Get CustomizeWoo.com FREE
- @author Rodolfo Melogli
- @compatible WC 5.0
- @donate $9 https://businessbloomer.com/bloomer-armada/
*/
// Part 1
// Single Product Page Add to Cartadd_filter( ‘woocommerce_product_single_add_to_cart_text’, ‘bbloomer_custom_add_cart_button_single_product’, 9999 );
function bbloomer_custom_add_cart_button_single_product( $label ) {
if ( WC()->cart && ! WC()->cart->is_empty() ) {
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$product = $values[‘data’];
if ( get_the_ID() == $product->get_id() ) {
$label = ‘Already in Cart. Add again?’;
break;
}
}
}
return $label;
}// Part 2
// Loop Pages Add to Cartadd_filter( ‘woocommerce_product_add_to_cart_text’, ‘bbloomer_custom_add_cart_button_loop’, 9999, 2 );
function bbloomer_custom_add_cart_button_loop( $label, $product ) {
if ( $product->get_type() == ‘simple’ && $product->is_purchasable() && $product->is_in_stock() ) {
if ( WC()->cart && ! WC()->cart->is_empty() ) {
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values[‘data’];
if ( get_the_ID() == $_product->get_id() ) {
$label = ‘Already in Cart. Add again?’;
break;
}
}
}
}
return $label;
}The page I need help with: [log in to see the link]
- The topic ‘add to cart to Added when product is added and back to add to cart when removed’ is closed to new replies.