/** * {@inheritdoc} */ public function getProducts(ProductInterface $product) { if (!isset($this->products[$product->getId()])) { if ($this->requestSafety->isSafeMethod()) { $productIds = $this->resource->getConnection()->fetchCol('(' . implode(') UNION (', $this->linkedProductSelectBuilder->build($product->getId())) . ')'); $this->products[$product->getId()] = $this->collectionFactory->create()->addAttributeToSelect(['price', 'special_price'])->addIdFilter($productIds); } else { $this->products[$product->getId()] = $this->configurable->getUsedProducts($product); } } return $this->products[$product->getId()]; }
/** * @param \Magento\Framework\Pricing\SaleableInterface|\Magento\Catalog\Model\Product $product * @return float * @throws \Magento\Framework\Exception\LocalizedException */ public function resolvePrice(\Magento\Framework\Pricing\SaleableInterface $product) { $price = null; foreach ($this->configurable->getUsedProducts($product) as $subProduct) { $productPrice = $this->priceResolver->resolvePrice($subProduct); $price = $price ? min($price, $productPrice) : $productPrice; } if (!$price) { throw new \Magento\Framework\Exception\LocalizedException(__('Configurable product "%1" do not have sub-products', $product->getName())); } return (double) $price; }
/** * @param \Magento\Framework\Pricing\SaleableInterface $product * @return float */ public function resolvePrice(\Magento\Framework\Pricing\SaleableInterface $product) { $selectedConfigurableOption = $product->getSelectedConfigurableOption(); if ($selectedConfigurableOption) { $price = $this->priceResolver->resolvePrice($selectedConfigurableOption); } else { $price = null; foreach ($this->configurable->getUsedProducts($product) as $subProduct) { $productPrice = $this->priceResolver->resolvePrice($subProduct); $price = $price ? min($price, $productPrice) : $productPrice; } } $priceInCurrentCurrency = $this->priceCurrency->convertAndRound($price); return $priceInCurrentCurrency ? (double) $priceInCurrentCurrency : false; }
public function testGetUsedProducts() { $products = $this->_model->getUsedProducts($this->_product); $this->assertInternalType('array', $products); $this->assertTrue(2 === count($products)); foreach ($products as $product) { $this->assertInstanceOf('Magento\\Catalog\\Model\\Product', $product); } }
/** * Retrieve Links in JSON format * * @return string */ public function getLinksJson() { $products = $this->_configurableType->getUsedProducts($this->getProduct()); if (!$products) { return '{}'; } $data = array(); foreach ($products as $product) { $data[$product->getId()] = $this->getConfigurableSettings($product); } return $this->_jsonEncoder->encode($data); }
/** * Retrieve actual list of associated products (i.e. if product contains variations matrix form data * - previously saved in database relations are not considered) * * @return Product[] */ protected function _getAssociatedProducts() { $product = $this->getProduct(); $ids = $this->getProduct()->getAssociatedProductIds(); if ($ids === null) { // form data overrides any relations stored in database return $this->_configurableType->getUsedProducts($product); } $products = []; foreach ($ids as $productId) { try { $products[] = $this->productRepository->getById($productId); } catch (NoSuchEntityException $e) { continue; } } return $products; }
/** * Retrieve actual list of associated products (i.e. if product contains variations matrix form data * - previously saved in database relations are not considered) * * @return Product[] */ protected function _getAssociatedProducts() { $product = $this->getProduct(); $ids = $this->getProduct()->getAssociatedProductIds(); if ($ids === null) { // form data overrides any relations stored in database return $this->_configurableType->getUsedProducts($product); } $products = array(); foreach ($ids as $productId) { /** @var $product Product */ $product = $this->_productFactory->create()->load($productId); if ($product->getId()) { $products[] = $product; } } return $products; }
public function testGetUsedProducts() { $attributeCollection = $this->getMockBuilder('\\Magento\\ConfigurableProduct\\Model\\Resource\\Product\\Type\\Configurable\\Attribute\\Collection')->setMethods(['setProductFilter', 'addFieldToFilter', 'walk'])->disableOriginalConstructor()->getMock(); $attributeCollection->expects($this->any())->method('setProductFilter')->will($this->returnSelf()); $this->_attributeCollectionFactory->expects($this->any())->method('create')->will($this->returnValue($attributeCollection)); $product = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Product')->setMethods(['dataHasChangedFor', 'getConfigurableAttributesData', 'getStoreId', 'getId', 'getData', 'hasData', 'getAssociatedProductIds', '__wakeup', '__sleep'])->disableOriginalConstructor()->getMock(); $product->expects($this->any())->method('getConfigurableAttributesData')->will($this->returnValue($this->attributeData)); $product->expects($this->any())->method('getStoreId')->will($this->returnValue(5)); $product->expects($this->any())->method('getId')->will($this->returnValue(1)); $product->expects($this->any())->method('getAssociatedProductIds')->will($this->returnValue([2])); $product->expects($this->any())->method('hasData')->will($this->returnValueMap([['_cache_instance_used_product_attribute_ids', 1], ['_cache_instance_products', 0], ['_cache_instance_configurable_attributes', 1]])); $product->expects($this->any())->method('getData')->will($this->returnValue(1)); $productCollection = $this->getMockBuilder('Magento\\ConfigurableProduct\\Model\\Resource\\Product\\Type\\Configurable\\Product\\Collection')->setMethods(['setFlag', 'setProductFilter', 'addStoreFilter', 'addAttributeToSelect', 'addFilterByRequiredOptions', 'setStoreId'])->disableOriginalConstructor()->getMock(); $productCollection->expects($this->any())->method('addAttributeToSelect')->will($this->returnSelf()); $productCollection->expects($this->any())->method('setProductFilter')->will($this->returnSelf()); $productCollection->expects($this->any())->method('setFlag')->will($this->returnSelf()); $productCollection->expects($this->any())->method('addFilterByRequiredOptions')->will($this->returnSelf()); $productCollection->expects($this->any())->method('setStoreId')->with(5)->will($this->returnValue([])); $this->_productCollectionFactory->expects($this->any())->method('create')->will($this->returnValue($productCollection)); $this->_model->getUsedProducts($product); }
/** * @return bool */ public function isHasVariations() { return $this->getProduct()->getTypeId() === Configurable::TYPE_CODE && $this->configurableType->getUsedProducts($this->getProduct()); }