Ejemplo n.º 1
0
 public function testCreateFromCart()
 {
     $productX = $this->dummyData->getProduct('PROD-X');
     $productY = $this->dummyData->getProduct('PROD-X');
     $cartPriceRuleItem = $this->dummyData->getCartPriceRuleProductItem($productX);
     $cartPriceRuleDiscount = $this->dummyData->getCartPriceRuleDiscount($productY);
     $cartPriceRule = $this->dummyData->getCartPriceRule();
     $cartPriceRule->setName('Buy X get Y FREE');
     $cartPriceRule->addItem($cartPriceRuleItem);
     $cartPriceRule->addDiscount($cartPriceRuleDiscount);
     $catalogPromotion = $this->dummyData->getCatalogPromotion();
     $catalogPromotion->setName('10% OFF Site Wide Catalog Promotion');
     $productQuantityDiscount = $this->dummyData->getProductQuantityDiscount($productX);
     $pricing = $this->dummyData->getPricing([$catalogPromotion], [$productQuantityDiscount]);
     $pricing->setCartPriceRules([$cartPriceRule]);
     $cartCalculator = $this->dummyData->getCartCalculator($pricing);
     $user = $this->dummyData->getUser();
     $coupon = $this->dummyData->getCoupon();
     $taxRate = $this->dummyData->getTaxRate();
     $shipmentRate = $this->dummyData->getShipmentRate(1000);
     $cartItem1 = $this->dummyData->getCartItem($productX);
     $cartItem2 = $this->dummyData->getCartItem($productY);
     $cart = $this->dummyData->getCart([$cartItem1, $cartItem2]);
     $cart->setUser($user);
     $cart->addCoupon($coupon);
     $cart->setTaxRate($taxRate);
     $cart->setShipmentRate($shipmentRate);
     $orderId = Uuid::uuid4();
     $order = Order::fromCart($orderId, $user, $cart, $cartCalculator, '10.0.0.1');
     $this->assertTrue($order instanceof Order);
     $this->assertSame('10.0.0.1', $order->getIp4());
     $this->assertSame($user, $order->getUser());
     $this->assertSame($coupon, $order->getCoupons()[0]);
     $this->assertSame($taxRate, $order->getTaxRate());
     $this->assertSame($shipmentRate, $order->getShipmentRate());
     $orderItem1 = $order->getOrderItems()[0];
     $this->assertEntitiesEqual($catalogPromotion, $orderItem1->getCatalogPromotions()[0]);
     $this->assertEntitiesEqual($productQuantityDiscount, $orderItem1->getProductQuantityDiscounts()[0]);
     $this->assertSame('10% OFF Site Wide Catalog Promotion, Buy 1 or more for 5% off', $orderItem1->getDiscountNames());
     $this->assertEntitiesEqual($cartPriceRule, $order->getCartPriceRules()[0]);
     $this->assertSame('Buy X get Y FREE, 20% OFF Test Coupon', $order->getDiscountNames());
 }
Ejemplo n.º 2
0
 /**
  * @param UuidInterface $orderId
  * @param User $user
  * @param Cart $cart
  * @param CartCalculatorInterface $cartCalculator
  * @param string $ip4
  * @param OrderAddress $shippingAddress
  * @param OrderAddress $billingAddress
  * @param CreditCard $creditCard
  * @return Order
  */
 public function createOrderFromCart(UuidInterface $orderId, User $user, Cart $cart, CartCalculatorInterface $cartCalculator, $ip4, OrderAddress $shippingAddress, OrderAddress $billingAddress, CreditCard $creditCard)
 {
     $this->throwValidationErrors($creditCard);
     $order = Order::fromCart($orderId, $user, $cart, $cartCalculator, $ip4);
     $order->setShippingAddress($shippingAddress);
     $order->setBillingAddress($billingAddress);
     $this->throwValidationErrors($order);
     $this->reserveProductsFromInventory($order);
     $this->addCreditCardPayment($order, $creditCard, $order->getTotal()->total);
     $this->orderRepository->create($order);
     $this->eventDispatcher->dispatchEvent(new OrderCreatedFromCartEvent($order->getId()));
     return $order;
 }