Exemple #1
0
 public function calculateDiscount(Coupon $coupon, Cart $cart)
 {
     $products = $coupon->getProducts();
     $config = $coupon->getConfig();
     $found = false;
     $eligibleItems = [];
     foreach ($cart as $item) {
         if (!$this->isApplicable($item, $products)) {
             continue;
         }
         if (array_key_exists($item->getProductId(), $products)) {
             $found = true;
             $hash = $this->getHash($item);
             $eligibleItems[$hash] = 0;
         }
     }
     if ($found) {
         foreach ($cart as &$item) {
             $hash2 = $this->getHash($item);
             if (array_key_exists($hash2, $eligibleItems)) {
                 $eligibleItems[$hash2]++;
                 if ($eligibleItems[$hash2] % 2 == 0) {
                     $item->setDiscount($item->getTerm()->getTotalPrice());
                 }
             }
         }
     }
 }
Exemple #2
0
 public function calculateDiscount(Coupon $coupon, Cart $cart)
 {
     $config = $coupon->getConfig();
     $over = $config['over'];
     $percent = $config['percent'];
     $total = $cart->totalExcludingTax();
     if ($total > $over) {
         $discount = $total - $total * (1 - $percent / 100);
         $cart->setDiscount($discount);
     }
 }
 public function calculateDiscount(Coupon $coupon, Cart $cart)
 {
     $products = $coupon->getProducts();
     $config = $coupon->getConfig();
     $percent = $config['percent'];
     foreach ($cart as &$item) {
         if (!$this->isApplicable($item, $products)) {
             continue;
         }
         if (empty($products) || array_key_exists($item->getProductId(), $products)) {
             $discount = $item->getPrice() - $item->getPrice() * (1 - $percent / 100);
             $item->setDiscount($discount);
         }
     }
 }
Exemple #4
0
 public function calculateDiscount(Coupon $coupon, Cart $cart)
 {
     $products = $coupon->getProducts();
     $config = $coupon->getConfig();
     $price = $config['price'];
     foreach ($cart as &$item) {
         if (!$this->isApplicable($item, $products)) {
             continue;
         }
         $period = $item->getTerm()->getPeriod();
         $priceForPeriod = $price * $period;
         if ($priceForPeriod < $item->getPrice()) {
             $item->setDiscount($item->getPrice() - $priceForPeriod);
         }
     }
 }
 public function testGetCoupon()
 {
     $collection = new CouponCollection();
     $coupon = new Coupon();
     $coupon->setCode('BLACK');
     $collection->addCoupon($coupon);
     $coupon = new Coupon();
     $coupon->setCode('CYBER');
     $collection->addCoupon($coupon);
     $coupon = new Coupon();
     $coupon->setCode('HANUKA');
     $collection->addCoupon($coupon);
     $this->assertInstanceOf('\\Cart\\Coupon\\Coupon', $collection->getCoupon('BLACK'));
     $this->assertInstanceOf('\\Cart\\Coupon\\Coupon', $collection->getCoupon('CYBER'));
     $this->assertInstanceOf('\\Cart\\Coupon\\Coupon', $collection->getCoupon('HANUKA'));
 }
Exemple #6
0
 /**
  * @dataProvider testApplicableProvider
  * @group coupon
  */
 public function testApplicable($period, $products, $applied)
 {
     $term = new Term($period);
     $term->setPrice(12.0);
     $product = new \Cart\Catalog\Product();
     $product->setId(24);
     $product->getBilling()->addTerm($term);
     $catalog = new Catalog();
     $item = $catalog->getCartItem($product);
     $cart = $this->getCart();
     $cart->add($item);
     $total = $cart->total();
     $coupon = new Coupon('COUPON');
     $coupon->setProducts($products);
     $coupon->setType('PercentDiscount');
     $coupon->setConfig(['percent' => 50]);
     $coupon->calculateDiscount($cart);
     $this->assertEquals($total != $cart->total(), $applied);
 }
Exemple #7
0
 public function calculateDiscount(Coupon $coupon, Cart $cart)
 {
     $products = $coupon->getProducts();
     $config = $coupon->getConfig();
     $addons = $config['addon'];
     $found = false;
     foreach ($cart as $item) {
         if (!$this->isApplicable($item, $products)) {
             continue;
         }
         if ($found == false && array_key_exists($item->getProductId(), $products)) {
             $found = true;
             break;
         }
     }
     if ($found) {
         foreach ($cart as &$item) {
             if (array_key_exists($item->getProductId(), $addons) && (empty($addons[$item->getProductId()]) || in_array($item->getTerm()->getPeriod(), $addons[$item->getProductId()]))) {
                 $item->setDiscount($item->getPrice());
                 unset($addons[$item->getProductId()]);
             }
         }
     }
 }
Exemple #8
0
 public function calculateDiscount(Coupon $coupon, Cart $cart)
 {
     $products = $coupon->getProducts();
     $config = $coupon->getConfig();
     $addon = $config['addon'];
     $found = false;
     foreach ($cart as $item) {
         if (!$this->isApplicable($item, $products)) {
             continue;
         }
         if ($found == false && array_key_exists($item->getProductId(), $products)) {
             $found = $item->getId();
             break;
         }
     }
     if ($found) {
         foreach ($cart as &$item) {
             if ($item->getId() != $found && in_array($item->getProductId(), $addon)) {
                 $item->setDiscount($item->getPrice() / 2);
                 break;
             }
         }
     }
 }
Exemple #9
0
 public function import(array $array)
 {
     foreach ($array as $p) {
         $coupon = new Coupon();
         $coupon->setCode($p['code']);
         $coupon->setType($p['type']);
         if (isset($p['products']) && !empty($p['products'])) {
             $coupon->setProducts($p['products']);
         }
         if (isset($p['config']) && !empty($p['config'])) {
             $coupon->setConfig($p['config']);
         }
         if (isset($p['valid_from']) && !empty($p['valid_from'])) {
             $coupon->setValidFrom($p['valid_from']);
         }
         if (isset($p['valid_until']) && !empty($p['valid_until'])) {
             $coupon->setValidUntil($p['valid_until']);
         }
         $this->addCoupon($coupon);
     }
 }
Exemple #10
0
 /**
  * @param Coupon $coupon
  */
 public function setCoupon(Coupon $coupon)
 {
     $this->coupon = $coupon;
     $coupon->calculateDiscount($this->cart);
 }