I have the following category structure in my magento store
- root
- category 0 (ID 26)
- category 1 (main)
- category A (sub)
- category V (sub)
- category 2 (main)
- category G (sub)
- category J (sub)
- category E (sub)
- category 3 (main)
- category L (sub)
and so on...
On a page i want to be able to output the following:
1. get each (main) category and its name and url
2. get the first (sub) category in (main) and its icon (custom attribute)
So it would output this for each:
<img src="main-cat-first-sub-icon">
main-cat-name
Giving something like:
<category 1 link><category A icon img></link>
<category 1 link><category 1 name>
<category 2 link><category G icon img>
<category 2 link><category 2 link>
<category 3 link><category L icon img>
<category 3 link><category 3 link>
What is the best way of doing so? So far I have:
<?php
$_id = 26 // category 0
$_main_categories = Mage::getModel('catalog/category')
->getCollection()
->addFieldToFilter('parent_id', array('eq'=>$_id))
->addAttributeToFilter('is_active', 1)
->addAttributeToSelect(array('id','name','url'))
foreach($_main_categories as $_main_cat)
{
// load full main category
$_category = Mage::getModel('catalog/category')->load($_main_cat->getId());
// get sub category's children
$_main_cat_subs = $_category->getChildrenCategories();
// loop through sub catagories, get icon in first and break
foreach($_main_cat_subs as $_sub_cat)
{
// load full sub category
$_sub = Mage::getModel('catalog/category')->load($_sub_cat->getId());
$i++;
$_icon = $_sub_cat->getIcon();
}
if( $i >= 1 ){
break; // as i only want the first one
}
}
// output
?>
<a href="<?php echo $_main_cat->getUrl() ?>">
<img src="<?php echo $_icon ?>">
</a>
<a href="<?php echo $_main_cat->getUrl() ?>">
<?php echo $_main_cat->getName() ?>
</a>
<?php
}
?>
This works but seems very bad practice to load the full category models in a foreach loop twice and when the site grows could take a long time.
What is the best way of doing the above performance wise?
What you could optimize is this:
$_category = Mage::getModel('catalog/category')->load($_main_cat->getId());
Here you are loading the category (again) from the database. Though you already loaded a collection, you should put all the categories you need in that collection with the right attributes.
Then instead of the code above, use this handful collection function to get the category entity:
$_category = $_main_categories->getItemById($_main_cat->getId());
//...
$_sub = $_main_categories->getItemById($_sub_cat->getId());
You should see a clear improvement in the performance, especially if you have a lot of categories.
Related
I have problem with getting attribute value in new_grid.phtml. If i make it like this:
<?php $_product = $block->getProduct()
echo $_product->getData('attr_name') ?>
Page generating to this place, and after just blank page.
Please show me other option, or maybe i have mistakes in my code.
Here is the place in new_grid.phtml:
<div class="product-img-block">
<a href="<?php /* #escapeNotVerified */ echo $block->getProductUrl($_item) ?>" class="product-item-photo">
<?php echo $block->getImage($_item, $image)->toHtml(); ?>
/* my code: */
<?php $_product = $block->getProduct() ?>
<img src="<?php echo $_product->getData('attr_name') ?>" />
</a>
it is code from custom_home.xml
<container name="header.container" as="header_container" label="Page Header Container" htmlTag="header" htmlClass="page-header" before="main.content"/>
<block class="Magento\Framework\View\Element\Template" name="test_file" template="Magento_Theme::html/front-slider.phtml" before="main.content"/>
<container name="main.content" htmlTag="main" htmlId="maincontent" htmlClass="page-main">
<container name="columns.top" label="Before Main Columns"/>
<container name="columns" htmlTag="div" htmlClass="columns">
<container name="main" label="Main Content Container" htmlTag="div" htmlClass="column main"/>
</container>
</container>
In front-slider.phtml i get the new_grid.phtml by code:
<?php echo $this->getLayout()->createBlock("Magento\Catalog\Block\Product\Widget\NewWidget")->setDisplayType("all_products")->setProductsCount("6")->setTemplate("product/widget/new/content/new_grid.phtml")->toHtml(); ?>
Errors for TBI answer:
1 exception(s):
Exception 0 (Magento\Framework\Exception\LocalizedException): Area code is already set
Exception 0 (Magento\Framework\Exception\LocalizedException): Area code is already set
0 /chroot/home/host/domain.com/html/app/design/frontend/central/central-theme/Magento_Catalog/templates/product/widget/new/content/new_grid.phtml(52): Magento\Framework\App\State->setAreaCode('frontend')
1 /chroot/home/host/domain.com/html/vendor/magento/framework/View/TemplateEngine/Php.php(59): include('/chroot/home/au...')
2 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Element/Template.php(255): Magento\Framework\View\TemplateEngine\Php->render(Object(Magento\Catalog\Block\Product\Widget\NewWidget\Interceptor), '/chroot/home/au...', Array)
3 /chroot/home/host/domain.com/html/var/generation/Magento/Catalog/Block/Product/Widget/NewWidget/Interceptor.php(518): Magento\Framework\View\Element\Template->fetchView('/chroot/home/au...')
4 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Element/Template.php(279): Magento\Catalog\Block\Product\Widget\NewWidget\Interceptor->fetchView('/chroot/home/au...')
5 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Element/AbstractBlock.php(659): Magento\Framework\View\Element\Template->_toHtml()
6 /chroot/home/host/domain.com/html/var/generation/Magento/Catalog/Block/Product/Widget/NewWidget/Interceptor.php(830): Magento\Framework\View\Element\AbstractBlock->toHtml()
7 /chroot/home/host/domain.com/html/app/design/frontend/central/central-theme/Magento_Theme/templates/html/front-slider.phtml(81): Magento\Catalog\Block\Product\Widget\NewWidget\Interceptor->toHtml()
8 /chroot/home/host/domain.com/html/vendor/magento/framework/View/TemplateEngine/Php.php(59): include('/chroot/home/au...')
9 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Element/Template.php(255): Magento\Framework\View\TemplateEngine\Php->render(Object(Magento\Framework\View\Element\Template), '/chroot/home/au...', Array)
10 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Element/Template.php(279): Magento\Framework\View\Element\Template->fetchView('/chroot/home/au...')
11 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Element/AbstractBlock.php(659): Magento\Framework\View\Element\Template->_toHtml()
12 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(542): Magento\Framework\View\Element\AbstractBlock->toHtml()
13 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(518): Magento\Framework\View\Layout->_renderBlock('test_file')
14 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(206): Magento\Framework\View\Layout->renderNonCachedElement('test_file')
15 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('test_file')
16 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(193): Magento\Framework\View\Layout->renderElement('test_file', true)
17 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(569): Magento\Framework\View\Layout\Interceptor->renderElement('test_file')
18 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(520): Magento\Framework\View\Layout->_renderContainer('page.wrapper')
19 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(206): Magento\Framework\View\Layout->renderNonCachedElement('page.wrapper')
20 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('page.wrapper')
21 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(193): Magento\Framework\View\Layout->renderElement('page.wrapper', true)
22 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(569): Magento\Framework\View\Layout\Interceptor->renderElement('page.wrapper')
23 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(520): Magento\Framework\View\Layout->_renderContainer('root')
24 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(206): Magento\Framework\View\Layout->renderNonCachedElement('root')
25 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(472): Magento\Framework\View\Layout\Interceptor->renderNonCachedElement('root')
26 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(193): Magento\Framework\View\Layout->renderElement('root', true)
27 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Layout.php(938): Magento\Framework\View\Layout\Interceptor->renderElement('root')
28 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Interceptor.php(146): Magento\Framework\View\Layout->getOutput()
29 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Layout/Interceptor.php(494): Magento\Framework\View\Layout\Interceptor->___callPlugins('getOutput', Array, Array)
30 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Result/Page.php(243): Magento\Framework\View\Layout\Interceptor->getOutput()
31 /chroot/home/host/domain.com/html/vendor/magento/framework/View/Result/Layout.php(164): Magento\Framework\View\Result\Page->render(Object(Magento\Framework\App\Response\Http\Interceptor))
32 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Interceptor.php(74): Magento\Framework\View\Result\Layout->renderResult(Object(Magento\Framework\App\Response\Http\Interceptor))
33 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Chain/Chain.php(70): Magento\Framework\View\Result\Page\Interceptor->___callParent('renderResult', Array)
34 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\Framewo...', 'renderResult', Object(Magento\Framework\View\Result\Page\Interceptor), Array, 'result-varnish-...')
35 /chroot/home/host/domain.com/html/vendor/magento/module-page-cache/Model/Controller/Result/VarnishPlugin.php(74): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain{closure}(Object(Magento\Framework\App\Response\Http\Interceptor))
36 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Chain/Chain.php(67): Magento\PageCache\Model\Controller\Result\VarnishPlugin->aroundRenderResult(Object(Magento\Framework\View\Result\Page\Interceptor), Object(Closure), Object(Magento\Framework\App\Response\Http\Interceptor))
37 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\Framewo...', 'renderResult', Object(Magento\Framework\View\Result\Page\Interceptor), Array, 'result-builtin-...')
38 /chroot/home/host/domain.com/html/vendor/magento/module-page-cache/Model/Controller/Result/BuiltinPlugin.php(67): Magento\Framework\View\Result\Page\Interceptor->Magento\Framework\Interception{closure}(Object(Magento\Framework\App\Response\Http\Interceptor))
39 /chroot/home/host/domain.com/html/vendor/magento/framework/Interception/Interceptor.php(142): Magento\PageCache\Model\Controller\Result\BuiltinPlugin->aroundRenderResult(Object(Magento\Framework\View\Result\Page\Interceptor), Object(Closure), Object(Magento\Framework\App\Response\Http\Interceptor))
40 /chroot/home/host/domain.com/html/var/generation/Magento/Framework/View/Result/Page/Interceptor.php(130): Magento\Framework\View\Result\Page\Interceptor->___callPlugins('renderResult', Array, Array)
41 /chroot/home/host/domain.com/html/vendor/magento/framework/App/Http.php(139): Magento\Framework\View\Result\Page\Interceptor->renderResult(Object(Magento\Framework\App\Response\Http\Interceptor))
42 /chroot/home/host/domain.com/html/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http->launch()
43 /chroot/home/host/domain.com/html/index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))
44 {main}
On this code:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$appState = $objectManager->get('\Magento\Framework\App\State');
$appState->setAreaCode('frontend');
$registry = $objectManager->get('\Magento\Framework\Registry');
$_product= $registry->registry('current_product');
$attribute_code = 'attr_name';
echo $_product->getResource()->getAttribute($attribute_code)-
>getFrontend()->getValue($_product);
Try adaptive this code to yours
#var $block \Magento\Catalog\Block\Product\Widget\NewWidget
if ($exist = ($block->getProductCollection()
&& $block->getProductCollection()->getSize()))
{
$items = $block->getProductCollection()->getItems();
}
<?php foreach ($items as $_item): ?>
<img src="<?php echo $_item->getData("nameofattribute") ?>" />
<?php endforeach ?>
If you dont know name of attribute you may use $_item->getData() and get all of list data for current item.
getResource()->getAttribute($attribute_code)->getFrontend()->getValue($_item);
?>
$_product = $block->getProduct();
print_r($_product->getData);
First you need to check for product data available or not if you got proper array then you just need to call product attribute from below code.
echo $_product->getResource()->getAttribute('ATTRIBUTE_CODE')->getFrontend()->getValue($_product);
$productModel = Mage::getModel('catalog/product');
$_product = $productModel->load(42); // Add your product ID
$attribute_code = 'attr_name'; // Add your attribute name
echo $_product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($_product);
As per your comment above please try this just change product id and attribute code as i have mention above
First, please verify if you have added your custom attribute in proper attribute set and assigned that attribute set to product for which you are adding this code and then finally use below code to get its value.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$appState = $objectManager->get('\Magento\Framework\App\State');
//$appState->setAreaCode('frontend');
$registry = $objectManager->get('\Magento\Framework\Registry');
$_product= $registry->registry('current_product');
$attribute_code = 'attr_name';
echo $_product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($_product);
Thanks
I feel product object not available in your code. Product object only on product view page in current product. On other page you have to create.
I struggled with a custom attribute in my own grid.phtml file. After a long time, I realised there is an attribute setting "use in product-isting" which was set to "No". Make sure this is set to yes.
We have a producfeed for Magento that is made in php and is making an .xml productfeed. In the image below you can see how we run the loop to get the products in the xml feed. We are also getting the categories with the following code:
//Prepare the loop
foreach($prodIds as $productId) {
$product->load($productId);
$product_data = array();
//Get product data
$product_data['title']=$product->getName();
$product_data['description']= strip_tags($product->getDescription());
$product_data['sku']=$product->getSku();
$product_data['color']=$product->getResource()->getAttribute('color')->getFrontend()->getValue($product);
if($product->getSpecialPrice())
$product_data['price']=number_format($product->getSpecialPrice(), 2);
else
$product_data['price']=number_format($product->getPrice(), 2);
$product_data['specialprice']=number_format($product->getSpecialPrice(), 2);
$product_data['old_price']=number_format($product->getPrice(), 2);
$product_data['availability']="Op voorraad";
$product_data['shippingcost']="6.95";
$product_data['delivery_time']="2 tot 5 werkdagen";
$product_data['link']=Mage::getBaseUrl().$product->getUrlPath();
$product_data['image_link']=Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'catalog/product'.$product->getImage();
$product_data['image_link_small']= Mage::helper('catalog/image')->init($product, 'small_image')->resize(200,200);
//Get feed category data
$childIds = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($product->getId());
// Echo run the loop
?>
<product>
<sku><![CDATA[<?php echo $product_data['sku'] ?>]]></sku>
<link><![CDATA[<?php echo $product_data['link'].$source ?>]]></link>
<title><![CDATA[<?php echo $product_data['title'] ?>]]></title>
<description><![CDATA[<?php echo $product_data['description'] ?>]]></description>
<image_link><![CDATA[<?php echo $product_data['image_link'] ?>]]></image_link>
<image_link_small><![CDATA[<?php echo $product_data['image_link_small'] ?>]]></image_link_small>
<price><![CDATA[<?php echo $product_data['price'] ?>]]></price>
<old_price><![CDATA[<?php echo $product_data['old_price'] ?>]]></old_price>
<availability><![CDATA[<?php echo $product_data['availability'] ?>]]></availability>
<delivery_time><![CDATA[<?php echo $product_data['delivery_time'] ?>]]></delivery_time>
<id><![CDATA[<?php echo $product_data['sku'] ?>]]></id>
<delivery_costs><![CDATA[<?php echo $product_data['shippingcost'] ?>]]></delivery_costs>
<special_price><![CDATA[<?php echo $product_data['specialprice'] ?>]]></special_price>
<categories><?php $categoryCollection = $product->getCategoryCollection()->addAttributeToSelect('name');?><?php foreach($categoryCollection as $cat): ?><subcat><?php echo htmlentities($cat->getName()); ?></subcat><?php endforeach; ?></categories>
</product>
<?php } //End of loop ?>
</products>
Now we want to make a difference between the main categories and the subcategories. With the code above we get a list with all categories and it should be two fields, one with main categories and one with subcategories.
Can someone help me modify the code?
You can check if a category is top category by it's level.
if ($category->getLevel() == 2){
//is top category
}
elseif ($category->getLevel() > 2){
//is subcategory
}
and next time post the code, not the screenshot. What is this, a question for ants? :)
I need to show on the product page (frontend) that the current item is for backorder ONLY and is not in stock.
I have at the moment those in stock showing qty of what is available and those products on backorder doesn't show anything.
Does anyone know a code I can put in the view.phtml file that will ONLY show a message on those products set as backorder?
Thanks!
Simon.
To do this make sure you have enabled backorders from inventory tab.
If you are on product page then first of all retrieve product qty.
<?php
$inventory = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
if( (int)$inventory->getQty() == 0 && $inventory->getBackorders() )
{
// No Backorders => getBackorders() = 0
// Allow Qty Below 0 => getBackorders() = 1
// Allow Qty Below 0 and Notify Customer => getBackorders() = 2
echo "display your backordedr message";
}
?>
You can also put this code in
app\design\frontend\base\default\template\catalog\product\view\type\default.phtml file where availability message of product come from.
Here is the code that you need to add in view.phtml. This will show the backorder message:
$inventory = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
$inv_qty = (int)$inventory->getQty();
if($inventory->getBackorders() >= 0 && $inv_qty == 0)
{
echo "Your backorder message goes here";
}
Hey guys i got a little dilema. I am running Magento ver. 1.7.0.2
When you create a configurable product you must set the stock "in stock" and then you add the other additional products with different stock.
When the additional products stock goes to 0 the main configurable product still is "in stock".
I want that when all the additional products stock goes to 0 then the main configurable product stock to turn into "out of stock".
I am using this custom code for "out of stock" products to appear always at the bottom page. And unless the configurable product dont receive the option "out of stock" it cant go to bottom page.
$this->getSelect()->joinLeft(array('_inventory_table'=>$this->getTable('cataloginventory/stock_item')),"_inventory_table.product_id = e.entity_id",array('is_in_stock', 'manage_stock'));
$this->addExpressionAttributeToSelect('on_top','(CASE WHEN (((_inventory_table.use_config_manage_stock = 1) AND (_inventory_table.is_in_stock = 1)) OR ((_inventory_table.use_config_manage_stock = 0) AND (1 - _inventory_table.manage_stock + _inventory_table.is_in_stock >= 1))) THEN 1 ELSE 0 END)',array());
$this->getSelect()->order('on_top DESC');
Have you made sure you have the correct Magento settings?
Inventory "Show Out Of Stock" = "No"
Configurable product Manage Stock = "No"
Simple product /Manage Stock = "Yes"
I don't know if this would work for you but I think you can solve this from the template itself on the file "app/base/default/template/catalog/product/view.phtml", the line of code that says:
<?php if ($_product->isSaleable() && $this->hasOptions()):?>
<?php echo $this->getChildChildHtml('container2', '', true, true) ?>
<? else : ?>
enter code here
<?php endif ?>
if you base it from the original file in the base template you may see this code starting from line 100 as you can see if all the options of the configurable product is empty it means that all products are already sold out causing it not to display the form fields necessary for adding it to the cart.
I hope this helps. :)
Add this code
$this->getSelect()->joinLeft(
array('_inventory_table'=>$this->getTable('cataloginventory/stock_item')),
"_inventory_table.product_id = e.entity_id",
array('is_in_stock', 'manage_stock')
);
$this->addExpressionAttributeToSelect('on_top',
'(CASE WHEN (((_inventory_table.use_config_manage_stock = 1) AND (_inventory_table.is_in_stock = 1)) OR ((_inventory_table.use_config_manage_stock = 0) AND (1 - _inventory_table.manage_stock + _inventory_table.is_in_stock >= 1))) THEN 1 ELSE 0 END)',
array());
$this->getSelect()->order('on_top DESC');
before line:
if ($attribute == 'price' && $storeId != 0) {
in files:
app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Collection.php
or in app/code/core/Mage/Catalog/Model/Product/Collection.php if you have Magento 1.7.0.0 +
I am unable to echo the number of my products reviews on one of my tabs.
Now the title of my reviews tab is Product’s Reviews and I would like to change it to Reviews(0)
I need to have a number of reviews in the brackets.
How to echo the nuber of reviews from this product?
FYI this is NOT working for me:
<?php echo $this->__(\'%d Review(s)\', $this->getReviewsCount()) ?>
Try the following code ( the code taken from Inchoo.com site ) :
// Get product review info (independent) of review page
<?php
$storeId = Mage::app()->getStore()->getId();
$summaryData = Mage::getModel('review/review_summary')
->setStoreId($storeId)
->load($_product->getId());
/* #var $summaryData Mage_Review_Model_Review_Summary */
/*
array(
['primary_id'] => 147
['entity_pk_value'] => 166
['entity_type'] => 1
['reviews_count'] => 1
['rating_summary'] => 80
['store_id'] => 1
)
*/
?>
and Echo this as <?php echo $summaryData['reviews_count']; ?>
$reviewsCount = Mage::getModel('review/review')
->getTotalReviews($product_id, true, Mage::app()->getStore()->getId());
i followed the code from Mage_Rating_Block_Entity_Detailed
as the second argument is set true in getTotalReviews so it will only get count of approved reviews.
i have found that the review summary count and ratings are taken from the code ...
$product=166;//demo product id
$storeId=Mage::app()->getStore()->getId();
$product=Mage::getModel('catalog/product')->load($product_id);
$product_review=Mage::getModel('review/review_summary')
->setStoreId($storeId)
->load($product_id);
echo $val->getReviewsCount();
echo $val->getRatingSummary();
Please try very simple code It's work for me.
<?php echo $this->getReviewsSummaryHtml($_product, 'short')?>
It's look like attached screenshot.