コード例 #1
0
 /**
  * @inheritdoc
  */
 public function getList($products, Struct\ProductContextInterface $context)
 {
     $group = $context->getCurrentCustomerGroup();
     $specify = $this->graduatedPricesGateway->getList($products, $group);
     //iterates the passed prices and products and assign the product unit to the prices and the passed customer group
     $prices = $this->buildPrices($products, $specify, $group);
     //check if one of the products have no assigned price within the prices variable.
     $fallbackProducts = array_filter($products, function (Struct\ListProduct $product) use($prices) {
         return !array_key_exists($product->getNumber(), $prices);
     });
     if (!empty($fallbackProducts)) {
         //if some product has no price, we have to load the fallback customer group prices for the fallbackProducts.
         $fallbackPrices = $this->graduatedPricesGateway->getList($fallbackProducts, $context->getFallbackCustomerGroup());
         $fallbackPrices = $this->buildPrices($fallbackProducts, $fallbackPrices, $context->getFallbackCustomerGroup());
         $prices = $prices + $fallbackPrices;
     }
     $priceGroups = $context->getPriceGroups();
     /**
      * If one of the products has a configured price group,
      * the graduated prices has to be build over the defined price group graduations.
      *
      * The price group discounts are defined with a percentage discount, which calculated
      * on the first graduated price of the product.
      */
     foreach ($products as $product) {
         if (!$product->isPriceGroupActive() || !$product->getPriceGroup()) {
             continue;
         }
         if (!isset($prices[$product->getNumber()])) {
             continue;
         }
         $priceGroupId = $product->getPriceGroup()->getId();
         if (!isset($priceGroups[$priceGroupId])) {
             continue;
         }
         $priceGroup = $priceGroups[$priceGroupId];
         $firstGraduation = array_shift($prices[$product->getNumber()]);
         $prices[$product->getNumber()] = $this->buildDiscountGraduations($firstGraduation, $context->getCurrentCustomerGroup(), $priceGroup->getDiscounts());
     }
     return $prices;
 }
コード例 #2
0
 /**
  * Returns the highest price group discount for the provided product.
  *
  * The price groups are stored in the provided context object.
  * If the product has no configured price group or the price group has no discount defined for the
  * current customer group, the function returns null.
  *
  * @param Struct\ListProduct $product
  * @param Struct\ProductContextInterface $context
  * @param $quantity
  * @return null|Struct\Product\PriceDiscount
  */
 private function getHighestQuantityDiscount(Struct\ListProduct $product, Struct\ProductContextInterface $context, $quantity)
 {
     $priceGroups = $context->getPriceGroups();
     if (empty($priceGroups)) {
         return null;
     }
     $id = $product->getPriceGroup()->getId();
     if (!isset($priceGroups[$id])) {
         return null;
     }
     $priceGroup = $priceGroups[$id];
     /**@var $highest Struct\Product\PriceDiscount*/
     $highest = null;
     foreach ($priceGroup->getDiscounts() as $discount) {
         if ($discount->getQuantity() > $quantity) {
             continue;
         }
         if (!$highest) {
             $highest = $discount;
             continue;
         }
         if ($highest->getPercent() < $discount->getPercent()) {
             $highest = $discount;
         }
     }
     return $highest;
 }