Jump to content
PawelTR

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

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)

Share this post


Link to post
Share on other sites
24 minutes ago, PawelTR said:

No idea?

Hi,

You are looking  to something  similar  to this  module

 

Share this post


Link to post
Share on other sites
On 2/11/2020 at 1:03 PM, PawelTR said:

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.

For  that  part this  free module will do the job :

 

 

Share this post


Link to post
Share on other sites

Hi @ndiaga thank for answer :) I'm trying to write my own basket module, but I have no idea where to start and what it should look like :(

Share this post


Link to post
Share on other sites
1 hour ago, PawelTR said:

Hi @ndiaga thank for answer :) I'm trying to write my own basket module, but I have no idea where to start and what it should look like :(

Reading  already existing  module  codes  is  a good way  to go.

Share this post


Link to post
Share on other sites
43 minutes ago, PawelTR said:

@ndiaga How can i override CartController in my custom module?

You can  not  override  a  controller  from  another  controller.

In  your case  you can just  use  hooks  from  the  cart.

 

Share this post


Link to post
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)

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More