Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/sbboke.com/wp-content/plugins/wordpress-seo/src/integrations/front-end-integration.php:409) in /www/wwwroot/sbboke.com/wp-content/themes/pacify/content-single.php on line 5

magento 2 如何获得销售产品系列

为了在网站上显示On Sale Products集合,商店管理员将需要有关On Sale Product Collection的数据。但是,获取集合并不是一项简单的任务,也没有很多文章可以为此解决此问题。因此,在今天的帖子中,我将指导您如何在Magento 2中获得销售产品系列

如何获得销售产品系列

  • 第1步:创建OnSaleProduct块
  • 第2步:在phtml文件中插入
  • 第3步:刷新缓存和测试结果

第1步:创建OnSaleProduct块{}

要获得销售产品系列,首先,您需要创建一个OnSaleProduct块。为此,请按照路径Mageplaza/Productslider/Block/OnSaleProduct.php添加以下代码:

<?php
/**
 * Mageplaza
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Mageplaza.com license that is
 * available through the world-wide-web at this URL:
 * https://www.mageplaza.com/LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category    Mageplaza
 * @package     Mageplaza_Productslider
 * @copyright   Copyright (c) Mageplaza (https://www.mageplaza.com/)
 * @license     https://www.mageplaza.com/LICENSE.txt
 */
namespace Mageplaza\Productslider\Block;
use Zend_Db_Expr;
/**
 * Class OnSaleProduct
 * @package Mageplaza\Productslider\Block
 */
class OnSaleProduct extends AbstractSlider
{
    /**
     * @inheritdoc
     */
    public function getProductCollection()
    {
        $visibleProducts = $this->_catalogProductVisibility->getVisibleInCatalogIds();
        $collection = $this->_productCollectionFactory->create()->setVisibility($visibleProducts);
        $collection = $this->_addProductAttributesAndPrices($collection)
            ->addAttributeToFilter(
                'special_from_date',
                ['date' => true, 'to' => $this->getEndOfDayDate()],
                'left'
            )->addAttributeToFilter(
                'special_to_date',
                ['or' => [0 => ['date' => true,
                                                   'from' => $this->getStartOfDayDate()],
                                             1 => ['is' => new Zend_Db_Expr(
                                                 'null'
                                             )],]],
                'left'
            )->addAttributeToSort(
                'news_from_date',
                'desc'
            )->addStoreFilter($this->getStoreId())->setPageSize(
                $this->getProductsCount()
            );
        return $collection;
    }
}

第2步:在phtml文件中插入{}

在块中进行收集后,现在您可以按照此片段从块中获取产品集合 Mageplaza/HelloWorld/view/frontend/templates/list.phtml

然后,请在phtml文件中插入以下代码。

<?php
$collection = $block->getProductCollection();
foreach ($collection as $_product) {
    echo $product->getName() . ' - ' . $product->getProductUrl() . '<br />';
}

第3步:刷新缓存和测试结果{}

最后,让我们刷新缓存和测试结果。

采集

以上是帮助您在Magento 2上获得On Sale Products Collection的三个步骤。我希望在阅读完之后,您将能够轻松地显示您的收藏。如果您有任何问题或想法,请随时在下面发表评论

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论