示例#1
0
文件: Price.php 项目: nja78/magento2
 /**
  * Get product final price
  *
  * @param   float $qty
  * @param   \Magento\Catalog\Model\Product $product
  * @return  float
  */
 public function getFinalPrice($qty, $product)
 {
     if ($qty === null && $product->getCalculatedFinalPrice() !== null) {
         return $product->getCalculatedFinalPrice();
     }
     if ($product->getCustomOption('simple_product')) {
         $product->setSelectedConfigurableOption($product->getCustomOption('simple_product')->getProduct());
     }
     //TODO: MAGETWO-23739 catalogrule price must get from simple product.
     $finalPrice = $product->getPriceInfo()->getPrice('final_price')->getAmount()->getValue();
     $finalPrice = $this->_applyOptionsPrice($product, $qty, $finalPrice);
     $finalPrice = max(0, $finalPrice);
     $product->setFinalPrice($finalPrice);
     return $finalPrice;
 }
示例#2
0
 /**
  * Get product final price
  *
  * @param   float $qty
  * @param   \Magento\Catalog\Model\Product $product
  * @return  float
  */
 public function getFinalPrice($qty, $product)
 {
     if ($qty === null && $product->getCalculatedFinalPrice() !== null) {
         return $product->getCalculatedFinalPrice();
     }
     if ($product->getCustomOption('simple_product') && $product->getCustomOption('simple_product')->getProduct()) {
         $finalPrice = parent::getFinalPrice($qty, $product->getCustomOption('simple_product')->getProduct());
     } else {
         $priceInfo = $product->getPriceInfo();
         $finalPrice = $priceInfo->getPrice('final_price')->getAmount()->getValue();
     }
     $finalPrice = $this->_applyOptionsPrice($product, $qty, $finalPrice);
     $finalPrice = max(0, $finalPrice);
     $product->setFinalPrice($finalPrice);
     return $finalPrice;
 }
 /**
  * Get selection quantity
  *
  * @param \Magento\Catalog\Model\Product $product
  * @param int $selectionId
  * @return float
  */
 public function getSelectionQty(\Magento\Catalog\Model\Product $product, $selectionId)
 {
     $selectionQty = $product->getCustomOption('selection_qty_' . $selectionId);
     if ($selectionQty) {
         return $selectionQty->getValue();
     }
     return 0;
 }
示例#4
0
 /**
  * Get selection quantity
  *
  * @param \Magento\Catalog\Model\Product $product
  * @param int $selectionId
  *
  * @return float
  */
 public function getSelectionQty($product, $selectionId)
 {
     $selectionQty = $product->getCustomOption('selection_qty_' . $selectionId);
     if ($selectionQty) {
         return $selectionQty->getValue();
     }
     return 0;
 }
 /**
  * @covers \Magento\Catalog\Model\Product::addCustomOption
  * @covers \Magento\Catalog\Model\Product::setCustomOptions
  * @covers \Magento\Catalog\Model\Product::getCustomOptions
  * @covers \Magento\Catalog\Model\Product::getCustomOption
  * @covers \Magento\Catalog\Model\Product::hasCustomOptions
  */
 public function testCustomOptionsApi()
 {
     $this->assertEquals([], $this->_model->getCustomOptions());
     $this->assertFalse($this->_model->hasCustomOptions());
     $this->_model->setId(99);
     $this->_model->addCustomOption('one', 'value1');
     $option = $this->_model->getCustomOption('one');
     $this->assertInstanceOf('Magento\\Framework\\DataObject', $option);
     $this->assertEquals($this->_model->getId(), $option->getProductId());
     $this->assertSame($option->getProduct(), $this->_model);
     $this->assertEquals('one', $option->getCode());
     $this->assertEquals('value1', $option->getValue());
     $this->assertEquals(['one' => $option], $this->_model->getCustomOptions());
     $this->assertTrue($this->_model->hasCustomOptions());
     $this->_model->setCustomOptions(['test']);
     $this->assertTrue(is_array($this->_model->getCustomOptions()));
 }
示例#6
0
 /**
  * Get sku of product
  *
  * @param  \Magento\Catalog\Model\Product $product
  * @return string
  */
 public function getSku($product)
 {
     $simpleOption = $product->getCustomOption('simple_product');
     if ($simpleOption) {
         $optionProduct = $simpleOption->getProduct();
         $simpleSku = null;
         if ($optionProduct) {
             $simpleSku = $simpleOption->getProduct()->getSku();
         }
         $sku = parent::getOptionSku($product, $simpleSku);
     } else {
         $sku = parent::getSku($product);
     }
     return $sku;
 }
示例#7
0
 /**
  * Apply options price
  *
  * @param Product $product
  * @param int $qty
  * @param float $finalPrice
  * @return float
  * @deprecated (MAGETWO-31469)
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 protected function _applyOptionsPrice($product, $qty, $finalPrice)
 {
     $optionIds = $product->getCustomOption('option_ids');
     if ($optionIds) {
         $basePrice = $finalPrice;
         foreach (explode(',', $optionIds->getValue()) as $optionId) {
             if ($option = $product->getOptionById($optionId)) {
                 $confItemOption = $product->getCustomOption('option_' . $option->getId());
                 $group = $option->groupFactory($option->getType())->setOption($option)->setConfigurationItemOption($confItemOption);
                 $finalPrice += $group->getOptionPrice($confItemOption->getValue(), $basePrice);
             }
         }
     }
     return $finalPrice;
 }
示例#8
0
 /**
  * @param \Magento\Catalog\Model\Product $product
  * @param \Magento\Framework\DataObject $selection
  * @return float|int
  */
 protected function getBeforeQty($product, $selection)
 {
     $beforeQty = 0;
     $customOption = $product->getCustomOption('product_qty_' . $selection->getId());
     if ($customOption && $customOption->getProduct()->getId() == $selection->getId()) {
         $beforeQty = (double) $customOption->getValue();
         return $beforeQty;
     }
     return $beforeQty;
 }
