Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/old/app/code/Kaliop/CatalogGenericProductItem/docs/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //home/corals/old/app/code/Kaliop/CatalogGenericProductItem/docs/05_Implement.md
[comment]: # (Chances are your module has seen conception while working on a client's project. Here is where you will provide instructions to properly implement your module's features in any project. Your fresh first use case will be very helpful to show some concrete stuff - be sure to make exhaustive references to it!)

By default, module viewModel is already injected on some native blocks. Have a look on view/frontend/layout/* files for more details.

Scenarios
---------

### Display generic product item from category/search result pages (or other covered native blocks)

#### Steps

1 - On your template (Magento_Catalog::product/list.phtml)
~~~php
use Kaliop\CatalogGenericProductItem\ViewModel\Product\Item;

...

// BLOCK TOP
/** @var Item $productItemViewModel */
$productItemViewModel = $block->getGenericProductItemViewModel();

...

// BEFORE THE LOOP, BUT AFTER DEFAULT PARAMS DEFINITION
/** CatalogGenericProductItem START (default display params) */
$displayParams = [
    'view_mode' => $viewMode,
    'image_area' => $imageDisplayArea,
    'show_description' => $showDescription,
    'template_type' => $templateType,
    'pos' => $pos
];
/** CatalogGenericProductItem END */

...

// INSIDE THE LOOP
<?php foreach ($_productCollection as $_product):?>
    <?php /** CatalogGenericProductItem START (addto block per product) */ ?>
    <?php if ($addToBlock = $block->getChildBlock('addto')): ?>
        <?php $addToBlockHtml = $addToBlock->setProduct($_product)->getChildHtml(); ?>
        <?php $displayParams['addto'] = $addToBlockHtml; ?>
    <?php endif; ?>
    <?php /** CatalogGenericProductItem END */ ?>

    <li class="item product product-item">
    <div class="product-item-info" data-container="product-<?= /* @noEscape */ $viewMode ?>">
        <?php /** CatalogGenericProductItem START (render) */ ?>
        <?= /* @noEscape */ $productItemViewModel->renderProductListItem($_product, $block, $displayParams); ?>
        <?php /** CatalogGenericProductItem END */ ?>
    </div>
</li>
<?php endforeach; ?>
~~~

And you are done!

### Display generic product item from custom template

#### Steps
1 - If not already injected (see view/frontend/layout/*.xml files), inject by yourself module ViewModel on the block which originally loops and renders products list item, from layout file
~~~xml
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="category.products.list">
            <arguments>
                <argument name="generic_product_item_view_model"
                          xsi:type="object">Kaliop\CatalogGenericProductItem\ViewModel\Product\Item
                </argument>
            </arguments>
        </referenceBlock>
    </body>
</page>
~~~

2 - Then, follow the same logic as the first scenario above

### I want to display an item with my own template

#### Steps
1 - Simply specify the parameter N°4 of *renderProductListItem* method
~~~php
 <?= /* @noEscape */ $productItemViewModel->renderProductListItem($_product, $block, $displayParams, 'Namespace_Module::item.phtml'); ?>
~~~

### I want to display an item with my own block type

#### Steps
1 - Simply specify the parameter N°5 of *renderProductListItem* method
~~~php
 <?= /* @noEscape */ $productItemViewModel->renderProductListItem($_product, $block, $displayParams, 'Kaliop_CatalogGenericProductItem::item.phtml', \Namespace\Module\Block\MySuperBlock::class); ?>
~~~

### Examples (from CNC Shopping project)
- product/list.phtml: https://bitbucket.org/agencesoon/0560-cnc-shopping/src/develop/app/design/frontend/Cnc/default/Magento_Catalog/templates/product/list.phtml
- product/list/items.phtml (upsell/related product lists): https://bitbucket.org/agencesoon/0560-cnc-shopping/src/develop/app/design/frontend/Cnc/default/Magento_Catalog/templates/product/list/items.phtml

Spamworldpro Mini