Skip to main content

Posts

Showing posts from March, 2018

Magento 2: Get account Login-Logout buttons

<? php $objectManagerlogin = \Magento\Framework\App\ObjectManager :: getInstance (); $customerSession = $objectManagerlogin -> get ( 'Magento\Customer\Model\Session' ); $baseurl = $objectManagerlogin -> get ( 'Magento\Store\Model\StoreManagerInterface' )-> getStore ( 0 )-> getBaseUrl (); ?> <? php if ( $customerSession -> isLoggedIn ()) { ?> <a href=" <? php echo $baseurl . 'customer/account/logout' ; ?> ">LOGOUT </a> <? php } else { ?> <a href=" <? php echo $baseurl . 'customer/account/login' ; ?> ">LOGIN </a> <? php } ?>

Magento 2: Get Products by category ID

<?php $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();        // $appState = $objectManager->get('\Magento\Framework\App\State'); // $appState->setAreaCode('frontend'); $categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory'); $categoryHelper = $objectManager->get('\Magento\Catalog\Helper\Category'); $categoryRepository = $objectManager->get('\Magento\Catalog\Model\CategoryRepository'); $store = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore(); $categoryId = 47; // YOUR CATEGORY ID $category = $categoryFactory->create()->load($categoryId); $categoryProducts = $category->getProductCollection()                              ->addAttributeToSelect('*');       ...

Magento 2: Category list for custom magento system configuration section ( Backend )

In system.xml file field for multi select of category is like: NOTE: Use Select for Single item and multiselect for multiple in - <field id = "bannerlist" translate = "label" type = " multiselect " <group id = "bannerblock_setting" translate = "label" type = "text" delault = "1" sortOrder = "3" showInDefault = "1" showInWebsite = "1" showInStore = "1" > <label> Setting </label> <field id = "bannerlist" translate = "label" type = "multiselect" sortOrder = "10" showInDefault = "1" showInWebsite = "1" showInStore = "1" > <label> Select Category </label> <!-- <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>--> <source_model> Ipragmatech\Bannerblock\Model\Config\Source\C...

Magento 2: How to Get Category Collection in Magento 2?

Sample File Path: app/code/YourCompanyName/YourModuleName/Block/YourCustomBlock.php <?php namespace YourCompanyName\YourModuleName\Block; class YourCustomBlock extends \Magento\Framework\View\Element\Template {      protected $_categoryCollectionFactory ;        protected $_categoryHelper ;          public function __construct(          \Magento\Framework\View\Element\Template\Context $context ,          \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory ,          \Magento\Catalog\Helper\Category $categoryHelper ,          array $data = []      ) {          $this ->_categoryCollectionFactory = $categoryCollec...

Magento2- Display Static Block in Phtml file & CMS page

In XML File: <referenceContainer name="content"> <block class="Magento\Cms\Block\Block" name="block_identifier"> <arguments> <argument name="block_id" xsi:type="string">block_identifier</argument> </arguments> </block> </referenceContainer>         In Phtml File: <?php echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_identifier')->toHtml();?>   <?php echo $this->getLayout()->createBlock("Magento\Newsletter\Block\Subscribe")->setTemplate("Magento_Newsletter::subscribe.phtml")->toHtml();?>     in CMS Content: {{block class="Magento\\Cms\\Block\\Block" block_id="block_identifier"}}

Magento 2: Add new tab in Product detail page

New tab in product detail page is very easy task with product attribute. Use below steps and check it out. Step 1. Create Product Attribute “video”. Step 2. Create file “ catalog_product_view.xml ” in the app/design/frontend/{vender name}/{theme name}/Magento_Catalog/layout In the file write the below code: <!-- this is code to add new tab start --> <referenceBlock name="product.info.details"> <block class="Magento\Catalog\Block\Product\View\Description" name="product.info.video" template="product/view/attribute.phtml" group="detailed_info"> <arguments> <argument name="at_call" xsi:type="string">getVideo</argument> <argument name="at_code" xsi:type="string">video</argument> <argument name="css_class" xsi:type="string">video</argument> <argument name="at_label" xsi:type="string">non...

Magento 2: How to Get sub category of current category in Magento2

<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category'); //Get Current Category $subcats = $category->getChildrenCategories(); $_helper = $this->helper('Magento\Catalog\Helper\Output'); ?> <ul class="sub-cat"> <?php foreach ($subcats as $subcat) { $_category = $objectManager->create('Magento\Catalog\Model\Category')->load($subcat->getId()); $_outputhelper = $this->helper('Magento\Catalog\Helper\Output'); $subcaturl = $subcat->getUrl(); $_imgHtml = ''; if ($_imgUrl = $_category->getImageUrl()) { $_imgHtml = '<img src="' . $_imgUrl . '" />'; $_imgHtml = $_outputhelper->categoryAttribute($_category, $_imgHtml, 'image'); /* @escapeNotVerified */} ?> <li> <div class="cat-image"> <a href="< ?php echo $...

Magento 2: How to Get Subcategories of Specific Parent Category

<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $catId = 47; // Parent Category ID $subcategory = $objectManager->create('Magento\Catalog\Model\Category')->load($catId); $subcats = $subcategory->getChildrenCategories(); $_helper = $this->helper('Magento\Catalog\Helper\Output'); ?> <ul class="sub-cat"> <?php foreach ($subcats as $subcat) { $_category = $objectManager->create('Magento\Catalog\Model\Category')->load($subcat->getId()); $_outputhelper = $this->helper('Magento\Catalog\Helper\Output'); $subcaturl = $subcat->getUrl(); $_imgHtml = ''; if ($_imgUrl = $_category->getImageUrl()) { $_imgHtml = '<img src="' . $_imgUrl . '" />'; $_imgHtml = $_outputhelper->categoryAttribute($_category, $_imgHtml, 'image'); /* @escapeNotVerified */ } ?> <li> <div class="cat-image"> <a hre...

Magento 2: Get product by product id

    // get product by product id           <?php             $productId = 1;            $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();            $currentproduct = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);            echo $currentproduct->getName();         ?>  //  Get current product data <?php     $objectManager = \Magento\Framework\App\ObjectManager::getInstance();     $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product     echo $product->getId();     echo $product->getName();     echo $product-...

Important links for magento 2

1)    https://magecomp.com/blog/add-custom-file-upload-control-magento-2/   2)    https://blog.landofcoder.com/magento-2-install-extension/ https://magenticians.com/create-custom-theme-magento-2/ http://unitedwebsoft.in/blog/complete-magento2-theme-development-html-to-magento2-theme/

Magento 2: Remove refrence containor or get custom templates

<referenceContainer name="footer-container" remove="true"> <?php echo $this->getLayout()->createBlock("Magento\Newsletter\Block\Subscribe")->setTemplate("Magento_Newsletter::subscribe.phtml")->toHtml();?>                <?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magento_Theme::html/test.phtml")->toHtml();?> <block class="Magento\Newsletter\Block\Subscribe" name="form.subscribe" as="subscribe" before="-" template="subscribe.phtml"/>

Magento 2: Get site Logo Url

 $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();                                                 $logo = $objectManager->get('\Magento\Theme\Block\Html\Header\Logo');                     echo $logo->getLogoSrc() . '<br />';                     echo $logo->getLogoAlt() . '<br />';                     echo $logo->getLogoWidth() . '<br />';                     echo $logo->getLogoHeight() . '<br />'; <?php $storeName = $block->getThemeN...

upgrade magento 2

bin/magento maintenance:enable composer require magento/product-community-edition 2.2.0 --no-update composer update rm -rf var/cache/* rm -rf var/page_cache/* rm -rf var/generation/* chmod +x bin/magento bin/magento setup:upgrade  php bin/magento setup:di:compile bin/magento maintenance:disable For further upgrades, change the version number: composer require magento/product-community-edition 2.2.2 --no-update rm -rf pub/static var/cache var/page_cache rm -rf var/cache rm -rf var/page_cache rm -rf var/view_preprocessed  php bin/magento cache:clean   php bin/magento cache:flush php bin/magento setup:static-content:deploy -f php bin/magento module:status      -- to check activated modules Step 1: Access Your Server via SSH You need to connect your server via SSH. Step 2: Navigate to the Magento 2 Root Directory Once your SSH connection is up, you will need to move to your target application folder. In that application folder, you will find the publ...

Magento 2: include css/script files

 <css src="https://use.fontawesome.com/releases/v5.0.8/css/all.css" src_type="url"/>                    <css src="css/flexslider.css"/>          <css src="css/custom.css" media="all"/>         <script src="js/jquery-2.2.2.min.js"/>         <script src="js/custom.js"/> <script type="text/javascript" src="<?php echo $this->getViewFileUrl('js/jquery.superslides.js'); ?>"></script>    <script  type="text/javascript">     require([         "jquery"     ], function($){             $('#slides').superslides({                 hashchange: false ...