• Resolved immuindia

    (@immuindia)


    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

    /**

    // Part 1
    // Single Product Page Add to Cart

    add_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 Cart

    add_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]

Viewing 1 replies (of 1 total)
  • Saif

    (@babylon1999)

    Hello @immuindia,

    Thank you for reaching out!

    Custom solutions are not something we can assist with per our support policy, however, the code you shared should work fine. :?)

    If you’re using AJAX to add products to the cart, then the button text is expected not to update unless you refresh the page. You can disable AJAX from WooCommerce → Settings → Products.


    Link to image: https://d.pr/i/0J57pF

    Let us know if there’s anything else we can assist you with. :?)

Viewing 1 replies (of 1 total)
  • The topic ‘add to cart to Added when product is added and back to add to cart when removed’ is closed to new replies.