/** * Returns true if the basket element is still valid * * @param \Sonata\Component\Basket\BasketInterface $basket * @param \Sonata\Component\Product\ProductInterface $product * @param \Sonata\Component\Basket\BasketElementInterface $basketElement * @return bool|\Sonata\Component\Basket\BasketElementInterface */ public function basketAddProduct(BasketInterface $basket, ProductInterface $product, BasketElementInterface $basketElement) { if ($basket->hasProduct($product)) { return false; } $basketElementOptions = $product->getOptions(); // add the default product options to the basket element if (is_array($basketElementOptions) && !empty($basketElementOptions)) { foreach ($basketElementOptions as $option => $value) { $basketElement->setOption($option, $value); } } $this->updateComputationPricesFields($basket, $basketElement, $product); $basket->addBasketElement($basketElement); return $basketElement; }
/** * Adds $basketElement related to $product to $basket * * @param \Sonata\Component\Basket\BasketInterface $basket * @param \Sonata\Component\Product\ProductInterface $product * @param \Sonata\Component\Basket\BasketElementInterface $basketElement * * @return bool|\Sonata\Component\Basket\BasketElementInterface * * @throws \Sonata\Component\Basket\InvalidProductException */ public function basketAddProduct(BasketInterface $basket, ProductInterface $product, BasketElementInterface $basketElement) { $event = new AddBasketElementEvent($basket, $basketElement, $product, $this); $this->getEventDispatcher()->dispatch(BasketEvents::PRE_ADD_PRODUCT, $event); if ($basket->hasProduct($product)) { return false; } if ($product->isMaster() && $this->hasVariations($product)) { throw new InvalidProductException(sprintf("You can't add '%s' to the basket as it is a master product with variations.", $product->getSku())); } $basketElementOptions = $product->getOptions(); // add the default product options to the basket element if (is_array($basketElementOptions) && !empty($basketElementOptions)) { foreach ($basketElementOptions as $option => $value) { $basketElement->setOption($option, $value); } } $this->updateComputationPricesFields($basket, $basketElement, $product); $basket->addBasketElement($basketElement); $event = new AddBasketElementEvent($basket, $basketElement, $product, $this); $this->getEventDispatcher()->dispatch(BasketEvents::POST_ADD_PRODUCT, $event); return $event->getBasketElement(); }