Jump to content

How to update the cart item count on the navbar, is it refreshed with ajax request? I have a custom module that add back all previous items in cart.


Recommended Posts

<?php
use Symfony\Component\HttpFoundation\JsonResponse;

class ReaddToCartModuleFrontController extends ModuleFrontController
{
    public $ajax = true;

    public function initContent()
    {
        parent::initContent();
        
        $productId = Tools::getValue('id_product');
        $quantity = Tools::getValue('qty', 1);

        // Debugging output
        error_log("Product ID: " . $productId);
        error_log("Quantity: " . $quantity);

        if (!$productId || !Validate::isUnsignedId($productId)) {
            error_log("Invalid product ID: " . $productId);
            $this->ajaxDie(json_encode(['success' => false, 'message' => 'Invalid product ID']));
        }

        $cart = $this->context->cart;
        if (!$cart->id) {
            $cart->add();
            $this->context->cookie->id_cart = $cart->id;
        }

        $updateQuantity = $cart->updateQty($quantity, $productId);
        if ($updateQuantity) {
            error_log("Product added to cart successfully");
            $this->ajaxDie(json_encode(['success' => true, 'message' => 'Product added to cart']));
        } else {
            error_log("Failed to add product to cart");
            $this->ajaxDie(json_encode(['success' => false, 'message' => 'Failed to add product to cart']));
        }
    }
}

 

Link to comment
Share on other sites

16 hours ago, Prestashop Addict said:

It can depend on theme and/or cart module. Can you specify theme, PS version, PHP version and cart module used and its configuration.

I'm not using any cart module but the original version PS 1.7, PHP 7.2 . Also is it possible to refresh just the cart and not the whole page? If possible what are the ways we can do that in prestashop?

Link to comment
Share on other sites

Il y a 1 heure, dwdsaq2 a dit :

I'm not using any cart module but the original version PS 1.7, PHP 7.2 . Also is it possible to refresh just the cart and not the whole page? If possible what are the ways we can do that in prestashop?

If you are using the Prestashop base and classic theme, the cart is managed by the module ps_shoppingcart. And you can set it to ajax mode or not.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...