Creating the size guide

We have been asked to add a functionality that shows the size guide on a product view page. This is to appear as a new tab next to the existing Details, More Information, and Reviews tabs. The content of the size guide tab is to be the same for all products containing the size attribute. We also need it to be editable from Magento admin.

Let's take a moment to think about our approach here:

  • To be the same for all products and editable from Magento admin needs the CMS block
  • The CMS block needs setup script for creating the size guide block
  • To Appear as a new tab next to the existing tabs requires a catalog_product_view.xml layout update

Assuming we have defined registration.php, composer.json, and etc/module.xml as basic module files, we can deal with the more specific details of our Magelicious_Catalog module.

We start by defining <MODULE_DIR>/Setup/InstallData.php with content, as follows:

namespace MageliciousCatalogSetup;
class InstallData implements MagentoFrameworkSetupInstallDataInterface {
protected $searchCriteriaBuilder;
protected $blockRepository;
protected $blockFactory;

public function __construct(
MagentoFrameworkApiSearchCriteriaBuilder $searchCriteriaBuilder,
MagentoCmsApiBlockRepositoryInterface $blockRepository,
MagentoCmsApiDataBlockInterfaceFactory $blockFactory
) {
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->blockRepository = $blockRepository;
$this->blockFactory = $blockFactory;
}

public function install(
MagentoFrameworkSetupModuleDataSetupInterface $setup,
MagentoFrameworkSetupModuleContextInterface $context
) {
$setup->startSetup();
$searchCriteria = $this->searchCriteriaBuilder
->addFilter('identifier', 'size-guide', 'eq')
->create();
$blocks = $this->blockRepository->getList($searchCriteria)->getItems();
if (empty($blocks)) {
/* @var MagentoCmsApiDataBlockInterface $block */
$block = $this->blockFactory->create();
$block->setIdentifier('size-guide'),
$block->setTitle('Size Guide'),
$block->setContent('Size guide!'),
$this->blockRepository->save($block);
}
$setup->endSetup();
}
}

The InstallData script ensures that the size-guide CMS block is created during module installation if it does not already exist. With this in place, we can already run the setup:upgrade command. This should install our module and create the size-guide CMS block.

We then define <MODULE_DIR>/Block/SizeGuide.php with content, as follows:

namespace MageliciousCatalogBlock;
class SizeGuide extends MagentoCmsBlockBlock implements MagentoFrameworkDataObjectIdentityInterface {
protected $product;
protected $coreRegistry;

public function __construct(
MagentoFrameworkViewElementContext $context,
MagentoCmsModelTemplateFilterProvider $filterProvider,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoCmsModelBlockFactory $blockFactory,
MagentoFrameworkRegistry $coreRegistry,
array $data = []
) {
$this->coreRegistry = $coreRegistry;
parent::__construct($context, $filterProvider, $storeManager, $blockFactory, $data);
}
public function _toHtml() { /* ... */ }
public function getProduct() {
if (!$this->product) {
$this->product = $this->coreRegistry->registry('product'),
}
return $this->product;
}
}

This is the actual block class that we will output on the product view page. The registry's object product key is already set by the parent class up the layout tree. This allows us to easily fetch the instance of the current product.

The _toHtml method is further implemented, as follows:

protected function _toHtml()
{
if ($this->getProduct()->getTypeId() == MagentoConfigurableProductModelProductTypeConfigurable::TYPE_CODE) {
$configurableAttributes = $this->getProduct()->getTypeInstance()->getConfigurableAttributesAsArray($this->getProduct());
foreach ($configurableAttributes as $attribute) {
if (isset($attribute['attribute_code']) && $attribute['attribute_code'] == 'size') {
return parent::_toHtml();
}
}
}
return '';
}

This is the gist of our size guide functionality. The configurable type and size attribute code checks ensure that the output of _toHtml renders the size-guide block only for certain groups of products.

We finally define <MODULE_DIR>/view/frontend/layout/catalog_product_view.xml with content, as follows:

<page>
<body>
<referenceBlock name="product.info.details">
<block class="MageliciousCatalogBlockSizeGuide" name="size-guide" after="-" group="detailed_info">
<arguments>
<argument name="block_id" xsi:type="string">size-guide</argument>
<argument name="css_class" xsi:type="string">description</argument>
<argument name="at_label" xsi:type="string">none</argument>
<argument name="title" translate="true" xsi:type="string">Size Guide</argument>
</arguments>
</block>
    </referenceBlock>
</body>
</page>

This is the glue that binds our SizeGuide block to a product view page, and, more specifically, the product.info.details block that neatly contains the DetailsMore Information, and Reviews tabs.

The final product view page result should look like this:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.233.58