magento 2如何在主页上显示类别产品?

时间:2015-12-15 14:44:25

标签: magento magento-2.0

 <p>{{block type="catalog/product_list" 
           column_count="4" 
           category_id="13" 
           template="catalog/product/list.phtml"}}</p>

我已在magento 1.x版中使用上述代码在主页中显示类别产品

更新到Magento 2.0版后,上面的代码不再有效了。

我该如何纠正?

5 个答案:

答案 0 :(得分:1)

&#13;
&#13;
{{widget type="Magento\CatalogWidget\Block\Product\ProductsList"
         products_count="4" 
        template="product/widget/content/grid.phtml"
        conditions_encoded="a:2:[i:1;a:4:[s:4:`type`;    s:50:`Magento|CatalogWidget|Model|Rule|Condition|Combine`;s:10:`aggregator`;s:3:`all`;s:5:`value`;s:1:`1`;s:9:`new_child`;s:0:``;]s:4:`1--1`;a:4:[s:4:`type`;s:50:`Magento|CatalogWidget|Model|Rule|Condition|Product`;s:9:`attribute`;s:12:`category_ids`;s:8:`operator`;s:2:`==`;s:5:`
            value`;s:1:`13`;]]"}}
&#13;
&#13;
&#13;

在上面的代码最后一行中,13表示类别id 根据需要更改category_id

答案 1 :(得分:0)

试试这个

{{block type =“core / template”column_count =“4”category_id =“13”template =“catalog / product / list.phtml”}}

之后转到yourtheme / catalog / product / list.phtml,然后尝试在此文件的顶部打印或回显

希望它能运作

答案 2 :(得分:0)

{{widget type="Magento\CatalogWidget\Block\Product\ProductsList"
         products_count="4" 
        template="product/widget/content/grid.phtml"
        conditions_encoded="a:2:[i:1;a:4:[s:4:`type`;    s:50:`Magento|CatalogWidget|Model|Rule|Condition|Combine`;s:10:`aggregator`;s:3:`all`;s:5:`value`;s:1:`1`;s:9:`new_child`;s:0:``;]s:4:`1--1`;a:4:[s:4:`type`;s:50:`Magento|CatalogWidget|Model|Rule|Condition|Product`;s:9:`attribute`;s:12:`category_ids`;s:8:`operator`;s:2:`==`;s:5:`
            value`;s:1:`13`;]]"}}

答案 3 :(得分:0)

试试这段代码

<p>{{widget type="Magento\CatalogWidget\Block\Product\ProductsList" title="CTlog" show_pager="1" products_per_page="5" products_count="10" template="product/widget/content/grid.phtml" conditions_encoded="a:1:[i:1;a:4:[s:4:`type`;s:50:`Magento|CatalogWidget|Model|Rule|Condition|Combine`;s:10:`aggregator`;s:3:`all`;s:5:`value`;s:1:`1`;s:9:`new_child`;s:0:``;]]" page_var_name="pmqabw"}}</p>

答案 4 :(得分:0)

尝试

<?php
$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();        
$categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');
$categoryHelper = $objectManager->get('\Magento\Catalog\Helper\Category');
$categoryRepository = $objectManager->get('\Magento\Catalog\Model\CategoryRepository');
$priceHelper = $objectManager->create('Magento\Framework\Pricing\Helper\Data'); 
$productmodel = $objectManager->create('Magento\Catalog\Block\Product\ListProduct'); 
?>
<div class="worldcup-deals">
                <div class="deals-baaner">
                    <img src='<?php echo $this->getViewFileUrl('images/worldcup/2.png'); ?>' alt="deal-banner">
                </div>
                <div class="deals-products">
                    <div class="category-product-listing-world">
                        <?php 
                        $bestsamrtphoneid = 6685; 
                        $category = $categoryFactory->create()->load($bestsamrtphoneid);
                        $samrtphonecategoryProducts = $category->getProductCollection()
                        ->addAttributeToSelect('*');
                        $samrtphonecategoryProducts->getSelect()->order('RAND()');
                        $samrtphonecategoryProducts->getSelect()->limit(8);
                        //->setPageSize(8) // only get 10 products 
                        //->setCurPage(1);
                        ?>
                        <?php foreach ($samrtphonecategoryProducts as $product) { ?>
                        <div class="product-listing-world-item">
                            <?php 
                            $imagehelper = $objectManager->create('Magento\Catalog\Helper\Image');
                            $image = $imagehelper->init($product,'category_page_list')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(300, 300)->getUrl();
                            ?>
                            <div class="inner-world-wrap">
                                <a href="<?php echo $product->getProductUrl(); ?>">
                                    <img src="<?php echo $image; ?>">
                                </a>
                                <h3 class="product-name">
                                    <a href="<?php echo $product->getProductUrl(); ?>">
                                        <?php echo $product->getName() ?>
                                    </a>
                                </h3>
                                <?php echo $productmodel->getProductPrice($product);?>

                                <div class="shop-new">
                                    <a href="<?php echo $product->getProductUrl(); ?>">
                                        <?php echo __('Shop now >'); ?>
                                    </a>
                                    <img src="<?php echo $this->getViewFileUrl('images/demo/demo.png'); ?>">
                                </div>
                            </div>
                        </div>
                        <?php } ?>
                    </div>  
                    <a class="view" href="https://avechi.com/wakilisha-world-cup/cool-phones.html" target="_blank">view more >></a>
                </div>                       
            </div>