Table of Contents for
Magento 2 Development Quick Start Guide

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Magento 2 Development Quick Start Guide by Branko Ajzele Published by Packt Publishing, 2018
  1. Magento 2 Development Quick Start Guide
  2. Title Page
  3. Copyright and Credits
  4. Magento 2 Development Quick Start Guide
  5. Packt Upsell
  6. Why subscribe?
  7. Packt.com
  8. Contributors
  9. About the author
  10. About the reviewer
  11. Packt is searching for authors like you
  12. Table of Contents
  13. Preface
  14. Who this book is for
  15. What this book covers
  16. To get the most out of this book
  17. Download the example code files
  18. Code in Action
  19. Conventions used
  20. Get in touch
  21. Reviews
  22. Understanding the Magento Architecture
  23. Technical requirements
  24. Installing Magento
  25. Modes
  26. Areas
  27. Request flow processing
  28. Modules
  29. Creating the minimal module
  30. Cache
  31. Dependency injection
  32. Argument injection
  33. Virtual types
  34. Proxies
  35. Factories
  36. Plugins
  37. The before plugin
  38. The around plugin
  39. The after plugin
  40. Events and observers
  41. Console commands
  42. Cron jobs
  43. Summary
  44. Working with Entities
  45. Technical requirements
  46. Understanding types of models
  47. Creating a simple model
  48. Methods worth memorizing
  49. Working with setup scripts
  50. The InstallSchema script
  51. The UpgradeSchema script
  52. The Recurring script
  53. The InstallData script
  54. The UpgradeData script
  55. The RecurringData script
  56. Extending entities
  57. Creating extension attributes
  58. Summary
  59. Understanding Web APIs
  60. Technical requirements
  61. Types of users
  62. Types of authentication
  63. Types of APIs
  64. Using existing web APIs
  65. Creating custom web APIs
  66. Understanding search criteria
  67. Summary
  68. Building and Distributing Extensions
  69. Technical requirements
  70. Building a shipping extension
  71. Distributing via GitHub
  72. Distributing via Packagist
  73. Summary
  74. Developing for Admin
  75. Technical requirements
  76. Using the listing component
  77. Using the form component
  78. Summary
  79. Developing for Storefront
  80. Technical requirements
  81. Setting up the playground
  82. Calling and initializing JS components
  83. Meet RequireJS
  84. Replacing jQuery widget components
  85. Extending jQuery widget components
  86. Creating jQuery widgets components
  87. Creating UI/KnockoutJS components
  88. Extending UI/KnockoutJS components
  89. Summary
  90. Customizing Catalog Behavior
  91. Technical requirements
  92. Creating the size guide
  93. Creating the same day delivery
  94. Flagging new products
  95. Summary
  96. Customizing Checkout Experiences
  97. Technical requirements
  98. Passing data to the checkout
  99. Adding order notes to the checkout
  100. Summary
  101. Customizing Customer Interactions
  102. Technical requirements
  103. Understanding the section mechanism
  104. Adding contact preferences to customer accounts
  105. Adding contact preferences to the checkout
  106. Summary
  107. Other Books You May Enjoy
  108. Leave a review - let other readers know what you think

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 Magelicious\Catalog\Setup;
class InstallData implements \Magento\Framework\Setup\InstallDataInterface {
protected $searchCriteriaBuilder;
protected $blockRepository;
protected $blockFactory;

public function __construct(
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
\Magento\Cms\Api\BlockRepositoryInterface $blockRepository,
\Magento\Cms\Api\Data\BlockInterfaceFactory $blockFactory
) {
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->blockRepository = $blockRepository;
$this->blockFactory = $blockFactory;
}

public function install(
\Magento\Framework\Setup\ModuleDataSetupInterface $setup,
\Magento\Framework\Setup\ModuleContextInterface $context
) {
$setup->startSetup();
$searchCriteria = $this->searchCriteriaBuilder
->addFilter('identifier', 'size-guide', 'eq')
->create();
$blocks = $this->blockRepository->getList($searchCriteria)->getItems();
if (empty($blocks)) {
/* @var \Magento\Cms\Api\Data\BlockInterface $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 Magelicious\Catalog\Block;
class SizeGuide extends \Magento\Cms\Block\Block implements \Magento\Framework\DataObject\IdentityInterface {
protected $product;
protected $coreRegistry;

public function __construct(
\Magento\Framework\View\Element\Context $context,
\Magento\Cms\Model\Template\FilterProvider $filterProvider,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Cms\Model\BlockFactory $blockFactory,
\Magento\Framework\Registry $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() == \Magento\ConfigurableProduct\Model\Product\Type\Configurable::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="Magelicious\Catalog\Block\SizeGuide" 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: