TransWikia.com

Magento 2 Sort order on catalogue widget

Magento Asked by Carter on December 23, 2020

I have a working widget that shows the products but how can I get the products to be sorted by the name. I have researched and a-lot of options show how to add a button that will sort the products but im looking for a way to have it by default sorted by name.

<?php if ($exist = ($block->getProductCollection() && $block->getProductCollection()- 
 >getSize())) : ?>
<?php
$type = 'widget-product-grid';

$mode = 'grid';

$image = 'new_products_content_widget_grid';
$items = $block->getProductCollection()->getItems();

$showWishlist = true;
$showCompare = true;
$showCart = true;
$templateType = MagentoCatalogBlockProductReviewRendererInterface::SHORT_VIEW;
$description = false;

?>
<div class="block widget block-products-list <?= /* @noEscape */ $mode ?>">
    <?php if ($block->getTitle()) : ?>
        <div class="block-title">
            <strong><?= $block->escapeHtml(__($block->getTitle())) ?></strong>
        </div>
    <?php endif ?>
    <div class="block-content">
        <?= /* @noEscape */ '<!-- ' . $image . '-->' ?>
        <div class="products-<?= /* @noEscape */ $mode ?> <?= /* @noEscape */ $mode ?>">
            <ol class="product-items <?= /* @noEscape */ $type ?>">
                <?php $iterator = 1; ?>
                <?php foreach ($items as $_item) : ?>
                    <?= /* @noEscape */ ($iterator++ == 1) ? '<li class="product-item">' : '</li><li class="product-item">' ?>
                    <div class="product-item-info series-widget-felix">  <!-- max-width: min-content; -->
                        <a href="<?= $block->escapeUrl($block->getProductUrl($_item)) ?>" class="product-item-photo">
                            <div class="img-widget-felix">
                                <?= $block->getImage($_item, $image)->toHtml() ?>
                            </div>
                        </a>
                        <div class="product-item-details details-widget-felix">
                            <center>
                            <strong class="product-item-name">
                                <a title="<?= $block->escapeHtml($_item->getName()) ?>"
                                   href="<?= $block->escapeUrl($block->getProductUrl($_item)) ?>"
                                   class="product-item-link">
                                    <?= $block->escapeHtml($_item->getName()) ?>
                                </a>
                            </strong>

                            <?php
                            $delivered_lumens = '';
                            if (null !== $_item->getCustomAttribute('delivered_lumens')) {
                                $delivered_lumens = $_item->getCustomAttribute('delivered_lumens')->getValue();
                                $lumens =  max (explode('-', $delivered_lumens));

                            }
                            $aperture = '';
                            if($_item->getAttributeText("aperture_size")){
                                $aperture = $_item->getAttributeText("aperture_size");
                            }
                            ?>

                            <?php
                            $product_name = $_item->getName();
                            $isMiniTrack = false;
                            if (stripos($product_name, "mini-track") !== false){
                                $isMiniTrack = true;
                            }
                            if(!$isMiniTrack){


                                if($lumens > 0){
                                    ?>
                                    <label> <?= $aperture ?> | <?= $lumens ?> lm</label>

                                <?php }
                            } ?>

                            </center>
                        </div>
                    </div>
                    <?= ($iterator == count($items) + 1) ? '</li>' : '' ?>
                <?php endforeach ?>
            </ol>
        </div>
        <?= $block->getPagerHtml() ?>
    </div>
</div>

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP