Friday, 18 November 2016

Magento add toolbar and pager to custom product collection

Magento add toolbar and pager to custom product collection

Create toolbar object like:-

$toolbar = Mage::getBlockSingleton("catalog/product_list")->getToolbarBlock();

Then Assign product collection to toolbar object :-

$toolbar->setCollection($_productCollection);
$layout = Mage::getSingleton("core/layout");

Then set pager to toolbar as a child for show with pagination:-

$pager = $layout->createBlock("page/html_pager");

$toolbar->setChild("product_list_toolbar_pager", $pager);
echo $toolbar->toHtml();  

Wednesday, 2 November 2016

How to place top menu to left side bar in magento 2

<referenceContainer name="sidebar.main">
<block class="Magento\Framework\View\Element\Template" name="store.menu" group="navigation-sections" template="Magento_Theme::html/container.phtml">
<arguments>
<argument name="title" translate="true" xsi:type="string">Menu</argument>
</arguments>
</block>
</referenceContainer>

Magento call newsletter in sidebar

Add below code in <body> tag in module-newsletter/view/frontend/layout/default.xml :-


You can use sidebar.main instead of sidebar.additional  for show in top of sidebar :-

<referenceContainer name="sidebar.additional">
            <block class="Magento\Newsletter\Block\Subscribe" name="form.subscribe" as="subscribe" after="wishlist_sidebar" template="subscribe.phtml"/>
        </referenceContainer>

Magento redirect to subdomain on mobile view

<script type="text/javascript">
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
  window.location="http://m.hostname.com";
}
</script>

magento get stock information of product

$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);

You can check stock data in this way:-

echo "<pre>"; print_r($stock->getData()); echo "</pre>";

Or, you can print individually like this:-

echo $stock->getQty();
echo $stock->getMinQty();
echo $stock->getMinSaleQty();

Monday, 18 July 2016

Get all parent category ids and self id in an array

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
   $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
  $current_cat = $category->getId(); // current category id

  $parent_cat =  $category->getParentCategory()->getId(); // parent category id
  foreach ($category->getParentCategories() as $parent) {
    $catids[] = $parent->getId(); // show all parent cat ids and self id in an array
}
if($catids[0]==parent_cat_id){
echo "show parent cat custom code";

}

magento 2 how to get parent category id current category

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
   $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
  $current_cat = $category->getId(); // current category id

  $parent_cat =  $category->getParentCategory()->getId(); // parent category id