Jump to content

[Solved]Cart limit for user per day, week, month and max 10 pieces of single product


PawelTR

Recommended Posts

Hello,
I try write module which limit cart. I mean limit:
- max 10 pieces of single product in one order
- max 1 pieces of single product which in database have flag "deficit"

This things i have check when user adding product to cart. When user adding product with quantity >= 10 or deficyt product >1 then i need show message with information.

I have code:

 public function hookActionBeforeCartUpdateQty($params)
    {
        $product = $params['product'];
        $quantity = $params['quantity'];

        echo '<pre>';
        echo 'Product name: ' . $product->name . '<br />';
        echo 'Product id: ' . $product->id . '<br />';
        echo 'Quantity' .$quantity;
        echo '</pre>';

        if($quantity >=10) {
            return false;
        }

    }

but not working, when i add product with qunantity >=10, product is added successfully, this is my first problem.

My second problem is that I want to write a module that will develop a product that will have flags:
- 1 item per day for 1 user
- 1 item per week for 1 user
- 1 item per month for 1 user

And I would like to check it when adding it to the cart, but it can't be done because I can't check if a person who has not logged in has already bought the product, he must be logged in, so I am looking for ideas on how to solve it.

Edited by PawelTR (see edit history)
Link to comment
Share on other sites

@ndiaga I copy CartController to override/controllers/front in my module and add this code for him:

 <?php

use PrestaShop\PrestaShop\Adapter\Presenter\Cart\CartPresenter;

class CartController extends CartControllerCore
{
    public $php_self = 'cart';

    public function init()
    {
        parent::init();
        $this->qty = abs(Tools::getValue('qty', 1));
        var_dump(1);

        if ($this->qty >= 2) {
            #How can i show notification?
        }
    }

}

all works fine, but how i can show for example js message when $this->qty is > = 2 ??

Edited by PawelTR (see edit history)
Link to comment
Share on other sites

/controllers/front/CartController.php

In

function processChangeProductInCart()

Add this 

 if ($qty_to_check >= 10) {
 $this->errors[] = $this->trans(
'The maximun purchase for the product %product% may not exceed %quantity%.',
 array('%product%' => $product->name, '%quantity%' => 9),
'Shop.Notifications.Error'
);
return;
}

Result

spacer.png

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...