/** * @param \Magento\Catalog\Model\Product $product * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute * @return array */ protected function getMediaEntriesDataCollection(\Magento\Catalog\Model\Product $product, \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute) { $attributeCode = $attribute->getAttributeCode(); $mediaData = $product->getData($attributeCode); if (!empty($mediaData['images']) && is_array($mediaData['images'])) { return $mediaData['images']; } return []; }
/** * Retrieve Select for update Flat data * * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute * @param int $store * @param bool $hasValueField flag which require option value * @return \Magento\Framework\DB\Select */ public function getFlatUpdateSelect(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute, $store, $hasValueField = true) { $adapter = $this->_getReadAdapter(); $attributeTable = $attribute->getBackend()->getTable(); $attributeCode = $attribute->getAttributeCode(); $joinConditionTemplate = "%s.entity_id = %s.entity_id" . " AND %s.entity_type_id = " . $attribute->getEntityTypeId() . " AND %s.attribute_id = " . $attribute->getId() . " AND %s.store_id = %d"; $joinCondition = sprintf($joinConditionTemplate, 'e', 't1', 't1', 't1', 't1', \Magento\Store\Model\Store::DEFAULT_STORE_ID); if ($attribute->getFlatAddChildData()) { $joinCondition .= ' AND e.child_id = t1.entity_id'; } $valueExpr = $adapter->getCheckSql('t2.value_id > 0', 't2.value', 't1.value'); /** @var $select \Magento\Framework\DB\Select */ $select = $adapter->select()->joinLeft(['t1' => $attributeTable], $joinCondition, [])->joinLeft(['t2' => $attributeTable], sprintf($joinConditionTemplate, 't1', 't2', 't2', 't2', 't2', $store), [$attributeCode => $valueExpr]); if ($attribute->getFrontend()->getInputType() != 'multiselect' && $hasValueField) { $valueIdExpr = $adapter->getCheckSql('to2.value_id > 0', 'to2.value', 'to1.value'); $select->joinLeft(['to1' => $this->getTable('eav_attribute_option_value')], "to1.option_id = {$valueExpr} AND to1.store_id = 0", [])->joinLeft(['to2' => $this->getTable('eav_attribute_option_value')], $adapter->quoteInto("to2.option_id = {$valueExpr} AND to2.store_id = ?", $store), [$attributeCode . '_value' => $valueIdExpr]); } if ($attribute->getFlatAddChildData()) { $select->where('e.is_child = 0'); } return $select; }
/** * Returns attributes all values in label-value or value-value pairs form. Labels are lower-cased. * * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute * @return array */ public function getAttributeOptions(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute) { $options = []; if ($attribute->usesSource()) { // should attribute has index (option value) instead of a label? $index = in_array($attribute->getAttributeCode(), $this->_indexValueAttributes) ? 'value' : 'label'; // only default (admin) store values used $attribute->setStoreId(\Magento\Store\Model\Store::DEFAULT_STORE_ID); try { foreach ($attribute->getSource()->getAllOptions(false) as $option) { foreach (is_array($option['value']) ? $option['value'] : [$option] as $innerOption) { if (strlen($innerOption['value'])) { // skip ' -- Please Select -- ' option $options[$innerOption['value']] = $innerOption[$index]; } } } } catch (\Exception $e) { // ignore exceptions connected with source models } } return $options; }
/** * Return if attribute exists in original data array. * * @param AbstractAttribute $attribute * @param mixed $v New value of the attribute. Can be used in subclasses. * @param array $origData * @return bool */ protected function _canUpdateAttribute(AbstractAttribute $attribute, $v, array &$origData) { return array_key_exists($attribute->getAttributeCode(), $origData); }
/** * @param Product $product * @param AbstractAttribute $attribute * @return array */ protected function getMediaEntriesDataCollection(Product $product, AbstractAttribute $attribute) { $attributeCode = $attribute->getAttributeCode(); $mediaData = $product->getData($attributeCode); if (!empty($mediaData['images']) && is_array($mediaData['images'])) { return $mediaData['images']; } return []; }
/** * Retrieve Select For Flat Attribute update * * @param AbstractAttribute $attribute * @param int $storeId * @return Select */ public function getFlatUpdateSelect(AbstractAttribute $attribute, $storeId) { $connection = $this->getConnection(); $joinConditionTemplate = "%s.entity_id=%s.entity_id" . " AND %s.entity_type_id = " . $attribute->getEntityTypeId() . " AND %s.attribute_id = " . $attribute->getId() . " AND %s.store_id = %d"; $joinCondition = sprintf($joinConditionTemplate, 'e', 't1', 't1', 't1', 't1', \Magento\Store\Model\Store::DEFAULT_STORE_ID); if ($attribute->getFlatAddChildData()) { $joinCondition .= ' AND e.child_id = t1.entity_id'; } $valueExpr = $connection->getCheckSql('t2.value_id > 0', 't2.value', 't1.value'); /** @var $select Select */ $select = $connection->select()->joinLeft(['t1' => $attribute->getBackend()->getTable()], $joinCondition, [])->joinLeft(['t2' => $attribute->getBackend()->getTable()], sprintf($joinConditionTemplate, 't1', 't2', 't2', 't2', 't2', $storeId), [$attribute->getAttributeCode() => $valueExpr]); if ($attribute->getFlatAddChildData()) { $select->where("e.is_child = ?", 0); } return $select; }
/** * Return increment needed for SKU uniqueness * * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute * @param Product $object * @return int */ protected function _getLastSimilarAttributeValueIncrement($attribute, $object) { $connection = $this->getAttribute()->getEntity()->getConnection(); $select = $connection->select(); $value = $object->getData($attribute->getAttributeCode()); $bind = ['attribute_code' => trim($value) . '-%']; $select->from($this->getTable(), $attribute->getAttributeCode())->where($attribute->getAttributeCode() . ' LIKE :attribute_code')->order(['entity_id DESC', $attribute->getAttributeCode() . ' ASC'])->limit(1); $data = $connection->fetchOne($select, $bind); return abs((int) str_replace($value, '', $data)); }
/** * Check whether the attribute is a real field in entity table * Rewrited for EAV Collection * * @param integer|string|\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute * @return bool */ public function isAttributeStatic($attribute) { $attributeCode = null; if ($attribute instanceof \Magento\Eav\Model\Entity\Attribute\AttributeInterface) { $attributeCode = $attribute->getAttributeCode(); } elseif (is_string($attribute)) { $attributeCode = $attribute; } elseif (is_numeric($attribute)) { $attributeCode = $this->getAttribute($attribute)->getAttributeCode(); } if ($attributeCode) { $columns = $this->getAllTableColumns(); if (in_array($attributeCode, $columns)) { return true; } } return false; }
/** * Build date element html string for attribute * * @param AbstractAttribute $attribute * @param string $part * @return string */ public function getDateInput($attribute, $part = 'from') { $name = $attribute->getAttributeCode() . '[' . $part . ']'; $value = $this->getAttributeValue($attribute, $part); return $this->_getDateBlock()->setName($name)->setId($attribute->getAttributeCode() . ($part == 'from' ? '' : '_' . $part))->setTitle($this->getAttributeLabel($attribute))->setValue($value)->setImage($this->getViewFileUrl('Magento_Theme::calendar.png'))->setDateFormat($this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT))->setClass('input-text')->getHtml(); }
/** * Check if attribute allowed * * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute * @return bool */ protected function _isAllowedAttribute($attribute) { return !in_array($attribute->getFrontendInput(), $this->_ignoredAttributeTypes) && !in_array($attribute->getAttributeCode(), $this->_ignoredAttributeCodes) && $attribute->getFrontendLabel() != ""; }
/** * Get options for all product attribute values from used products * * @param \Magento\Catalog\Model\Product[] $usedProducts * @param AbstractAttribute $productAttribute * @return array */ protected function getIncludedOptions(array $usedProducts, AbstractAttribute $productAttribute) { $attributeValues = []; foreach ($usedProducts as $associatedProduct) { $attributeValues[] = $associatedProduct->getData($productAttribute->getAttributeCode()); } $options = $productAttribute->getSource()->getSpecificOptions(array_unique($attributeValues)); return $options; }
/** * @param \Magento\Catalog\Model\Product $product * @param AbstractAttribute $attribute * @return $this */ protected function duplicate($product, $attribute) { $mediaGalleryData = $product->getData($attribute->getAttributeCode()); if (!isset($mediaGalleryData['images']) || !is_array($mediaGalleryData['images'])) { return $this; } $this->getResource()->duplicate($attribute->getId(), isset($mediaGalleryData['duplicate']) ? $mediaGalleryData['duplicate'] : [], $product->getOriginalId(), $product->getId()); return $this; }
/** * Build date element html string for attribute * * @param AbstractAttribute $attribute * @param string $part * @return string */ public function getDateInput($attribute, $part = 'from') { $name = $attribute->getAttributeCode() . '[' . $part . ']'; $value = $this->getAttributeValue($attribute, $part); return $this->_getDateBlock()->setName($name)->setId($attribute->getAttributeCode() . ($part == 'from' ? '' : '_' . $part))->setTitle($this->getAttributeLabel($attribute))->setValue($value)->setImage($this->getViewFileUrl('Magento_Core::calendar.gif'))->setDateFormat($this->_localeDate->getDateFormat(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::FORMAT_TYPE_SHORT))->setClass('input-text')->getHtml(); }
/** * Return increment needed for SKU uniqueness * * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute * @param Product $object * @return int */ protected function _getLastSimilarAttributeValueIncrement($attribute, $object) { $adapter = $this->getAttribute()->getEntity()->getReadConnection(); $select = $adapter->select(); $value = $object->getData($attribute->getAttributeCode()); $bind = array('entity_type_id' => $attribute->getEntityTypeId(), 'attribute_code' => trim($value) . '-%'); $select->from($this->getTable(), $attribute->getAttributeCode())->where('entity_type_id = :entity_type_id')->where($attribute->getAttributeCode() . ' LIKE :attribute_code')->order(array('entity_id DESC', $attribute->getAttributeCode() . ' ASC'))->limit(1); $data = $adapter->fetchOne($select, $bind); return abs((int) str_replace($value, '', $data)); }