Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cms block to category by block name in categories yaml #100

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion Component/Categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class Categories extends YamlComponentAbstract
protected $description = 'Component to import categories.';
protected $groupFactory;
protected $category;

/**
* @var \Magento\Cms\Api\Data\BlockInterfaceFactory
*/
protected $blockFactory;
private $mainAttributes = [
'name',
'is_active',
Expand All @@ -26,11 +31,13 @@ public function __construct(
\Magento\Framework\ObjectManagerInterface $objectManager,
\Magento\Catalog\Model\CategoryFactory $category,
\Magento\Store\Model\GroupFactory $groupFactory,
\Magento\Framework\App\Filesystem\DirectoryList $dirList
\Magento\Framework\App\Filesystem\DirectoryList $dirList,
\Magento\Cms\Api\Data\BlockInterfaceFactory $blockFactory
) {
$this->category = $category;
$this->groupFactory = $groupFactory;
$this->dirList = $dirList;
$this->blockFactory = $blockFactory;
parent::__construct($log, $objectManager);
}

Expand Down Expand Up @@ -137,6 +144,25 @@ public function createOrUpdateCategory(
}

$category->setImage($img);
break;
// Attaching cms block
case 'cms_block':
// getting block by name
$block = $this->blockFactory->create()->getCollection()
->addFieldToFilter('title', $value)
->setPageSize(1)
->getFirstItem();

// check if block exist
if (!$block) {
$this->log->logError("Can't find cms block with name '%s'", $value);
}

// Attach cms block by id
$category->setData('landing_page', $block->getId());
// set category display mode to Satic block and products
$category->setData('display_mode', 'PRODUCTS_AND_PAGE');

break;
default:
$category->setCustomAttribute($attribute, $value);
Expand Down
1 change: 1 addition & 0 deletions Samples/Components/Categories/categories.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ categories:
name: Category 1
description: "A description"
url_key: "category-1"
cms_block: "All stores" # cms block title
categories:
-
name: "Sub Category 1"
Expand Down