addProductToCollection
The addProductToCollection hook is triggered every time a product is added to the cart.
It allows manipulating the number of items added to the cart.
Parameters
-
\Isotope\Interfaces\IsotopeProduct
$productThe product that is added to the collection.
-
int
$quantityThe quantity of the product that is added to the collection.
-
\Isotope\Interfaces\IsotopeProductCollection
$collectionThe collection the product is added to.
-
array
$configThe model configuration.
Example
// src/EventListener/Isotope/AddProductToCollectionListener.php
namespace App\EventListener\Isotope;
use Isotope\Interfaces\IsotopeProduct;
use Isotope\Interfaces\IsotopeProductCollection;
use Isotope\ServiceAnnotation\IsotopeHook;
/**
* @IsotopeHook("addProductToCollection")
*/
class AddProductToCollectionListener
{
public function __invoke(IsotopeProduct $product, int $quantity, IsotopeProductCollection $collection, array $config): int
{
// example for allowing max 5 items per product
if ($quantity > 5) {
return 5;
}
return $quantity
}
}