/** * Retrieve attributes data * * @return array * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function getAttributes() { if (!$this->hasData('attributes')) { $attributes = (array) $this->_configurableType->getConfigurableAttributesAsArray($this->getProduct()); $productData = (array) $this->getRequest()->getParam('product'); if (isset($productData['configurable_attributes_data'])) { $configurableData = $productData['configurable_attributes_data']; foreach ($attributes as $key => &$attribute) { if (isset($configurableData[$key])) { $attribute['values'] = array_merge(isset($attribute['values']) ? $attribute['values'] : array(), isset($configurableData[$key]['values']) ? array_filter($configurableData[$key]['values']) : array()); } } } foreach ($attributes as &$attribute) { if (isset($attribute['values']) && is_array($attribute['values'])) { foreach ($attribute['values'] as &$attributeValue) { if (!$this->getCanReadPrice()) { $attributeValue['pricing_value'] = ''; $attributeValue['is_percent'] = 0; } $attributeValue['can_edit_price'] = $this->getCanEditPrice(); $attributeValue['can_read_price'] = $this->getCanReadPrice(); } } } $this->setData('attributes', $attributes); } return $this->getData('attributes'); }
/** * Select one of the options and "prepare for cart" with a proper buy request */ protected function _prepareForCart() { $attributes = $this->_model->getConfigurableAttributesAsArray($this->_product); $attribute = reset($attributes); $optionValueId = $attribute['values'][0]['value_index']; $buyRequest = new \Magento\Framework\Object(['qty' => 5, 'super_attribute' => [$attribute['attribute_id'] => $optionValueId]]); $this->_model->prepareForCart($buyRequest, $this->_product); }
/** * Select one of the options and "prepare for cart" with a proper buy request * * @return ProductInterface */ protected function _prepareForCart() { $product = $this->productRepository->getById(1, true); $attributes = $this->model->getConfigurableAttributesAsArray($product); $attribute = reset($attributes); $optionValueId = $attribute['values'][0]['value_index']; $buyRequest = new \Magento\Framework\DataObject(['qty' => 5, 'super_attribute' => [$attribute['attribute_id'] => $optionValueId]]); $this->model->prepareForCart($buyRequest, $product); return $product; }
/** * Retrieve attributes data * * @return array */ public function getAttributes() { if (!$this->hasData('attributes')) { $attributes = (array) $this->_configurableType->getConfigurableAttributesAsArray($this->getProduct()); $productData = (array) $this->getRequest()->getParam('product'); if (isset($productData['configurable_attributes_data'])) { $configurableData = $productData['configurable_attributes_data']; foreach ($attributes as $key => &$attribute) { if (isset($configurableData[$key])) { $attribute['values'] = array_merge(isset($attribute['values']) ? $attribute['values'] : array(), isset($configurableData[$key]['values']) ? array_filter($configurableData[$key]['values']) : array()); } } } $this->setData('attributes', $attributes); } return $this->getData('attributes'); }
/** * @param int $productStore * @param int $attributeStore * * @dataProvider getConfigurableAttributesAsArrayDataProvider */ public function testGetConfigurableAttributesAsArray($productStore, $attributeStore) { $attributeSource = $this->getMockForAbstractClass('Magento\\Eav\\Model\\Entity\\Attribute\\Source\\AbstractSource', [], '', false, true, true, ['getAllOptions']); $attributeSource->expects($this->any())->method('getAllOptions')->will($this->returnValue([])); $attributeFrontend = $this->getMockForAbstractClass('Magento\\Eav\\Model\\Entity\\Attribute\\Frontend\\AbstractFrontend', [], '', false, true, true, ['getLabel']); $attributeFrontend->expects($this->any())->method('getLabel')->will($this->returnValue('Label')); $eavAttribute = $this->getMock('Magento\\Catalog\\Model\\Resource\\Eav\\Attribute', ['getFrontend', 'getSource', 'getStoreLabel', '__wakeup', 'setStoreId', '__sleep'], [], '', false); $eavAttribute->expects($this->any())->method('getFrontend')->will($this->returnValue($attributeFrontend)); $eavAttribute->expects($this->any())->method('getSource')->will($this->returnValue($attributeSource)); $eavAttribute->expects($this->any())->method('getStoreLabel')->will($this->returnValue('Store Label')); $eavAttribute->expects($this->any())->method('setStoreId')->with($attributeStore); $attribute = $this->getMockBuilder('\\Magento\\ConfigurableProduct\\Model\\Product\\Type\\Configurable\\Attribute')->disableOriginalConstructor()->setMethods(['getProductAttribute', '__wakeup', '__sleep'])->getMock(); $attribute->expects($this->any())->method('getProductAttribute')->will($this->returnValue($eavAttribute)); $product = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Product')->setMethods(['getStoreId', 'getData', 'hasData', '__wakeup', '__sleep'])->disableOriginalConstructor()->getMock(); $product->expects($this->any())->method('getStoreId')->will($this->returnValue($productStore)); $product->expects($this->any())->method('hasData')->will($this->returnValueMap([['_cache_instance_configurable_attributes', 1]])); $product->expects($this->any())->method('getData')->will($this->returnValueMap([['_cache_instance_configurable_attributes', null, [$attribute]]])); $result = $this->_model->getConfigurableAttributesAsArray($product); $this->assertCount(1, $result); }
/** * Retrieve attributes data * * @return array */ protected function getAttributes() { return (array) $this->configurableType->getConfigurableAttributesAsArray($this->locator->getProduct()); }