示例#9
0
文件: Price.php 项目: opexsw/magento2
 /**
  * Get Total price  for Bundle items
  *
  * @param \Magento\Catalog\Model\Product $product
  * @param null|float $qty
  * @return float
  */
 public function getTotalBundleItemsPrice($product, $qty = null)
 {
     $price = 0.0;
     if ($product->hasCustomOptions()) {
         $customOption = $product->getCustomOption('bundle_selection_ids');
         if ($customOption) {
             $selectionIds = unserialize($customOption->getValue());
             $selections = $product->getTypeInstance()->getSelectionsByIds($selectionIds, $product);
             $selections->addTierPriceData();
             $this->_eventManager->dispatch('prepare_catalog_product_collection_prices', ['collection' => $selections, 'store_id' => $product->getStoreId()]);
             foreach ($selections->getItems() as $selection) {
                 if ($selection->isSalable()) {
                     $selectionQty = $product->getCustomOption('selection_qty_' . $selection->getSelectionId());
                     if ($selectionQty) {
                         $price += $this->getSelectionFinalTotalPrice($product, $selection, $qty, $selectionQty->getValue());
                     }
                 }
             }
         }
     }
     return $price;
 }
示例#10
0
 /**
  * Retrieve array of bundle selection IDs
  *
  * @param \Magento\Catalog\Model\Product $product
  * @return array
  */
 protected function getBundleSelectionIds(\Magento\Catalog\Model\Product $product)
 {
     $customOption = $product->getCustomOption('bundle_selection_ids');
     if ($customOption) {
         $selectionIds = unserialize($customOption->getValue());
         if (!empty($selectionIds) && is_array($selectionIds)) {
             return $selectionIds;
         }
     }
     return [];
 }
示例#11
0
 /**
  * Default action to get sku of product with option
  *
  * @param \Magento\Catalog\Model\Product $product Product with Custom Options
  * @param string $sku Product SKU without option
  * @return string
  */
 public function getOptionSku($product, $sku = '')
 {
     $skuDelimiter = '-';
     if (empty($sku)) {
         $sku = $product->getData('sku');
     }
     $optionIds = $product->getCustomOption('option_ids');
     if ($optionIds) {
         foreach (explode(',', $optionIds->getValue()) as $optionId) {
             $option = $product->getOptionById($optionId);
             if ($option) {
                 $confItemOption = $product->getCustomOption(self::OPTION_PREFIX . $optionId);
                 $group = $option->groupFactory($option->getType())->setOption($option)->setListener(new \Magento\Framework\DataObject());
                 $optionSku = $group->getOptionSku($confItemOption->getValue(), $skuDelimiter);
                 if ($optionSku) {
                     $sku .= $skuDelimiter . $optionSku;
                 }
                 if ($group->getListener()->getHasError()) {
                     $product->setHasError(true)->setMessage($group->getListener()->getMessage());
                 }
             }
         }
     }
     return $sku;
 }
示例#12
0
 /**
  * Check is virtual product
  *
  * @param \Magento\Catalog\Model\Product $product
  * @return bool
  */
 public function isVirtual($product)
 {
     if ($product->hasCustomOptions()) {
         $customOption = $product->getCustomOption('bundle_selection_ids');
         $selectionIds = unserialize($customOption->getValue());
         $selections = $this->getSelectionsByIds($selectionIds, $product);
         $virtualCount = 0;
         foreach ($selections->getItems() as $selection) {
             if ($selection->isVirtual()) {
                 $virtualCount++;
             }
         }
         if ($virtualCount == count($selections)) {
             return true;
         }
     }
     return false;
 }
示例#13
0
 /**
  * {@inheritdoc}
  */
 public function getCustomOption($code)
 {
     $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getCustomOption');
     if (!$pluginInfo) {
         return parent::getCustomOption($code);
     } else {
         return $this->___callPlugins('getCustomOption', func_get_args(), $pluginInfo);
     }
 }
示例#14
0
 /**
  * Import product recurring payment information
  * Returns false if it cannot be imported
  *
  * @param \Magento\Catalog\Model\Product $product
  * @return $this|false
  */
 public function importProduct(\Magento\Catalog\Model\Product $product)
 {
     if ($product->getIsRecurring() && is_array($product->getRecurringPayment())) {
         // import recurring payment data
         $this->addData($product->getRecurringPayment());
         // automatically set product name if there is no schedule description
         if (!$this->hasScheduleDescription()) {
             $this->setScheduleDescription($product->getName());
         }
         // collect start datetime from the product options
         $options = $product->getCustomOption(self::PRODUCT_OPTIONS_KEY);
         if ($options) {
             $options = unserialize($options->getValue());
             if (is_array($options)) {
                 if (isset($options['start_datetime'])) {
                     $startDatetime = new \Magento\Framework\Stdlib\DateTime\Date($options['start_datetime'], \Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT);
                     $this->setNearestStartDatetime($startDatetime);
                 }
             }
         }
         return $this->_filterValues();
     }
     return false;
 }