/** * Set category ids to product data * * @param \Magento\Catalog\Model\Product $object * @return $this */ public function afterLoad($object) { $object->setData($this->getAttribute()->getAttributeCode(), $object->getCategoryIds()); return parent::afterLoad($object); }
/** * Unserialize after loading * * @param \Magento\Framework\DataObject $object * @return $this */ public function afterLoad($object) { parent::afterLoad($object); $this->_unserialize($object); return $this; }
/** * @param \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend $subject * @param \Closure $proceed * @param \Magento\Framework\DataObject $attribute * @return bool */ public function aroundValidate(\Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend $subject, \Closure $proceed, \Magento\Framework\DataObject $attribute) { $useDefault = $attribute->getUseDefault(); $attrCode = $subject->getAttribute()->getAttributeCode(); if ($useDefault && isset($useDefault[$attrCode])) { return true; } return $proceed($attribute); }
/** * Check whether attribute instance (attribute, backend, frontend or source) has method and applicable * * @param AbstractAttribute|\Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend|\Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend|\Magento\Eav\Model\Entity\Attribute\Source\AbstractSource $instance * @param string $method * @param array $args array of arguments * @return boolean */ protected function _isCallableAttributeInstance($instance, $method, $args) { if ($instance instanceof \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend && ($method == 'beforeSave' || $method == 'afterSave')) { $attributeCode = $instance->getAttribute()->getAttributeCode(); if (isset($args[0]) && $args[0] instanceof \Magento\Framework\DataObject && $args[0]->getData($attributeCode) === false) { return false; } } return parent::_isCallableAttributeInstance($instance, $method, $args); }
public function testGetAffectedFields() { $valueId = 10; $attributeId = 42; $attribute = $this->getMock('Magento\\Eav\\Model\\Entity\\Attribute\\AbstractAttribute', ['getBackendTable', 'isStatic', 'getAttributeId', '__wakeup'], [], '', false); $attribute->expects($this->any())->method('getAttributeId')->will($this->returnValue($attributeId)); $attribute->expects($this->any())->method('isStatic')->will($this->returnValue(false)); $attribute->expects($this->any())->method('getBackendTable')->will($this->returnValue('table')); $this->_model->setAttribute($attribute); $object = new \Magento\Framework\Object(); $this->_model->setValueId($valueId); $this->assertEquals(['table' => [['value_id' => $valueId, 'attribute_id' => $attributeId]]], $this->_model->getAffectedFields($object)); }
/** * @param \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend $subject * @param \Closure $proceed * @param \Magento\Framework\DataObject $entity * @return bool */ public function aroundValidate(\Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend $subject, \Closure $proceed, \Magento\Framework\DataObject $entity) { $isAllowedType = !empty(array_filter(array_map(function ($allowedEntity) use($entity) { return $entity instanceof $allowedEntity; }, $this->allowedEntityTypes))); if ($isAllowedType && $this->storeManager->getStore()->getId() !== Store::DEFAULT_STORE_ID) { $attrCode = $subject->getAttribute()->getAttributeCode(); // Null is meaning "no value" which should be overridden by value from default scope if (array_key_exists($attrCode, $entity->getData()) && $entity->getData($attrCode) === null) { return true; } } return $proceed($entity); }
protected function setUp() { $this->objectManagerHelper = new ObjectManagerHelper($this); $this->collection = $this->getMock('\\Magento\\Catalog\\Model\\Resource\\Product\\Collection', ['__wakeup', 'getSelect', 'joinLeft', 'order', 'getStoreId', 'getConnection', 'getCheckSql'], [], '', false); $this->attributeModel = $this->getMock('\\Magento\\Catalog\\Model\\Entity\\Attributee', ['__wakeup', 'getAttributeCode', 'getBackend', 'getId', 'isScopeGlobal'], [], '', false); $this->backendAttributeModel = $this->getMock('\\Magento\\Catalog\\Model\\Product\\Attribute\\Backend\\Sku', ['__wakeup', 'getTable'], [], '', false); $this->status = $this->objectManagerHelper->getObject('Magento\\Catalog\\Model\\Product\\Attribute\\Source\\Status'); $this->attributeModel->expects($this->any())->method('getAttribute')->will($this->returnSelf()); $this->attributeModel->expects($this->any())->method('getAttributeCode')->will($this->returnValue('attribute_code')); $this->attributeModel->expects($this->any())->method('getId')->will($this->returnValue('1')); $this->attributeModel->expects($this->any())->method('getBackend')->will($this->returnValue($this->backendAttributeModel)); $this->collection->expects($this->any())->method('getSelect')->will($this->returnSelf()); $this->collection->expects($this->any())->method('joinLeft')->will($this->returnSelf()); $this->backendAttributeModel->expects($this->any())->method('getTable')->will($this->returnValue('table_name')); }
/** * Method is invoked after save * * @param \Magento\Framework\DataObject $object * @return \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend */ public function afterSave($object) { if ($object->getOrderItem()) { $object->getOrderItem()->save(); } return parent::beforeSave($object); }
/** * Performed before data is saved * * @param \Magento\Framework\Object $object * @return $this */ public function beforeSave($object) { if ($object->getShipment()) { $object->setParentId($object->getShipment()->getId()); } return parent::beforeSave($object); }
/** * Method is invoked before save * * @param \Magento\Framework\DataObject $object * @return \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend */ public function beforeSave($object) { if ($object->getCreditmemo()) { $object->setParentId($object->getCreditmemo()->getId()); } return parent::beforeSave($object); }
/** * Perform operation before save * * @param \Magento\Framework\DataObject $object * @return $this */ public function beforeSave($object) { if ($object->getOrder()) { $object->setParentId($object->getOrder()->getId()); } parent::beforeSave($object); return $this; }
/** * @param \Magento\Framework\Object $object * @return bool */ public function validate($object) { $password = $object->getPassword(); if ($password && $password == $object->getPasswordConfirm()) { return true; } return parent::validate($object); }
/** * Set store id to the attribute * * @param \Magento\Framework\DataObject $object * @return $this */ public function beforeSave($object) { if ($object->getAddress()) { $object->setParentId($object->getAddress()->getId())->setStoreId($object->getAddress()->getStoreId()); } parent::beforeSave($object); return $this; }
/** * Method is invoked before save * * @param \Magento\Framework\Object $object * @return $this */ public function beforeSave($object) { if ($object->getOrder()) { $object->setOrderId($object->getOrder()->getId()); $object->setBillingAddressId($object->getOrder()->getBillingAddress()->getId()); } return parent::beforeSave($object); }
/** * Implode data for validation * * @param \Magento\Catalog\Model\Product $object * @return bool */ public function validate($object) { $attributeCode = $this->getAttribute()->getAttributeCode(); $data = $object->getData($attributeCode); if (is_array($data)) { $object->setData($attributeCode, implode(',', array_filter($data))); } return parent::validate($object); }
/** * Prepare inventory data from custom attribute * * @param Product $object * @return void */ public function beforeSave($object) { $stockData = $object->getData($this->getAttribute()->getAttributeCode()); if (isset($stockData['qty']) && $stockData['qty'] === '') { $stockData['qty'] = null; } if ($object->getStockData() !== null || $stockData !== null) { $object->setStockData(array_replace((array) $object->getStockData(), (array) $stockData)); } $object->unsetData($this->getAttribute()->getAttributeCode()); parent::beforeSave($object); }
/** * @param \Magento\Framework\Logger $logger * @param \Magento\Catalog\Model\Url $catalogUrl */ public function __construct(\Magento\Framework\Logger $logger, \Magento\Catalog\Model\Url $catalogUrl) { $this->_catalogUrl = $catalogUrl; parent::__construct($logger); }
/** * Validate * * @param \Magento\Catalog\Model\Product $object * @throws \Magento\Framework\Model\Exception * @return bool */ public function validate($object) { $value = $object->getData($this->getAttribute()->getAttributeCode()); if (empty($value)) { return parent::validate($object); } if (!preg_match('/^\\d*(\\.|,)?\\d{0,4}$/i', $value) || $value < 0) { throw new \Magento\Framework\Model\Exception(__('Please enter a number 0 or greater in this field.')); } return true; }
/** * Make SKU unique before save * * @param Product $object * @return $this */ public function beforeSave($object) { $this->_generateUniqueSku($object); return parent::beforeSave($object); }
/** * @param \Magento\Framework\Logger $logger * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate */ public function __construct(\Magento\Framework\Logger $logger, \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate) { $this->_localeDate = $localeDate; parent::__construct($logger); }
/** * @param \Magento\Framework\Logger $logger * @param \Magento\Directory\Model\RegionFactory $regionFactory */ public function __construct(\Magento\Framework\Logger $logger, \Magento\Directory\Model\RegionFactory $regionFactory) { $this->_regionFactory = $regionFactory; parent::__construct($logger); }
/** * @param \Magento\Framework\Logger $logger * @param \Magento\Framework\App\Filesystem $filesystem * @param \Magento\Core\Model\File\UploaderFactory $fileUploaderFactory */ public function __construct(\Magento\Framework\Logger $logger, \Magento\Framework\App\Filesystem $filesystem, \Magento\Core\Model\File\UploaderFactory $fileUploaderFactory) { $this->_filesystem = $filesystem; $this->_fileUploaderFactory = $fileUploaderFactory; parent::__construct($logger); }
/** * Construct the custom layout update class * * @param \Magento\Framework\Logger $logger * @param ValidatorFactory $layoutUpdateValidatorFactory */ public function __construct(\Magento\Framework\Logger $logger, ValidatorFactory $layoutUpdateValidatorFactory) { $this->_layoutUpdateValidatorFactory = $layoutUpdateValidatorFactory; parent::__construct($logger); }
/** * @param \Magento\Framework\Logger $logger * @param \Magento\Framework\Stdlib\DateTime $dateTime */ public function __construct(\Magento\Framework\Logger $logger, \Magento\Framework\Stdlib\DateTime $dateTime) { $this->dateTime = $dateTime; parent::__construct($logger); }
/** * Construct * * @param \Magento\Framework\Logger $logger * @param \Magento\Catalog\Model\Resource\ProductFactory $productFactory * @param \Magento\Framework\Event\ManagerInterface $eventManager * @param \Magento\Core\Helper\File\Storage\Database $fileStorageDb * @param \Magento\Core\Helper\Data $coreData * @param \Magento\Catalog\Model\Product\Media\Config $mediaConfig * @param \Magento\Framework\App\Filesystem $filesystem * @param \Magento\Catalog\Model\Resource\Product\Attribute\Backend\Media $resourceProductAttribute */ public function __construct(\Magento\Framework\Logger $logger, \Magento\Catalog\Model\Resource\ProductFactory $productFactory, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Core\Helper\File\Storage\Database $fileStorageDb, \Magento\Core\Helper\Data $coreData, \Magento\Catalog\Model\Product\Media\Config $mediaConfig, \Magento\Framework\App\Filesystem $filesystem, \Magento\Catalog\Model\Resource\Product\Attribute\Backend\Media $resourceProductAttribute) { $this->_productFactory = $productFactory; $this->_eventManager = $eventManager; $this->_fileStorageDb = $fileStorageDb; $this->_coreData = $coreData; $this->_resourceModel = $resourceProductAttribute; $this->_mediaConfig = $mediaConfig; $this->_mediaDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem::MEDIA_DIR); $this->_mediaDirectory->create($this->_mediaConfig->getBaseMediaPath()); $this->_mediaDirectory->create($this->_mediaConfig->getBaseTmpMediaPath()); parent::__construct($logger); }
/** * @param \Magento\Framework\Logger $logger * @param \Magento\Store\Model\StoreManagerInterface $storeManager */ public function __construct(\Magento\Framework\Logger $logger, \Magento\Store\Model\StoreManagerInterface $storeManager) { $this->_storeManager = $storeManager; parent::__construct($logger); }
/** * Validate * * @param \Magento\Catalog\Model\Product $object * @throws \Magento\Framework\Exception\LocalizedException * @return bool */ public function validate($object) { $value = $object->getData($this->getAttribute()->getAttributeCode()); if (empty($value)) { return parent::validate($object); } if (!$this->isPositiveOrZero($value)) { throw new \Magento\Framework\Exception\LocalizedException(__('Please enter a number 0 or greater in this field.')); } return true; }
/** * Construct * * @param \Magento\Framework\Logger $logger * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig */ public function __construct(\Magento\Framework\Logger $logger, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig) { $this->_scopeConfig = $scopeConfig; parent::__construct($logger); }