This publish goes to be a brief one as a result of the answer to the query “The best way to present Product Already in Cart” button is moderately easy one.
Video: The best way to Present Product Already in Cart button
The best way to Present Product Already in Cart button in Woocommerce?
You simply have to seize a code snippet proven under and paste it to your capabilities.php file or higher but, use Code Snippets plugin. This lets you add your personal customized snippets whithoout dropping them after themes updates.
Step 1: Set up Code Snippets plugin
You’ll be able to obtain it right here. After putting in and activating it you’ll see the Snippets menu on the left. Click on on Add new hyperlink.
Step 2: Insert the code
Now, copy the code right here under and paste it to the Code Snippets code field. Select “Solely run on web site front-end” and press on “Save modifications and activate button”.
// Change button for single merchandise
add_filter( 'woocommerce_product_single_add_to_cart_text', 'already_in_cart_single_product' );
operate already_in_cart_single_product( $label ) {
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$product = $values['data'];
if( get_the_ID() == $product->get_id() ) {
$label = __('Product already in Cart. Add once more?', 'woocommerce');
}
}
return $label;
}
// Change button for archive web page merchandise
add_filter( 'woocommerce_product_add_to_cart_text', 'already_in_cart_archive_product', 99, 2 );
operate already_in_cart_archive_product( $label, $product ) {
if ( $product->get_type() == 'easy' && $product->is_purchasable() && $product->is_in_stock() ) {
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if( get_the_ID() == $_product->get_id() ) {
$label = __('Product already in Cart. Add once more?', 'woocommerce');
}
}
}
return $label;
}
Subsequent, go to the store and single product web page and confirm whether or not all the pieces works because it ought to. If all the pieces is OK then the tip consequence ought to appear to be this.