public function aroundDelete(\Magento\Catalog\Model\ResourceModel\Category $categoryResource, \Closure $proceed, \Magento\Framework\Model\AbstractModel $category)
 {
     $categoryResource->addCommitCallback(function () use($category) {
         if (!$this->indexer->isScheduled()) {
             Category::$affectedProductIds = (array) $category->getAffectedProductIds();
             $this->indexer->reindexRow($category->getId());
         }
     });
     return $proceed($category);
 }
 public function testToOptionArray()
 {
     $expect = [['label' => __('-- Please Select a Category --'), 'value' => ''], ['label' => 'name', 'value' => 3]];
     $this->categoryCollection->expects($this->once())->method('addAttributeToSelect')->with($this->equalTo('name'))->will($this->returnValue($this->categoryCollection));
     $this->categoryCollection->expects($this->once())->method('addRootLevelFilter')->will($this->returnValue($this->categoryCollection));
     $this->categoryCollection->expects($this->once())->method('load');
     $this->categoryCollection->expects($this->any())->method('getIterator')->will($this->returnValue(new \ArrayIterator([$this->category])));
     $this->category->expects($this->once())->method('getName')->will($this->returnValue('name'));
     $this->category->expects($this->once())->method('getId')->will($this->returnValue(3));
     $this->assertEquals($expect, $this->model->toOptionArray());
 }
 /**
  * Resource model save function plugin.
  * Append a commit callback to save the product positions.
  *
  * @param \Magento\Catalog\Model\ResourceModel\Category $categoryResource Category original resource model.
  * @param \Closure                                      $proceed          Original save method.
  * @param \Magento\Framework\Model\AbstractModel        $category         Saved category.
  *
  * @return \Magento\Catalog\Model\ResourceModel\Category
  */
 public function aroundSave(\Magento\Catalog\Model\ResourceModel\Category $categoryResource, \Closure $proceed, \Magento\Framework\Model\AbstractModel $category)
 {
     if ($category->getId() && $category->getSortedProducts()) {
         $this->unserializeProductPositions($category);
         if ($category->getIsVirtualCategory()) {
             $category->setPostedProducts([]);
         }
         $categoryResource->addCommitCallback(function () use($category) {
             $affectedProductIds = $this->getAffectedProductIds($category);
             $category->setAffectedProductIds($affectedProductIds);
             $this->saveHandler->saveProductPositions($category);
         });
     }
     return $proceed($category);
 }
Example #4
0
 /**
  * Add attribute to filter
  *
  * @param int $storeId
  * @param string $attributeCode
  * @param mixed $value
  * @param string $type
  * @return \Magento\Framework\DB\Select|bool
  */
 protected function _addFilter($storeId, $attributeCode, $value, $type = '=')
 {
     if (!$this->_select instanceof \Magento\Framework\DB\Select) {
         return false;
     }
     if (!isset($this->_attributesCache[$attributeCode])) {
         $attribute = $this->_categoryResource->getAttribute($attributeCode);
         $this->_attributesCache[$attributeCode] = ['entity_type_id' => $attribute->getEntityTypeId(), 'attribute_id' => $attribute->getId(), 'table' => $attribute->getBackend()->getTable(), 'is_global' => $attribute->getIsGlobal(), 'backend_type' => $attribute->getBackendType()];
     }
     $attribute = $this->_attributesCache[$attributeCode];
     switch ($type) {
         case '=':
             $conditionRule = '=?';
             break;
         case 'in':
             $conditionRule = ' IN(?)';
             break;
         default:
             return false;
             break;
     }
     if ($attribute['backend_type'] == 'static') {
         $this->_select->where('e.' . $attributeCode . $conditionRule, $value);
     } else {
         $this->_select->join(['t1_' . $attributeCode => $attribute['table']], 'e.entity_id = t1_' . $attributeCode . '.entity_id AND t1_' . $attributeCode . '.store_id = 0', [])->where('t1_' . $attributeCode . '.attribute_id=?', $attribute['attribute_id']);
         if ($attribute['is_global']) {
             $this->_select->where('t1_' . $attributeCode . '.value' . $conditionRule, $value);
         } else {
             $ifCase = $this->getConnection()->getCheckSql('t2_' . $attributeCode . '.value_id > 0', 't2_' . $attributeCode . '.value', 't1_' . $attributeCode . '.value');
             $this->_select->joinLeft(['t2_' . $attributeCode => $attribute['table']], $this->getConnection()->quoteInto('t1_' . $attributeCode . '.entity_id = t2_' . $attributeCode . '.entity_id AND t1_' . $attributeCode . '.attribute_id = t2_' . $attributeCode . '.attribute_id AND t2_' . $attributeCode . '.store_id=?', $storeId), [])->where('(' . $ifCase . ')' . $conditionRule, $value);
         }
     }
     return $this->_select;
 }
Example #5
0
 /**
  * @param ResourceCategory $resourceCategory
  * @param \Closure $proceed
  * @param AbstractModel $category
  * @return ResourceCategory
  * @throws \Exception
  */
 private function addCommitCallback(ResourceCategory $resourceCategory, \Closure $proceed, AbstractModel $category)
 {
     try {
         $resourceCategory->beginTransaction();
         $result = $proceed($category);
         $resourceCategory->addCommitCallback(function () use($category) {
             $affectedProducts = $category->getAffectedProductIds();
             if (is_array($affectedProducts)) {
                 $this->reindexList($affectedProducts);
             }
         });
         $resourceCategory->commit();
     } catch (\Exception $e) {
         $resourceCategory->rollBack();
         throw $e;
     }
     return $result;
 }
