/** * Load prices for configurable attribute * * @param ConfigurableAttribute $object * @return $this */ protected function loadPrices(ConfigurableAttribute $object) { $websiteId = $this->_catalogData->isPriceGlobal() ? 0 : (int) $this->_storeManager->getStore()->getWebsiteId(); $select = $this->_getReadAdapter()->select()->from($this->_priceTable)->where('product_super_attribute_id = ?', $object->getId())->where('website_id = ?', $websiteId); foreach ($select->query() as $row) { $data = ['value_index' => $row['value_index'], 'is_percent' => $row['is_percent'], 'pricing_value' => $row['pricing_value']]; $object->addPrice($data); } return $this; }
/** * Load label for configurable attribute * * @param ConfigurableAttribute $object * @return $this */ protected function loadLabel(ConfigurableAttribute $object) { $storeId = (int) $this->_storeManager->getStore()->getId(); $connection = $this->_getReadAdapter(); $useDefaultCheck = $connection->getCheckSql('store.use_default IS NULL', 'def.use_default', 'store.use_default'); $labelCheck = $connection->getCheckSql('store.value IS NULL', 'def.value', 'store.value'); $select = $connection->select()->from(['def' => $this->_labelTable])->joinLeft(['store' => $this->_labelTable], $connection->quoteInto('store.product_super_attribute_id = def.product_super_attribute_id AND store.store_id = ?', $storeId), ['use_default' => $useDefaultCheck, 'label' => $labelCheck])->where('def.product_super_attribute_id = ?', $object->getId())->where('def.store_id = ?', 0); $data = $connection->fetchRow($select); $object->setLabel($data['label']); $object->setUseDefault($data['use_default']); return $this; }
/** * Save Options prices (Depends from price save scope) * * @param \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $attribute * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function savePrices($attribute) { $write = $this->_getWriteAdapter(); // define website id scope if ($this->getCatalogHelper()->isPriceGlobal()) { $websiteId = 0; } else { $websiteId = (int) $this->_storeManager->getStore($attribute->getStoreId())->getWebsite()->getId(); } $values = $attribute->getValues(); if (!is_array($values)) { $values = array(); } $new = array(); $old = array(); // retrieve old values $select = $write->select()->from($this->_priceTable)->where('product_super_attribute_id = :product_super_attribute_id')->where('website_id = :website_id'); $bind = array('product_super_attribute_id' => (int) $attribute->getId(), 'website_id' => $websiteId); $rowSet = $write->fetchAll($select, $bind); foreach ($rowSet as $row) { $key = implode('-', array($row['website_id'], $row['value_index'])); if (!isset($old[$key])) { $old[$key] = $row; } else { // delete invalid (duplicate row) $where = $write->quoteInto('value_id = ?', $row['value_id']); $write->delete($this->_priceTable, $where); } } // prepare new values foreach ($values as $v) { if (empty($v['value_index'])) { continue; } $key = implode('-', array($websiteId, $v['value_index'])); $new[$key] = array('value_index' => $v['value_index'], 'pricing_value' => $v['pricing_value'], 'is_percent' => $v['is_percent'], 'website_id' => $websiteId, 'use_default' => !empty($v['use_default_value']) ? true : false); } $insert = array(); $update = array(); $delete = array(); foreach ($old as $k => $v) { if (!isset($new[$k])) { $delete[] = $v['value_id']; } } foreach ($new as $k => $v) { $needInsert = false; $needUpdate = false; $needDelete = false; $isGlobal = true; if (!$this->getCatalogHelper()->isPriceGlobal() && $websiteId != 0) { $isGlobal = false; } $hasValue = $isGlobal && !empty($v['pricing_value']) || !$isGlobal && !$v['use_default']; if (isset($old[$k])) { // data changed $dataChanged = $old[$k]['is_percent'] != $v['is_percent'] || $old[$k]['pricing_value'] != $v['pricing_value']; if (!$hasValue) { $needDelete = true; } else { if ($dataChanged) { $needUpdate = true; } } } else { if ($hasValue) { $needInsert = true; } } if (!$isGlobal && empty($v['pricing_value'])) { $v['pricing_value'] = 0; $v['is_percent'] = 0; } if ($needInsert) { $insert[] = array('product_super_attribute_id' => $attribute->getId(), 'value_index' => $v['value_index'], 'is_percent' => $v['is_percent'], 'pricing_value' => $v['pricing_value'], 'website_id' => $websiteId); } if ($needUpdate) { $update[$old[$k]['value_id']] = array('is_percent' => $v['is_percent'], 'pricing_value' => $v['pricing_value']); } if ($needDelete) { $delete[] = $old[$k]['value_id']; } } if (!empty($delete)) { $where = $write->quoteInto('value_id IN(?)', $delete); $write->delete($this->_priceTable, $where); } if (!empty($update)) { foreach ($update as $valueId => $bind) { $where = $write->quoteInto('value_id=?', $valueId); $write->update($this->_priceTable, $bind, $where); } } if (!empty($insert)) { $write->insertMultiple($this->_priceTable, $insert); } return $this; }
/** * @param Option $option * @param Attribute $configurableAttribute * @return Attribute */ public function getModelFromData(Option $option, Attribute $configurableAttribute) { /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $returnConfigurableAttribute */ $returnConfigurableAttribute = $this->attributeFactory->create(); $returnConfigurableAttribute->setData($configurableAttribute->getData()); $returnConfigurableAttribute->addData($option->__toArray()); $returnConfigurableAttribute->setId($configurableAttribute->getId()); $returnConfigurableAttribute->setAttributeId($configurableAttribute->getAttributeId()); $returnConfigurableAttribute->setValues($configurableAttribute->getPrices()); $values = $option->getValues(); if (!is_null($values)) { $prices = []; foreach ($values as $value) { $prices[] = $this->valueConverter->convertArrayFromData($value); } $returnConfigurableAttribute->setValues($prices); } return $returnConfigurableAttribute; }