public function testLoadCartPriceRules() { $cartPriceRule1 = $this->dummyData->getCartPriceRule(); $cartPriceRuleRepository = $this->mockRepository->getCartPriceRuleRepository(); $cartPriceRuleRepository->shouldReceive('findAll')->andReturn([$cartPriceRule1])->once(); $this->pricing->loadCartPriceRules($cartPriceRuleRepository); $cartPriceRules = $this->pricing->getCartPriceRules(); $this->assertEntitiesEqual($cartPriceRule1, $cartPriceRules[0]); }
public function getPricing() { static $pricing = null; if ($pricing === null) { $pricing = new Pricing(); $pricing->loadCatalogPromotions($this->getRepositoryFactory()->getCatalogPromotionRepository()); } return $pricing; }
private function calculateCatalogPromotions() { foreach ($this->pricing->getCatalogPromotions() as $catalogPromotion) { if ($catalogPromotion->isValid($this->pricing->getDate(), $this->product)) { $this->price->unitPrice = $catalogPromotion->getUnitPrice($this->price->unitPrice); $this->price->addCatalogPromotion($catalogPromotion); } } // No prices below zero! $this->price->unitPrice = max(0, $this->price->unitPrice); }
public function testGetPriceWithProductQuantityDiscountPercent() { $product = new Product(); $product->setUnitPrice(500); $productQuantityDiscount6 = new ProductQuantityDiscount($product); $productQuantityDiscount6->setType(PromotionType::percent()); $productQuantityDiscount6->setQuantity(6); $productQuantityDiscount6->setValue(5); $productQuantityDiscount12 = new ProductQuantityDiscount($product); $productQuantityDiscount12->setType(PromotionType::percent()); $productQuantityDiscount12->setQuantity(12); $productQuantityDiscount12->setValue(30); $productQuantityDiscount24 = new ProductQuantityDiscount($product); $productQuantityDiscount24->setType(PromotionType::percent()); $productQuantityDiscount24->setQuantity(24); $productQuantityDiscount24->setValue(35); $productQuantityDiscounts = $product->getProductQuantityDiscounts(); $this->pricing->setProductQuantityDiscounts($productQuantityDiscounts); $expectedPrice = new Price(); $expectedPrice->unitPrice = 500; $expectedPrice->origUnitPrice = 500; $expectedPrice->quantityPrice = 500; $expectedPrice->origQuantityPrice = 500; $this->assertEquals($expectedPrice, $this->pricingCalculator->getPrice($product, 1)); $expectedPrice = new Price(); $expectedPrice->unitPrice = 475; $expectedPrice->origUnitPrice = 500; $expectedPrice->quantityPrice = 2850; $expectedPrice->origQuantityPrice = 3000; $expectedPrice->addProductQuantityDiscount($productQuantityDiscount6); $this->assertEquals($expectedPrice, $this->pricingCalculator->getPrice($product, 6)); $expectedPrice = new Price(); $expectedPrice->unitPrice = 350; $expectedPrice->origUnitPrice = 500; $expectedPrice->quantityPrice = 4200; $expectedPrice->origQuantityPrice = 6000; $expectedPrice->addProductQuantityDiscount($productQuantityDiscount12); $this->assertEquals($expectedPrice, $this->pricingCalculator->getPrice($product, 12)); $expectedPrice = new Price(); $expectedPrice->unitPrice = 325; $expectedPrice->origUnitPrice = 500; $expectedPrice->quantityPrice = 7800; $expectedPrice->origQuantityPrice = 12000; $expectedPrice->addProductQuantityDiscount($productQuantityDiscount24); $this->assertEquals($expectedPrice, $this->pricingCalculator->getPrice($product, 24)); }
private function calculateCouponDiscounts() { foreach ($this->cart->getCoupons() as $key => $coupon) { if ($coupon->isValid($this->pricing->getDate(), $this->cartTotal->subtotal)) { $newSubtotal = $coupon->getUnitPrice($this->cartTotal->subtotal); $discountValue = $this->cartTotal->subtotal - $newSubtotal; $this->cartTotal->discount += $discountValue; $this->cartTotal->coupons[$key] = $coupon; if ($coupon->getReducesTaxSubtotal()) { $this->cartTotal->taxSubtotal -= $discountValue; } if ($coupon->getFlagFreeShipping()) { $this->cartTotal->shippingDiscount = $this->cartTotal->shipping; $this->cartTotal->discount += $this->cartTotal->shipping; } } } // No taxes below zero! $this->cartTotal->taxSubtotal = max(0, $this->cartTotal->taxSubtotal); }
/** * @param CatalogPromotion[]|null $catalogPromotions * @param ProductQuantityDiscount[]|null $productQuantityDiscounts * @return Pricing */ public function getPricing(array $catalogPromotions = null, array $productQuantityDiscounts = null) { if ($catalogPromotions === null) { $catalogPromotions = [$this->getCatalogPromotion()]; } if ($productQuantityDiscounts === null) { $productQuantityDiscounts = [$this->getProductQuantityDiscount()]; } $pricing = new Pricing(); $pricing->setCatalogPromotions($catalogPromotions); $pricing->setProductQuantityDiscounts($productQuantityDiscounts); return $pricing; }
/** * @param Pricing $pricing * @return static */ public function withProductQuantityDiscounts(Pricing $pricing) { $productQuantityDiscounts = $this->entity->getProductQuantityDiscounts(); $pricing->setProductQuantityDiscounts($productQuantityDiscounts); foreach ($productQuantityDiscounts as $productQuantityDiscount) { $this->entityDTO->productQuantityDiscounts[] = $this->dtoBuilderFactory->getProductQuantityDiscountDTOBuilder($productQuantityDiscount)->withPrice($pricing)->build(); } return $this; }
public function testGetTotalWithCartPriceRulesAndReducesTaxSubtotal() { $productShirt = new Product(); $productShirt->setSku('TS-NAVY-LG'); $productShirt->setName('Navy T-shirt (large)'); $productShirt->setUnitPrice(1200); $productShirt->setIsTaxable(true); $productPoster = new Product(); $productPoster->setSku('PST-CKN'); $productPoster->setName('Citizen Kane (1941) Poster'); $productPoster->setUnitPrice(500); $productPoster->setIsTaxable(true); $cartPriceRule = new CartPriceRule(); $cartPriceRule->setName('Buy a Shirt get a FREE poster'); $cartPriceRule->addItem(new CartPriceRuleProductItem($productShirt, 1)); $cartPriceRule->addItem(new CartPriceRuleProductItem($productPoster, 1)); $cartPriceRule->addDiscount(new CartPriceRuleDiscount($productPoster, 1)); $cartPriceRule->setReducesTaxSubtotal(true); $pricing = new Pricing(); $pricing->setCartPriceRules([$cartPriceRule]); $cartItem1 = new CartItem(); $cartItem1->setProduct($productShirt); $cartItem1->setQuantity(1); $cartItem2 = new CartItem(); $cartItem2->setProduct($productPoster); $cartItem2->setQuantity(1); $taxRate = new TaxRate(); $taxRate->setZip5(92606); $taxRate->setRate(8.0); $taxRate->setApplyToShipping(false); $cart = new Cart(); $cart->addCartItem($cartItem1); $cart->addCartItem($cartItem2); $cart->setTaxRate($taxRate); $expectedCartTotal = new CartTotal(); $expectedCartTotal->origSubtotal = 1700; $expectedCartTotal->subtotal = 1700; $expectedCartTotal->taxSubtotal = 1200; $expectedCartTotal->shipping = 0; $expectedCartTotal->discount = 500; $expectedCartTotal->tax = 96; $expectedCartTotal->total = 1296; $expectedCartTotal->savings = 500; $expectedCartTotal->addCartPriceRule($cartPriceRule); $expectedCartTotal->taxRate = $taxRate; $cartCalculator = new CartCalculator($pricing); $this->assertEquals($expectedCartTotal, $cartCalculator->getTotal($cart)); }