Пример #1
0
 /**
  * Duplicate the current existing cart. Only the token is changed
  *
  * @param string $token
  * @param Customer $customer
  * @param Currency $currency
  * @param EventDispatcherInterface $dispatcher
  * @return Cart
  * @throws \Exception
  * @throws \Propel\Runtime\Exception\PropelException
  */
 public function duplicate($token, Customer $customer = null, Currency $currency = null, EventDispatcherInterface $dispatcher = null)
 {
     if (!$dispatcher) {
         return false;
     }
     $cartItems = $this->getCartItems();
     $cart = new Cart();
     $cart->setAddressDeliveryId($this->getAddressDeliveryId());
     $cart->setAddressInvoiceId($this->getAddressInvoiceId());
     $cart->setToken($token);
     $discount = 0;
     if (null === $currency) {
         $currencyQuery = CurrencyQuery::create();
         $currency = $currencyQuery->findPk($this->getCurrencyId()) ?: $currencyQuery->findOneByByDefault(1);
     }
     $cart->setCurrency($currency);
     if ($customer) {
         $cart->setCustomer($customer);
         if ($customer->getDiscount() > 0) {
             $discount = $customer->getDiscount();
         }
     }
     $cart->save();
     foreach ($cartItems as $cartItem) {
         $product = $cartItem->getProduct();
         $productSaleElements = $cartItem->getProductSaleElements();
         if ($product && $productSaleElements && $product->getVisible() == 1 && ($productSaleElements->getQuantity() >= $cartItem->getQuantity() || $product->getVirtual() === 1 || !ConfigQuery::checkAvailableStock())) {
             $item = new CartItem();
             $item->setCart($cart);
             $item->setProductId($cartItem->getProductId());
             $item->setQuantity($cartItem->getQuantity());
             $item->setProductSaleElements($productSaleElements);
             $prices = $productSaleElements->getPricesByCurrency($currency, $discount);
             $item->setPrice($prices->getPrice())->setPromoPrice($prices->getPromoPrice())->setPromo($productSaleElements->getPromo());
             $item->save();
             $dispatcher->dispatch(TheliaEvents::CART_ITEM_DUPLICATE, new CartItemDuplicationItem($item, $cartItem));
         }
     }
     try {
         $this->delete();
     } catch (\Exception $e) {
         // just fail silently in some cases
     }
     return $cart;
 }