Sunday, November 6, 2016

Magento2: Override Widget Template

Suppose you need to add a new template for "Catalog Product List" type widget.

According to below code, you may easily can do that.

(1) app/code/ISM/Blog/etc/module.xml
<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="ISM_Blog" setup_version="0.1.1">
        <sequence>
            <module name="Magento_CatalogWidget" />
        </sequence>
    </module>
</config>

(2) app/code/ISM/Blog/etc/widget.xml
<?xml version="1.0" encoding="UTF-8"?><widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Widget:etc/widget.xsd">
    <widget id="products_list">
        <parameters>
            <parameter name="template" xsi:type="select" required="true" visible="true">
                <label translate="true">Template</label>
                <options>
                    <option name="blog_post_products_template" value="ISM_Blog::product/widget/content/blogGrid.phtml">
                        <label translate="true">Blog Post Products Grid Template</label>
                    </option>
                </options>
            </parameter>
        </parameters>
    </widget>
</widgets>

(3) app/code/ISM/Blog/view/frontend/templates/product/widget/content/blogGrid.phtml
Note: Copy vendor/magento/module-catalog-widget/view/frontend/templates/product/widget/content/grid.phtml to the above path in custom module.

<?php/** * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */
// @codingStandardsIgnoreFile
?><?php/** * Template for displaying products list widget * * @var $block \Magento\CatalogWidget\Block\Product\ProductsList */?><?php if ($exist = ($block->getProductCollection() && $block->getProductCollection()->getSize())):?><?php    $type = 'widget-product-grid';

    $mode = 'grid';

    $image = 'new_products_content_widget_grid';
    $title = $block->getTitle() ? __($block->getTitle()) : '';
    $items = $block->getProductCollection()->getItems();

    $showWishlist = true;
    $showCompare = true;
    $showCart = true;
    $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::DEFAULT_VIEW;
    $description = false;
?>    <div class="block widget block-products-list <?php /* @escapeNotVerified */ echo $mode; ?>">
        <?php if ($title):?>        <div class="block-title">
            <strong><?php /* @escapeNotVerified */ echo $title; ?></strong>
        </div>
        <?php endif ?>        <div class="block-content">
            <?php /* @escapeNotVerified */ echo '<!-- ' . $image . '-->' ?>            <div class="products-<?php /* @escapeNotVerified */ echo $mode; ?> <?php /* @escapeNotVerified */ echo $mode; ?>">
                <ol class="product-items <?php /* @escapeNotVerified */ echo $type; ?>">
                    <?php $iterator = 1; ?>                    <?php foreach ($items as $_item): ?>                    <div class="col-md-12">
                        <div class="row">
                        <?php /* @escapeNotVerified */ echo($iterator++ == 1) ? '<li class="product-item">' : '</li><li class="product-item">' ?>                        <div class="product-item-info">
                           <div class="col-md-3">
                            <div class="product-photo-capsules">
                                <a href="<?php /* @escapeNotVerified */ echo $block->getProductUrl($_item) ?>" class="product-item-photo">
                                    <?php echo $block->getImage($_item, $image)->toHtml(); ?>                                </a>
                            </div>
                           </div>
                            <div class="col-md-9">
                            <div class="product-item-details">
                                <strong class="product-item-name">
                                    <a title="<?php echo $block->escapeHtml($_item->getName()) ?>"                                       href="<?php /* @escapeNotVerified */ echo $block->getProductUrl($_item) ?>"                                       class="product-item-link">
                                        <?php echo $block->escapeHtml($_item->getName()) ?>                                    </a>
                                </strong>
                                <?php                                echo $block->getProductPriceHtml($_item, $type);
                                ?>
                                <?php if ($templateType): ?>                                    <?php echo $block->getReviewsSummaryHtml($_item, $templateType) ?>                                <?php endif; ?>
                                <?php if ($showWishlist || $showCompare || $showCart): ?>                                    <div class="product-item-actions">
                                        <?php if ($showCart): ?>                                            <div class="actions-primary">
                                                <?php if ($_item->isSaleable()): ?>                                                    <?php if ($_item->getTypeInstance()->hasRequiredOptions($_item)): ?>                                                        <button class="action tocart primary"                                                                data-mage-init='{"redirectUrl":{"url":"<?php /* @escapeNotVerified */ echo $block->getAddToCartUrl($_item) ?>"}}'                                                                type="button" title="<?php /* @escapeNotVerified */ echo __('Add to Cart') ?>">
                                                            <span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
                                                        </button>
                                                    <?php else: ?>                                                        <?php                                                            $postDataHelper = $this->helper('Magento\Framework\Data\Helper\PostHelper');
                                                            $postData = $postDataHelper->getPostData($block->getAddToCartUrl($_item), ['product' => $_item->getEntityId()])
                                                        ?>                                                        <button class="action tocart primary"                                                                data-post='<?php /* @escapeNotVerified */ echo $postData; ?>'                                                                type="button" title="<?php /* @escapeNotVerified */ echo __('Add to Cart') ?>">
                                                            <span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
                                                        </button>
                                                    <?php endif; ?>                                                <?php else: ?>                                                    <?php if ($_item->getIsSalable()): ?>                                                        <div class="stock available"><span><?php /* @escapeNotVerified */ echo __('In stock') ?></span></div>
                                                    <?php else: ?>                                                        <div class="stock unavailable"><span><?php /* @escapeNotVerified */ echo __('Out of stock') ?></span></div>
                                                    <?php endif; ?>                                                <?php endif; ?>                                            </div>
                                        <?php endif; ?>                                        <?php if ($showWishlist || $showCompare): ?>                                            <div class="actions-secondary" data-role="add-to-links">
                                                <?php if ($this->helper('Magento\Wishlist\Helper\Data')->isAllow() && $showWishlist): ?>                                                    <a href="#"                                                       data-post='<?php /* @escapeNotVerified */ echo $block->getAddToWishlistParams($_item); ?>'                                                       class="action towishlist" data-action="add-to-wishlist"                                                       title="<?php /* @escapeNotVerified */ echo __('Add to Wish List') ?>">
                                                        <span><?php /* @escapeNotVerified */ echo __('Add to Wish List') ?></span>
                                                    </a>
                                                <?php endif; ?>                                                <?php if ($block->getAddToCompareUrl() && $showCompare): ?>                                                    <?php $compareHelper = $this->helper('Magento\Catalog\Helper\Product\Compare');?>                                                    <a href="#" class="action tocompare"                                                       data-post='<?php /* @escapeNotVerified */ echo $compareHelper->getPostDataParams($_item);?>'                                                       title="<?php /* @escapeNotVerified */ echo __('Add to Compare') ?>">
                                                        <span><?php /* @escapeNotVerified */ echo __('Add to Compare') ?></span>
                                                    </a>
                                                <?php endif; ?>                                            </div>
                                        <?php endif; ?>                                    </div>
                                <?php endif; ?>                            </div>
                            </div>
                        </div>
                        <?php echo($iterator == count($items)+1) ? '</li>' : '' ?>                       </div>
                    </div>
                    <?php endforeach ?>                </ol>
            </div>
            <?php echo $block->getPagerHtml() ?>        </div>
    </div>
<?php endif;?>

No comments:

Post a Comment