/** * Returns product final price depending on options chosen * * @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(); } $finalPrice = parent::getFinalPrice($qty, $product); if ($product->hasCustomOptions()) { /* @var $typeInstance \Magento\GroupedProduct\Model\Product\Type\Grouped */ $typeInstance = $product->getTypeInstance(); $associatedProducts = $typeInstance->setStoreFilter($product->getStore(), $product)->getAssociatedProducts($product); foreach ($associatedProducts as $childProduct) { /* @var $childProduct \Magento\Catalog\Model\Product */ $option = $product->getCustomOption('associated_product_' . $childProduct->getId()); if (!$option) { continue; } $childQty = $option->getValue(); if (!$childQty) { continue; } $finalPrice += $childProduct->getFinalPrice($childQty) * $childQty; } } $product->setFinalPrice($finalPrice); return max(0, $product->getData('final_price')); }
/** * 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; }
/** * testGetTierPrices * testSetTierPrices * * @dataProvider pricesDataProvider */ public function testTierPrices($priceScope, $expectedWebsiteId) { // establish the behavior of the mocks $this->scopeConfigMock->expects($this->any())->method('getValue')->will($this->returnValue($priceScope)); $this->websiteMock->expects($this->any())->method('getId')->will($this->returnValue($expectedWebsiteId)); $this->tpFactory->expects($this->any())->method('create')->will($this->returnCallback(function () { return $this->objectManagerHelper->getObject('Magento\\Catalog\\Model\\Product\\TierPrice'); })); // create sample TierPrice objects that would be coming from a REST call $tp1 = $this->objectManagerHelper->getObject('Magento\\Catalog\\Model\\Product\\TierPrice'); $tp1->setValue(10); $tp1->setCustomerGroupId(1); $tp1->setQty(11); $tp2 = $this->objectManagerHelper->getObject('Magento\\Catalog\\Model\\Product\\TierPrice'); $tp2->setValue(20); $tp2->setCustomerGroupId(2); $tp2->setQty(22); $tps = [$tp1, $tp2]; // force the product to have null tier prices $this->product->setData($this::KEY_TIER_PRICE, null); $this->assertNull($this->product->getData($this::KEY_TIER_PRICE)); // set the product with the TierPrice objects $this->model->setTierPrices($this->product, $tps); // test the data actually set on the product $tpArray = $this->product->getData($this::KEY_TIER_PRICE); $this->assertNotNull($tpArray); $this->assertTrue(is_array($tpArray)); $this->assertEquals(sizeof($tps), sizeof($tpArray)); for ($i = 0; $i < sizeof($tps); $i++) { $tpData = $tpArray[$i]; $this->assertEquals($expectedWebsiteId, $tpData['website_id'], 'Website Id does not match'); $this->assertEquals($tps[$i]->getValue(), $tpData['price'], 'Price/Value does not match'); $this->assertEquals($tps[$i]->getValue(), $tpData['website_price'], 'WebsitePrice/Value does not match'); $this->assertEquals($tps[$i]->getCustomerGroupId(), $tpData['cust_group'], 'Customer group Id does not match'); $this->assertEquals($tps[$i]->getQty(), $tpData['price_qty'], 'Qty does not match'); } // test with the data retrieved as a REST object $tpRests = $this->model->getTierPrices($this->product); $this->assertNotNull($tpRests); $this->assertTrue(is_array($tpRests)); $this->assertEquals(sizeof($tps), sizeof($tpRests)); for ($i = 0; $i < sizeof($tps); $i++) { $this->assertEquals($tps[$i]->getValue(), $tpRests[$i]->getValue(), 'REST: Price/Value does not match'); $this->assertEquals($tps[$i]->getCustomerGroupId(), $tpRests[$i]->getCustomerGroupId(), 'REST: Customer group Id does not match'); $this->assertEquals($tps[$i]->getQty(), $tpRests[$i]->getQty(), 'REST: Qty does not match'); } }
/** * Retrieve product final price * * @param integer $qty * @param \Magento\Catalog\Model\Product $product * @return float */ public function getFinalPrice($qty, $product) { if ($qty === null && $product->getCalculatedFinalPrice() !== null) { return $product->getCalculatedFinalPrice(); } $finalPrice = parent::getFinalPrice($qty, $product); /** * links prices are added to base product price only if they can be purchased separately */ if ($product->getLinksPurchasedSeparately()) { if ($linksIds = $product->getCustomOption('downloadable_link_ids')) { $linkPrice = 0; $links = $product->getTypeInstance()->getLinks($product); foreach (explode(',', $linksIds->getValue()) as $linkId) { if (isset($links[$linkId])) { $linkPrice += $links[$linkId]->getPrice(); } } $finalPrice += $linkPrice; } } $product->setData('final_price', $finalPrice); return max(0, $product->getData('final_price')); }
/** * Whether group price value fixed or percent of original price * * @param \Magento\Catalog\Model\Product\Type\Price $priceObject * @return bool */ protected function _isPriceFixed($priceObject) { return $priceObject->isGroupPriceFixed(); }
/** * @param \Magento\CatalogRule\Model\Resource\RuleFactory $ruleFactory * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Framework\Event\ManagerInterface $eventManager * @param PriceCurrencyInterface $priceCurrency * @param GroupManagementInterface $groupManagement * @param \Magento\Catalog\Api\Data\ProductGroupPriceInterfaceFactory $groupPriceFactory * @param \Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory $tierPriceFactory * @param \Magento\Framework\App\Config\ScopeConfigInterface $config * @param \Magento\Catalog\Helper\Data $catalogData * * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct(\Magento\CatalogRule\Model\Resource\RuleFactory $ruleFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Magento\Customer\Model\Session $customerSession, \Magento\Framework\Event\ManagerInterface $eventManager, PriceCurrencyInterface $priceCurrency, GroupManagementInterface $groupManagement, \Magento\Catalog\Api\Data\ProductGroupPriceInterfaceFactory $groupPriceFactory, \Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory $tierPriceFactory, \Magento\Framework\App\Config\ScopeConfigInterface $config, \Magento\Catalog\Helper\Data $catalogData) { $this->_catalogData = $catalogData; parent::__construct($ruleFactory, $storeManager, $localeDate, $customerSession, $eventManager, $priceCurrency, $groupManagement, $groupPriceFactory, $tierPriceFactory, $config); }
/** * Construct * * @param \Magento\CatalogRule\Model\Resource\RuleFactory $ruleFactory * @param \Magento\Framework\StoreManagerInterface $storeManager * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Framework\Event\ManagerInterface $eventManager * @param \Magento\Catalog\Helper\Data $catalogData */ public function __construct(\Magento\CatalogRule\Model\Resource\RuleFactory $ruleFactory, \Magento\Framework\StoreManagerInterface $storeManager, \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Magento\Customer\Model\Session $customerSession, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Catalog\Helper\Data $catalogData) { $this->_catalogData = $catalogData; parent::__construct($ruleFactory, $storeManager, $localeDate, $customerSession, $eventManager); }
public function testIsTierPriceFixed() { $this->assertTrue($this->_model->isTierPriceFixed()); }
/** * @param \Magento\CatalogRule\Model\Resource\RuleFactory $ruleFactory * @param \Magento\Framework\StoreManagerInterface $storeManager * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Framework\Event\ManagerInterface $eventManager * @param PriceCurrencyInterface $priceCurrency * @param PriceModifierInterface $priceModifier */ public function __construct(\Magento\CatalogRule\Model\Resource\RuleFactory $ruleFactory, \Magento\Framework\StoreManagerInterface $storeManager, \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Magento\Customer\Model\Session $customerSession, \Magento\Framework\Event\ManagerInterface $eventManager, PriceCurrencyInterface $priceCurrency, PriceModifierInterface $priceModifier) { $this->priceModifier = $priceModifier; parent::__construct($ruleFactory, $storeManager, $localeDate, $customerSession, $eventManager, $priceCurrency); }