Prevent user from adding items to basket

Questions around the TYPO3 integration and plugins
Forum rules
Always add your TYPO3, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
HisDudeness
Posts: 6
Joined: 05 Feb 2020, 11:24

Prevent user from adding items to basket

Post by HisDudeness » 05 Feb 2020, 12:07

Hello,

I'm completely new to Aimeos and currently trying to figure out, how to prevent items being added to the basket under certain circumstances. In my case the shop has a point-based currency. The amount of points a user has, will be stored in a database table. If the item(s) cost more points, than are available to the user, it shouldn't be possible to add them to the basket.

I already tried it with a Provider "Aimeos\MShop\Plugin\Provider\Order". I tried "check.before", "check.after", "addProduct.before", but to no avail. If I return false in "check.after" for example nothing happens at all. I dumped the variables, to see, if my code is executed correctly and everything seems fine.
With "addProduct.before" it isn't working correctly either. The Quantity in the value object ($value->getPrice()->getQuantity()) is always 1 (same goes for the basket object). If the item is already in the basket and i add more of it via +/- it throws an error message "A non-recoverable error occured". In this case it still ups the quantity and total price of the item, but not the total of the basket.

Here is a bit of code I use for testing with a fixed point value:

Code: Select all

class ExtendBasket extends \Aimeos\MShop\Plugin\Provider\Factory\Base
                       implements \Aimeos\MShop\Plugin\Provider\Iface, \Aimeos\MShop\Plugin\Provider\Factory\Iface
{
    public function register(\Aimeos\MW\Observer\Publisher\Iface $p) : \Aimeos\MW\Observer\Listener\Iface
    {
        $p->attach($this->getObject(), 'addProduct.before');
        return $this;
    }
    
    public function update(\Aimeos\MW\Observer\Publisher\Iface $basket, $event, $value = null)
    {
        if (!$basket instanceof $iface) {
            $msg = sprintf( 'Object is not of required type "%1$s"', $iface );
            throw new \Aimeos\MShop\Plugin\Provider\Exception($msg);
        }

        if ($event === 'addProduct.before') {
            if ($value != null) {
                    return $this->checkPoints((float)$value->getPrice()->getQuantity() * (float)$value->getPrice()->getValue());
            }
        }
        
        return $value;
    }
    
    protected function checkPoints($total)
    {
        $testPoints = 20000.0;
        
        if ($total > $testPoints) {
            return false;
        }
        
        return true;
    }
}
To sum it up: the aim is, to prevent the user from putting items into the basket if his available points can't cover them and it also should prevent him from checking the basket out in the end of course.

Any ideas how I could do that? Maybe a Provider class isn't the way to go? Or do I get the logic behind this all wrong?

User avatar
aimeos
Administrator
Posts: 7836
Joined: 01 Jan 1970, 00:00

Re: Prevent user from adding items to basket

Post by aimeos » 06 Feb 2020, 08:35

Using a basket plugin is the right way but you need to throw an exception if not enough credits are available instead of returning true/false. Furthermore, you get the product and the chosen quantity by the user when listening to "addProduct.before". You need to check the basket if there's the same product already in the basket using

Code: Select all

$qty = $value->getQuantity();
foreach( $basket->getProducts() as $orderProduct ) {
    if( $orderProduct->compare( $value ) === true ) {
        $qty += $orderProduct->getQuantity();
    }
}
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

HisDudeness
Posts: 6
Joined: 05 Feb 2020, 11:24

Re: Prevent user from adding items to basket

Post by HisDudeness » 06 Feb 2020, 09:10

Problem solved. Thank you very much for the fast reply. :)

Post Reply