Reading list Switch to dark mode

    How to restrict Product Quantity of a Order to buy in PrestaShop

    Updated 21 January 2019

    If you want to restrict a client to buy only a specific number of quantity of a product in PrestaShop this blog will help you in the same with some lines of script. you will just need to override function checkQuantities() of Cart.php class, in PrestaShop this function checks if product quantities in Cart are available in stock or not.

    /* override function*/
    public function checkQuantities($return_product = false)
    {
        $parentResult = parent::checkQuantities($return_product);
        if ($parentResult) {
            $cartProducts = $this->getProducts();
            if (!empty($cartProducts)) {
                foreach ($cartProducts as $productList) {
                    //-------our conditions as require---------
                    -------------------------------------------
                    $ristrictedQty = 1; //max quantity 
                    $currentQuantity = $productList['quantity'];
                    if ($currentQuantity > $ristrictedQty) {
                        return $productList;
                    }
                }
            }
        }
    
        return $parentResult;
    }

    let’s discuss this code first This function should return an array of the product if you want to restrict a product else true this is because there are some restriction. now to know this restriction lets see this function call this function calls in CartController of the front controller of PrestaShop in the private function areProductsAvailable().

    private function areProductsAvailable()
    {
        $product = $this->context->cart->checkQuantities(true);
    
        if (true === $product || !is_array($product)) {
            return true;
        }
        if ($product['active']) {
            return $this->trans(
                'The item %product% in your cart is no longer available in this quantity. You cannot proceed with your order until the quantity is adjusted.',
                array('%product%' => $product['name']),
                'Shop.Notifications.Error'
            );
        }
    
        return $this->trans(
            'This product (%product%) is no longer available.',
            array('%product%' => $product['name']),
            'Shop.Notifications.Error'
        );
    }

    Hence the error above written in areProductsAvailable() will be shown something like this on cart page,

    prestahop-cart-page

    and the customer can’t order as the checkout button is disabled. even if on changing the disable attribute by inspect element the page will again redirect on this cart page.

    Searching for an experienced
    Prestashop Company ?
    Find out More

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home