I want the ‘voir le panier’ button to remain visible for products I’ve added to my cart in WooCommerce, even after refreshing the page. Right now, the buttons disappear when I refresh. I need help with this feature to ensure that the product remains displayed, similar to the example in the attached image.
i have added this ti add-to-cart.php
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
global $product;
$product_id = $product->get_id();
$cart = WC()->cart->get_cart();
$is_in_cart = false;
// Check if the product is already in the cart
foreach ( $cart as $cart_item ) {
if ( $cart_item['product_id'] == $product_id ) {
$is_in_cart = true;
break;
}
}
// Output the "Ajouter au Panier" button
$add_to_cart_button = sprintf(
'<a href="%s" data-quantity="%s" class="button-primary xsmall rounded wide %s add-to-cart-button" data-product_id="%d" %s>%s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
esc_attr( $product_id ),
isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
esc_html( $product->add_to_cart_text() )
);
// Show the "Ajouter au Panier" button
echo $add_to_cart_button;
// Show "Voir le Panier" button if the product is in the cart
if ( $is_in_cart ) {
echo sprintf(
'<a href="%s" class="button voir_panier_button" style="display:block;">%s</a>',
esc_url( wc_get_cart_url() ),
esc_html__( 'Voir le Panier', 'woocommerce' )
);
}
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
let viewCartButtonShown = false;
$('.add-to-cart-button').on('click', function(e) {
e.preventDefault();
var button = $(this);
var productId = button.data('product_id');
var viewCartButton = $('.voir_panier_button');
// Check if the product is already in the cart
if (viewCartButton.length === 0) {
// If the "Voir le Panier" button is not already shown
button.parent().append(viewCartButton);
viewCartButton.show();
viewCartButtonShown = true; // Mark the button as shown
} else {
// If the product is already in the cart, hide the "Voir le Panier" button
viewCartButton.hide();
}
// Optional: Hide the "added_to_cart wc-forward" button
$('.added_to_cart.wc-forward').hide();
});
});
</script>
2
Answers
thanks brother for the help iv try it before and still same problem but i did and alternative solution based on java that it change the text and the color of the button but the only thing im have issue with now is when i remove somthiing from the cart the button dosent back to intial state this is the code i used :
To ensure that the
Voir le Panier
button remains visible for products that are already in the cart even after a page refresh in WooCommerce, you’ll need to ensure that your PHP and Javascript logic are correctly handling the state persistence. From what you’ve provided the PHP part of your logic seems mostly set up to check if the product is in the cart and then show the button. However, there might be some tweaks necessary to ensure everything works seamlessly across page reloads.First, the following refined code is for PHP part to check the cart status.
For Javascript refinement you can do something like this: