示例#1
0
 /**
  * Get price with including/excluding tax
  *
  * @param float $price
  * @param bool $includingTax
  * @return float
  */
 public function getPrice($price, $includingTax = null)
 {
     if (!is_null($includingTax)) {
         $price = $this->_taxData->getPrice($this->getProduct(), $price, true);
     } else {
         $price = $this->_taxData->getPrice($this->getProduct(), $price);
     }
     return $price;
 }
示例#2
0
 /**
  * Get price configuration
  *
  * @param \Magento\Catalog\Model\Product\Option\Value|\Magento\Catalog\Model\Product\Option $option
  * @return array
  */
 protected function _getPriceConfiguration($option)
 {
     $data = array();
     $data['price'] = $this->_coreData->currency($option->getPrice(true), false, false);
     $data['oldPrice'] = $this->_coreData->currency($option->getPrice(false), false, false);
     $data['priceValue'] = $option->getPrice(false);
     $data['type'] = $option->getPriceType();
     $data['exclTaxPrice'] = $price = $this->_taxData->getPrice($option->getProduct(), $data['price'], false);
     $data['inclTaxPrice'] = $price = $this->_taxData->getPrice($option->getProduct(), $data['price'], true);
     return $data;
 }
示例#3
0
 /**
  * @param \Magento\Framework\Object $input
  * @param float $expectOutputPrice
  * @param string[] $configs
  * @param string $productClassName
  *
  * @magentoDataFixture Magento/Catalog/_files/products.php
  * @magentoDataFixture Magento/Customer/_files/customer.php
  * @magentoDataFixture Magento/Customer/_files/customer_address.php
  * @magentoDbIsolation enabled
  * @dataProvider getPriceDataProvider
  */
 public function testGetPrice($input, $expectOutputPrice, $configs = [], $productClassName = 'DefaultProductClass')
 {
     $this->setUpDefaultRules();
     $fixtureProductId = 1;
     /** @var \Magento\Catalog\Model\Product $product */
     $product = $this->objectManager->create('Magento\\Catalog\\Model\\Product')->load($fixtureProductId);
     $product->setTaxClassId($this->taxClasses[$productClassName]);
     $shippingAddress = $this->getCustomerAddress();
     $billingAddress = $shippingAddress;
     foreach ($configs as $config) {
         $this->scopeConfig->setValue($config['path'], $config['value'], ScopeInterface::SCOPE_STORE, 'default');
     }
     $price = $this->helper->getPrice($product, $input->getPrice(), $input->getIncludingTax(), $shippingAddress, $billingAddress, $this->taxClasses['DefaultCustomerClass'], $input->getStore(), $input->getPriceIncludesTax(), $input->getRoundPrice());
     $this->assertEquals($expectOutputPrice, $price);
 }
示例#4
0
 /**
  * Calculate final price
  *
  * @param Product $product
  * @param \Magento\Store\Model\Store $store
  * @param bool $inclTax
  * @param bool $isSalePriceAllowed
  * @return float|null
  */
 private function _getFinalPrice($product, $store, $inclTax, $isSalePriceAllowed)
 {
     // calculate sale_price attribute value
     $salePriceAttribute = $this->getGroupAttributeSalePrice();
     $salePriceMapValue = null;
     $finalPrice = null;
     if (!is_null($salePriceAttribute)) {
         $salePriceMapValue = $salePriceAttribute->getProductAttributeValue($product);
     }
     if (!is_null($salePriceMapValue) && floatval($salePriceMapValue) > 0.0001) {
         $finalPrice = $salePriceMapValue;
     } else {
         if ($isSalePriceAllowed) {
             $finalPrice = $this->catalogPrice->getCatalogPrice($product, $store, $inclTax);
         }
     }
     if ($product->getTypeId() != Product\Type::TYPE_BUNDLE) {
         $finalPrice = $this->_taxData->getPrice($product, $finalPrice, $inclTax, null, null, null, $product->getStoreId());
     }
     return $finalPrice;
 }
示例#5
0
 /**
  * Process price emails
  *
  * @param \Magento\ProductAlert\Model\Email $email
  * @return $this
  */
 protected function _processPrice(\Magento\ProductAlert\Model\Email $email)
 {
     $email->setType('price');
     foreach ($this->_getWebsites() as $website) {
         /* @var $website \Magento\Store\Model\Website */
         if (!$website->getDefaultGroup() || !$website->getDefaultGroup()->getDefaultStore()) {
             continue;
         }
         if (!$this->_scopeConfig->getValue(self::XML_PATH_PRICE_ALLOW, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $website->getDefaultGroup()->getDefaultStore()->getId())) {
             continue;
         }
         try {
             $collection = $this->_priceColFactory->create()->addWebsiteFilter($website->getId())->setCustomerOrder();
         } catch (\Exception $e) {
             $this->_errors[] = $e->getMessage();
             return $this;
         }
         $previousCustomer = null;
         $email->setWebsite($website);
         foreach ($collection as $alert) {
             try {
                 if (!$previousCustomer || $previousCustomer->getId() != $alert->getCustomerId()) {
                     $customer = $this->_customerAccountService->getCustomer($alert->getCustomerId());
                     if ($previousCustomer) {
                         $email->send();
                     }
                     if (!$customer) {
                         continue;
                     }
                     $previousCustomer = $customer;
                     $email->clean();
                     $email->setCustomerData($customer);
                 } else {
                     $customer = $previousCustomer;
                 }
                 /** @var \Magento\Catalog\Model\Product $product */
                 $product = $this->_productFactory->create()->setStoreId($website->getDefaultStore()->getId())->load($alert->getProductId());
                 if (!$product) {
                     continue;
                 }
                 $product->setCustomerGroupId($customer->getGroupId());
                 if ($alert->getPrice() > $product->getFinalPrice()) {
                     $productPrice = $product->getFinalPrice();
                     $product->setFinalPrice($this->_taxData->getPrice($product, $productPrice));
                     $product->setPrice($this->_taxData->getPrice($product, $product->getPrice()));
                     $email->addPriceProduct($product);
                     $alert->setPrice($productPrice);
                     $alert->setLastSendDate($this->_dateFactory->create()->gmtDate());
                     $alert->setSendCount($alert->getSendCount() + 1);
                     $alert->setStatus(1);
                     $alert->save();
                 }
             } catch (\Exception $e) {
                 $this->_errors[] = $e->getMessage();
             }
         }
         if ($previousCustomer) {
             try {
                 $email->send();
             } catch (\Exception $e) {
                 $this->_errors[] = $e->getMessage();
             }
         }
     }
     return $this;
 }
示例#6
0
 /**
  * Retrieve Price considering tier price
  *
  * @param  \Magento\Catalog\Model\Product $product
  * @param  string|null                $which
  * @param  bool|null                  $includeTax
  * @param  bool                       $takeTierPrice
  * @return float|array
  */
 public function getTotalPrices($product, $which = null, $includeTax = null, $takeTierPrice = true)
 {
     // check calculated price index
     if ($product->getData('min_price') && $product->getData('max_price')) {
         $minimalPrice = $this->_taxData->getPrice($product, $product->getData('min_price'), $includeTax);
         $maximalPrice = $this->_taxData->getPrice($product, $product->getData('max_price'), $includeTax);
         $this->_isPricesCalculatedByIndex = true;
     } else {
         /**
          * Check if product price is fixed
          */
         $finalPrice = $product->getFinalPrice();
         if ($product->getPriceType() == self::PRICE_TYPE_FIXED) {
             $minimalPrice = $maximalPrice = $this->_taxData->getPrice($product, $finalPrice, $includeTax);
         } else {
             // PRICE_TYPE_DYNAMIC
             $minimalPrice = $maximalPrice = 0;
         }
         $options = $this->getOptions($product);
         $minPriceFounded = false;
         if ($options) {
             foreach ($options as $option) {
                 /* @var $option \Magento\Bundle\Model\Option */
                 $selections = $option->getSelections();
                 if ($selections) {
                     $selectionMinimalPrices = array();
                     $selectionMaximalPrices = array();
                     foreach ($option->getSelections() as $selection) {
                         /* @var $selection \Magento\Bundle\Model\Selection */
                         if (!$selection->isSalable()) {
                             /**
                              * @todo CatalogInventory Show out of stock Products
                              */
                             continue;
                         }
                         $qty = $selection->getSelectionQty();
                         $item = $product->getPriceType() == self::PRICE_TYPE_FIXED ? $product : $selection;
                         $selectionMinimalPrices[] = $this->_taxData->getPrice($item, $this->getSelectionFinalTotalPrice($product, $selection, 1, $qty, true, $takeTierPrice), $includeTax);
                         $selectionMaximalPrices[] = $this->_taxData->getPrice($item, $this->getSelectionFinalTotalPrice($product, $selection, 1, null, true, $takeTierPrice), $includeTax);
                     }
                     if (count($selectionMinimalPrices)) {
                         $selMinPrice = min($selectionMinimalPrices);
                         if ($option->getRequired()) {
                             $minimalPrice += $selMinPrice;
                             $minPriceFounded = true;
                         } elseif (true !== $minPriceFounded) {
                             $selMinPrice += $minimalPrice;
                             $minPriceFounded = false === $minPriceFounded ? $selMinPrice : min($minPriceFounded, $selMinPrice);
                         }
                         if ($option->isMultiSelection()) {
                             $maximalPrice += array_sum($selectionMaximalPrices);
                         } else {
                             $maximalPrice += max($selectionMaximalPrices);
                         }
                     }
                 }
             }
         }
         // condition is TRUE when all product options are NOT required
         if (!is_bool($minPriceFounded)) {
             $minimalPrice = $minPriceFounded;
         }
         $customOptions = $product->getOptions();
         if ($product->getPriceType() == self::PRICE_TYPE_FIXED && $customOptions) {
             foreach ($customOptions as $customOption) {
                 /* @var $customOption \Magento\Catalog\Model\Product\Option */
                 $values = $customOption->getValues();
                 if ($values) {
                     $prices = array();
                     foreach ($values as $value) {
                         /* @var $value \Magento\Catalog\Model\Product\Option\Value */
                         $valuePrice = $value->getPrice(true);
                         $prices[] = $valuePrice;
                     }
                     if (count($prices)) {
                         if ($customOption->getIsRequire()) {
                             $minimalPrice += $this->_taxData->getPrice($product, min($prices), $includeTax);
                         }
                         $multiTypes = array(\Magento\Catalog\Model\Product\Option::OPTION_TYPE_CHECKBOX, \Magento\Catalog\Model\Product\Option::OPTION_TYPE_MULTIPLE);
                         if (in_array($customOption->getType(), $multiTypes)) {
                             $maximalValue = array_sum($prices);
                         } else {
                             $maximalValue = max($prices);
                         }
                         $maximalPrice += $this->_taxData->getPrice($product, $maximalValue, $includeTax);
                     }
                 } else {
                     $valuePrice = $customOption->getPrice(true);
                     if ($customOption->getIsRequire()) {
                         $minimalPrice += $this->_taxData->getPrice($product, $valuePrice, $includeTax);
                     }
                     $maximalPrice += $this->_taxData->getPrice($product, $valuePrice, $includeTax);
                 }
             }
         }
         $this->_isPricesCalculatedByIndex = false;
     }
     if ($which == 'max') {
         return $maximalPrice;
     } elseif ($which == 'min') {
         return $minimalPrice;
     }
     return array($minimalPrice, $maximalPrice);
 }
示例#7
0
 /**
  * Return item base original price
  *
  * @param AbstractItem $item
  * @return float
  */
 public function getItemBaseOriginalPrice($item)
 {
     return $this->_taxData->getPrice($item, $item->getBaseOriginalPrice(), true);
 }