Example #6
0
 /**
  * {@inheritdoc}
  */
 public function delete($object)
 {
     $pluginInfo = $this->pluginList->getNext($this->subjectType, 'delete');
     if (!$pluginInfo) {
         return parent::delete($object);
     } else {
         return $this->___callPlugins('delete', func_get_args(), $pluginInfo);
     }
 }
 public function testChildrenUrlPathAttributeNoUpdatingIfParentUrlPathIsNotChanged()
 {
     $this->categoryUrlPathGenerator->expects($this->any())->method('getUrlKey')->willReturn('url_key');
     $this->categoryUrlPathGenerator->expects($this->any())->method('getUrlPath')->willReturn('url_path');
     $this->categoryResource->expects($this->once())->method('saveAttribute')->with($this->category, 'url_path');
     $this->category->expects($this->any())->method('getUrlKey')->willReturn('not_formatted_url_key');
     $this->category->expects($this->any())->method('setUrlKey')->willReturnSelf();
     $this->category->expects($this->any())->method('setUrlPath')->willReturnSelf();
     $this->category->expects($this->once())->method('isObjectNew')->willReturn(false);
     // break code execution
     $this->category->expects($this->once())->method('dataHasChangedFor')->with('url_path')->willReturn(false);
     $this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
 }
Example #8
0
 /**
  * Process product data before save
  *
  * @param \Magento\Framework\DataObject $object
  * @return $this
  */
 protected function _beforeSave(\Magento\Framework\DataObject $object)
 {
     /**
      * Check if declared category ids in object data.
      */
     if ($object->hasCategoryIds()) {
         $categoryIds = $this->_catalogCategory->verifyIds($object->getCategoryIds());
         $object->setCategoryIds($categoryIds);
     }
     $self = parent::_beforeSave($object);
     /**
      * Try detect product id by sku if id is not declared
      */
     if (!$object->getId() && $object->getSku()) {
         $object->setId($this->getIdBySku($object->getSku()));
     }
     return $self;
 }
 /**
  * Validate category process
  *
  * @param  Category $category
  * @return void
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 protected function validateCategory(Category $category)
 {
     $useConfigFields = [];
     foreach ($this->useConfigFields as $field) {
         if (!$category->getData($field)) {
             $useConfigFields[] = $field;
         }
     }
     $category->setData('use_post_data_config', $useConfigFields);
     $validate = $category->validate();
     if ($validate !== true) {
         foreach ($validate as $code => $error) {
             if ($error === true) {
                 $attribute = $this->categoryResource->getAttribute($code)->getFrontend()->getLabel();
                 throw new \Magento\Framework\Exception\LocalizedException(__('Attribute "%1" is required.', $attribute));
             } else {
                 throw new \Magento\Framework\Exception\LocalizedException(__($error));
             }
         }
     }
     $category->unsetData('use_post_data_config');
 }
Example #10
0
 /**
  * Prepare chooser element HTML
  *
  * @param AbstractElement $element Form Element
  * @return AbstractElement
  */
 public function prepareElementHtml(AbstractElement $element)
 {
     $uniqId = $this->mathRandom->getUniqueHash($element->getId());
     $sourceUrl = $this->getUrl('catalog/product_widget/chooser', ['uniq_id' => $uniqId, 'use_massaction' => false]);
     $chooser = $this->getLayout()->createBlock('Magento\\Widget\\Block\\Adminhtml\\Widget\\Chooser')->setElement($element)->setConfig($this->getConfig())->setFieldsetId($this->getFieldsetId())->setSourceUrl($sourceUrl)->setUniqId($uniqId);
     if ($element->getValue()) {
         $value = explode('/', $element->getValue());
         $productId = false;
         if (isset($value[0]) && isset($value[1]) && $value[0] == 'product') {
             $productId = $value[1];
         }
         $categoryId = isset($value[2]) ? $value[2] : false;
         $label = '';
         if ($categoryId) {
             $label = $this->_resourceCategory->getAttributeRawValue($categoryId, 'name', $this->_storeManager->getStore()) . '/';
         }
         if ($productId) {
             $label .= $this->_resourceProduct->getAttributeRawValue($productId, 'name', $this->_storeManager->getStore());
         }
         $chooser->setLabel($label);
     }
     $element->setData('after_element_html', $chooser->toHtml());
     return $element;
 }
Example #11
0
 /**
  * {@inheritdoc}
  */
 public function getValidationRulesBeforeSave()
 {
     $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getValidationRulesBeforeSave');
     if (!$pluginInfo) {
         return parent::getValidationRulesBeforeSave();
     } else {
         return $this->___callPlugins('getValidationRulesBeforeSave', func_get_args(), $pluginInfo);
     }
 }
 /**
  * Executing parents move method and cleaning cache after it
  *
  * @param mixed $category
  * @param mixed $newParent
  * @param mixed $prevNode
  * @return void
  */
 public function move($category, $newParent, $prevNode = null)
 {
     $this->_catalogCategory->move($category->getId(), $newParent->getId());
     parent::move($category, $newParent, $prevNode);
     $this->_afterMove();
 }
Example #13
0
 public function testAfterSaveScheduled()
 {
     $this->categoryResourceMock->expects($this->once())->method('addCommitCallback');
     $this->assertEquals($this->categoryResourceMock, $this->model->aroundSave($this->categoryResourceMock, $this->proceed, $this->categoryMock));
 }