コード例 #1
0
 /**
  * @param array $categoryPath
  * @param string $ruleType
  * @return array|null
  */
 protected function getConditionFromCategory($categoryPath, $ruleType = 'Rule')
 {
     $categoryId = null;
     $tree = $this->categoryReadService->getTree();
     foreach ($categoryPath as $categoryName) {
         $categoryId = null;
         foreach ($tree->getChildrenData() as $child) {
             if ($child->getName() == $categoryName) {
                 $tree = $child;
                 /** @var \Magento\Catalog\Api\Data\CategoryTreeInterface $child */
                 $categoryId = $child->getId();
                 break;
             }
         }
     }
     if (!$categoryId) {
         return null;
     }
     $types = ['Rule' => 'Magento\\TargetRule\\Model\\Rule\\Condition\\Product\\Attributes', 'Actions' => 'Magento\\TargetRule\\Model\\Actions\\Condition\\Product\\Attributes'];
     if (empty($types[$ruleType])) {
         return null;
     }
     return ['type' => $types[$ruleType], 'attribute' => 'category_ids', 'operator' => '==', 'value' => $categoryId];
 }
コード例 #2
0
 /**
  * Get product category ids from array
  *
  * @param array $categories
  * @return array
  */
 protected function getCategoryIds($categories)
 {
     $ids = [];
     $tree = $this->categoryReadService->getTree();
     foreach ($categories as $name) {
         foreach ($tree->getChildrenData() as $child) {
             if ($child->getName() == $name) {
                 /** @var \Magento\Catalog\Api\Data\CategoryTreeInterface $child */
                 $tree = $child;
                 $ids[] = $child->getId();
                 if (!$tree->getChildrenData()) {
                     $tree = $this->categoryReadService->getTree();
                 }
                 break;
             }
         }
     }
     return $ids;
 }