I want to display top rated products in magento 2
My block Toprated.php
public function getTesting()
{
$collection = $this->_productCollectionFactory->create();
foreach($collection as $eachColl)
{
$storeId = $eachColl->getStore()->getId();
$reviewSum = $this->reviewSummaryFactory->create()->setStoreId($storeId)->load($eachColl->getId());
$rated[] = array(
'rating' => $reviewSum['rating_summary'],
'name' => $eachColl->getName(),
'url' => $eachColl->getUrlPath(),
'product_sku' => $eachColl->getSku()
);
$rateds[$eachColl->getSku()] = $reviewSum['rating_summary'];
}
arsort($rateds);
$rateds = array_slice($rateds, 0, 3);
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToFilter('status', '1');
$collection->addAttributeToFilter('rating', array('in' => implode(",", $rateds)));
return $collection;
}
my template toprated.phtml file
<?php
$_productCollection = $this->getTesting();
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
?>
but phtml file not calling any data.what i did mistake here
Where do you want to show the new block ?
It is possible by edit/add a layout file in view/frontend/layout/
<?xml version="1.0" encoding="UTF-8"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<referenceContainer name="content">
<block
template="[your template]"
class="[your class]"
name="[your name]"/>
</referenceContainer>
create layout file in view -> frontend -> templates -> layout ,
file name should be your routname_controllernamespace_controllername (controller->Index->index.php)
ex. blog_index_index file should be look like this
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Demo\Blog\Block\PostList" name="demo.list" template="Demo_Blog::list.phtml" />
</referenceContainer>
</body>
</page>
Related
We have a Magento 2 site running Amasty Special Promotions, the "offer label" is showing on the product pages as expected however, it's not showing on the category pages at all. How could this be added, I came across this code in the Amasty extension:
In catalog_product_view.xml
<referenceContainer name="product.info.media">
<block before="-" class="Amasty\BannersLite\Block\Banner" name="amasty.banners.lite.label" template="label.phtml">
<action method="setPosition">
<argument name="position" xsi:type="string">2</argument>
</action>
</block>
</referenceContainer>
In label.phtml:
<?php foreach ($block->getBanners() as $banner) : ?>
<?php if ($block->isEnableBannerPosition() && $img = $block->getImage($banner)) : ?>
<img class="am-banners-lite-label"
alt="<?= $block->escapeHtml($block->getAlt($banner)) ?>"
src="<?= $block->escapeUrl($img) ?>">
<?php endif;?>
<?php endforeach; ?>
"catalog_product_view" fie is layout file for product page.
if you want to display on category page add file in your theme design folder "catalog_category_view.xml" :
<referenceContainer name="content">
<block before="-" class="Amasty\BannersLite\Block\Banner"
name="amasty.banners.lite.label" template="label.phtml">
<action method="setPosition">
<argument name="position" xsi:type="string">2</argument>
</action>
</block>
</referenceContainer>
I have created several custom product attributes and I need them to display on the Product Details tab if data has been entered in the Magento Admin. There are three in total and on a lot of occasions all three will have data but there will be around 20% of products where one or two of the have data.
I have created the attributes easily enough but I am struggling to get them to display on the frontend. I have pasted the code for the layout file and the phtml file for the attributes.
The result I am getting is a blank product screen with no info at all. Definitely an error somewhere but after hours of looking I am missing it.
Below is the custom catalog_product_view.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page layout="2columns-right" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<move element="product.info.stock.sku" destination="product.info.price" after="product.price.final"/>
<move element="page.main.title" destination="product.info.main" before="-"/>
<referenceBlock name="product.info.overview" remove="true"/>
<referenceBlock name="product.info.details">
<block class="Magento\Catalog\Block\Product\View\Description" name="product.info.description." template="Magento_Catalog::product/view/product_attributes.phtml" group="detailed_info">
<arguments>
<argument name="at_call" xsi:type="string">getDescription</argument>
<argument name="at_code" xsi:type="string">description</argument>
<argument name="css_class" xsi:type="string">description</argument>
<argument name="at_label" xsi:type="string">none</argument>
<argument name="title" translate="true" xsi:type="string">Product Details</argument>
</arguments>
</block>
<block class="Magento\Catalog\Block\Product\View" name="deliveryinfo.tab" as="deliveryinfo" template="product/view/delivery_info.phtml" group="detailed_info" >
<arguments>
<argument translate="true" name="title" xsi:type="string">Delivery</argument>
</arguments>
</block>
</referenceBlock>
<referenceContainer name="sidebar.main">
<block class="Magento\Cms\Block\Block" name="sidebar_delivery" after="-">
<arguments>
<argument name="block_id" xsi:type="string">sidebar_delivery</argument>
</arguments>
</block>
<block class="Magento\Cms\Block\Block" name="sidebar_instructions" after="-">
<arguments>
<argument name="block_id" xsi:type="string">sidebar_instructions</argument>
</arguments>
</block>
<block class="Magento\Cms\Block\Block" name="sidebar_brochures" after="-">
<arguments>
<argument name="block_id" xsi:type="string">sidebar_brochures</argument>
</arguments>
</block>
<block class="Magento\Cms\Block\Block" name="sidebar_blog" after="-">
<arguments>
<argument name="block_id" xsi:type="string">sidebar_blog</argument>
</arguments>
</block>
</referenceContainer>
</body>
</page>
Below is product_attributes.phtml called in the Layout file
<?php
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_product = $block->getProduct();
$_code = $block->getAtCode();
$_className = $block->getCssClass();
$_attributeLabel = $block->getAtLabel();
$_attributeType = $block->getAtType();
$_attributeAddAttribute = $block->getAddAttribute();
if ($_attributeLabel && $_attributeLabel == 'default') {
$_attributeLabel = $_product->getResource()->getAttribute($_code)->getFrontendLabel();
}
$_attributeValue =$_product->getResource()->getAttribute($_code)->getFrontend()->getValue($_product);
?>
<?php if ($_attributeValue): ?>
<div>
<?php if ($_attributeLabel != 'none'): ?><strong class="type"><?php echo $_attributeLabel?></strong><?php endif; ?>
<div class="value" <?php echo $_attributeAddAttribute;?>><?php echo $_attributeValue; ?></div>
</div>
<?php endif; ?>
<?php
$dimensions = $_product->getResource()->getAttribute('dimensions')->getFrontend()->getValue($_product);
$features_benefits = $_product->getResource()->getAttribute('features_benefits')->getFrontend()->getValue($_product);
$flooring_specification = $_product->getResource()->getAttribute('flooring_specification')->getFrontend()->getValue($_product);
if (!empty($dimensions) || !empty($features_benefits) || !empty($flooring_specification) {
?>
<?php
if (!empty($dimensions)) {
?>
<h2>Dimensions</h2>
<?php echo $dimensions; ?>
<?php
}
if (!empty($features_benefits)) {
?>
<h2>Features & Benefits</h2>
<?php echo $features_benefits; ?>
<?php
}
if (!empty($flooring_specification)) {
?>
<h2>Specification</h2>
<?php echo $flooring_specification; ?>
<?php
}
?>
<?php
}
?>
It is probably something laughably simple I have done wrong but I can't for the life of me find it.
The site is on Community Edition 2.2.5 and the server is running PHP 7.1
Any suggestions appreciated.
Kev
Solved the issue in the end. I missed out a closing bracket.
This line:
if (!empty($dimensions) || !empty($features_benefits) || !empty($flooring_specification) {
Should have been:
if (!empty($dimensions) || !empty($features_benefits) || !empty($flooring_specification)) {
I knew it would have been something simple. Works perfectly now.
Show category with images in homepage Magento2
http://ibnab.com/en/blog/magento-2/magento-2-frontend-how-to-call-category-collection-on-home-page
This article working fine but I need to show category image.How to fetch category images also
I am using $category->getImageUrl();
but its not working
I was able to get this to show on the homepage by combining the tutorial and R T's answer. Because i didn't have a _objectManager (and the page was kicking an error when i tried R T's code) working on that page, I included the category model in the block file (Collection.php)
protected $_categoryHelper;
protected $categoryFlatConfig;
protected $topMenu;
protected $categoryView;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Helper\Category $categoryHelper,
\Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState,
\Magento\Theme\Block\Html\Topmenu $topMenu,
\Magento\Catalog\Model\Category $categoryView
) {
$this->_categoryHelper = $categoryHelper;
$this->categoryFlatConfig = $categoryFlatState;
$this->topMenu = $topMenu;
$this->categoryView = $categoryView;
parent::__construct($context);
}
I then added a method to call in the phtml at the bottom of the block file.
public function getCategoryView() {
return $this->categoryView;
}
In the phtml (storecategories.phtml) I changed up the code to work like this.
<?php
$categories = $this->getStoreCategories(true,false,true);
$categoryHelper = $this->getCategoryHelper();
?>
<ul>
<?php
foreach($categories as $category):
if (!$category->getIsActive()) {
continue;
}
?>
<li><a href="<?php echo $categoryHelper->getCategoryUrl($category) ?>">
<?php
$catId = $category->getId();
$categoryAgain = $this->getCategoryView()->load($catId);
$_outputhelper = $this->helper('Magento\Catalog\Helper\Output');
$_imgHtml = '';
if ($_imgUrl = $categoryAgain->getImageUrl()) {
$_imgHtml = '<img src="' . $_imgUrl . '" />';
$_imgHtml = $_outputhelper->categoryAttribute($categoryAgain, $_imgHtml, 'image');
/* #escapeNotVerified */
echo $_imgHtml;
}
?>
<?php echo $category->getName() ?></a></li>
<?php endforeach; ?>
</ul>
And then i added the new call into the di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<type name="Ibnab\CategoriesSide\Block\CategorisCollection">
<arguments>
<argument name="deleteorderAction" xsi:type="array">
<item name="context" xsi:type="string">\Magento\Framework\View\Element\Template\Context</item>
<item name="helper" xsi:type="string">\Magento\Catalog\Helper\Category</item>
<item name="flatstate" xsi:type="string">\Magento\Catalog\Model\Indexer\Category\Flat\State</item>
<item name="menu" xsi:type="string">\Magento\Theme\Block\Html\Topmenu</item>
<item name="categoryview" xsi:type="string">\Magento\Catalog\Model\Category</item>
</argument>
</arguments>
</type>
I've managed to do this as below in template:
$category = $this->_objectManager->create('Magento\Catalog\Model\Category')->load($item->getId());
$_outputhelper = $this->helper('Magento\Catalog\Helper\Output');
$_imgHtml = '';
if ($_imgUrl = $category->getImageUrl()) {
$_imgHtml = '<img src="' . $_imgUrl . '" />';
$_imgHtml = $_outputhelper->categoryAttribute($category, $_imgHtml, 'image');
/* #escapeNotVerified */
echo $_imgHtml;
}
hope it helps
So i have a Zend_Route in my application like this :
public function _initRoutes() {
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$route = new Zend_Controller_Router_Route(':language/:controller/:action/*',
array(
'language' => 'de',
'controller'=> 'index',
'action' => 'index'
),
array(
'language' => '[a-z]{2}'
));
$router->addRoute('lang_route', $route);
}
and my xml
<?xml version="1.0" encoding="UTF-8" ?>
<configdate>
<nav>
<home>
<label>Home</label>
<controller>index</controller>
<action>index</action>
<pages>
<my_account>
<label>Galery</label>
<controller>index</controller>
<action>list</action>
</my_account>
</pages>
</home>
<login>
<label>Login</label>
<controller>login</controller>
<action>index</action>
</login>
</nav>
</configdate>
My problem is, that Zend_Navigation creats wrong urls.
So when i enter the url
http://localhost/zf/public/en
the urls generated by Zend_Navigation still looks like
http://localhost/zf/public/de/index/
Hope anyone has some ideas :)
You have to add the route you want to use for creating the correct URL in your Xml configuration:
<?xml version="1.0" encoding="UTF-8" ?>
<configdate>
<nav>
<home>
<label>Home</label>
<controller>index</controller>
<action>index</action>
<route>lang_route</route>
</home>
</nav>
</configdate>
In the Xml configuration you can use the same keywords as specified for Zend_Navigation_Page_Mvc.
hi i want to use a breadcrumb for my zend framework application
i craeted navigation.xml in configs folder where application.ini is .
and in the bootstarp i added following code
protected function _initNavigation()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$config = new Zend_Config_Xml(APPLICATION_PATH.'/configs/navigation.xml');
$navigation = new Zend_Navigation($config);
$view->navigation($navigation);
}
and in the layout i added folllowing code
<div id="menu">
<?php echo $this->navigation()->menu(); ?>
</div>
<div id="breadcrumbs">
You are in: <?php echo $this->navigation()->breadcrumbs()->setLinkLast(false)->setMinDepth(0)->render(); ?>
</div>
it is not working , errors are given
Fatal error: Uncaught exception 'Zend_Navigation_Exception' with message 'Invalid argument: Unable to determine class to instantiate' in /home/kanishka/workspace/jetwing_ibe/library/Zend/Navigation/Page.php:223
Stack trace:
#0 /home/kanishka/workspace/jetwing_ibe/library/Zend/Navigation/Container.php(117): Zend_Navigation_Page::factory(Array)
#1 /home/kanishka/workspace/jetwing_ibe/library/Zend/Navigation/Container.php(164): Zend_Navigation_Container->addPage(Array)
#2 /home/kanishka/workspace/jetwing_ibe/library/Zend/Navigation.php(46): Zend_Navigation_Container->addPages(Object(Zend_Config_Xml))
#3 /home/kanishka/workspace/jetwing_ibe/application/Bootstrap.php(94): Zend_Navigation->__construct(Object(Zend_Config_Xml))
#4 /home/kanishka/workspace/jetwing_ibe/library/Zend/Application/Bootstrap/BootstrapAbstract.php(666): Bootstrap->_initNavigation()
#5 /home/kanishka/workspace/jetwing_ibe/library/Zend/Application/Bootstrap/BootstrapAbstract.php(619): Zend_Application_Bootstrap_BootstrapAbstract->_executeResource('navigati in /home/kanishka/workspace/jetwing_ibe/library/Zend/Navigation/Page.php on line 223
this is my xml file
<?xml version="1.0" encoding="UTF-8"?>
<config>
<nav>
<dashboard>
<label>dashboard</label>
<controller>dashboard</controller>
<action>index</action>
<resource>dashboard</resource>
<pages>
<rates>
<label>Rates</label>
<controller>rates</controller>
<action>index</action>
<pages>
<index>
<label>index</label>
<controller>rates</controller>
<action>index</action>
<class>dontdisplay</class>
</index>
</pages>
</rates>
<occupancydenomination>
<label>Occupancydenominations</label>
<controller>occupancydenomination</controller>
<action>index</action>
<pages>
<index>
<label>Occupancydenomination</label>
<controller>occupancydenomination</controller>
<action>index</action>
<class>dontdisplay</class>
</index>
<add>
<label>Add Occupancydenomination</label>
<controller>occupancydenomination</controller>
<action>add</action>
<class>dontdisplay</class>
</add>
</pages>
</occupancydenomination>
</pages>
</dashboard>
</nav>
</config>
i am not sure what the error is . please help me ................
The error is due to your config file.
You're not providing enough parameters for the navigation container to determine the correct page type, Zend_Navigation_Page_Mvc or Zend_Navigation_Page_Uri.
Also, you know there's a navigation resource plugin, right?
UPDATE
Get rid of the <nav> wrapper element. It's trying to interpret that as a page.
That or do follow the example and specify the config section correctly
$config = new Zend_Config_Xml('/path/to/navigation.xml', 'nav');
$container = new Zend_Navigation($config);