Skip to main content

Posts

Showing posts with the label Magento2

Magento 2: get child product of current configurable product

<?php      $objectManager = \Magento\Framework\App\ObjectManager::getInstance();     $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product     $productTypeInstance = $product->getTypeInstance();     $usedProducts = $productTypeInstance->getUsedProducts($product);     echo $product->getId(); //Main configurable product ID     echo $product->getName(); //Main Configurable Name     foreach ($usedProducts  as $child) {         echo $child->getId()."</br>"; //Child Product Id          echo $child->getName()."</br>"; //Child Product Name     }?>

Magento 2: Putty Commands

cd - change directory cd ..    - come out of one directory pwd        -    Path of my current directory wget - get downloadable data unzip filename.zip -d /pictures  - for unzip data and -d for directory unzip -l filename.zip  -  list all files from a .zip file unzip filename.zip filename.txt  -- extract / unzip certain file from a .zip file zip -r filename *  -   zip up an entire directory including all sub-directories zip -r example2.zip *  -  zip all files in current directory rm -rf *    -    delete all files in directory rm -rf foldername/  -  To delete a whole folder and its content mysql -u user_name -p database_name< advomate_pompusalive.sql  ------ for import DB mysqldump -u username -p  database_to_be_exported > db_file_name.sql  ------   for export DB yum install zip unzip -y...

Magento 2: CRUD

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager $resource = $objectManager->get('Magento\Framework\App\ResourceConnection'); $connection = $resource->getConnection(); $tableName = $resource->getTableName('employee'); //gives table name with prefix //Select Data from table $sql = "Select * FROM " . $tableName; $result = $connection->fetchAll($sql); // gives associated array, table fields as key in array. //Delete Data from table $sql = "Delete FROM " . $tableName." Where emp_id = 10"; $connection->query($sql); //Insert Data into table $sql = "Insert Into " . $tableName . " (emp_id, emp_name, emp_code, emp_salary) Values ('','XYZ','ABD20','50000')"; $connection->query($sql); //Update Data into table $sql = "Update " . $tableName . "Set emp_salary = 20000 where emp_id = 12"; $connecti...

Magento 2: Adding new category attributes in Magento 2

 Text attribute in category To create new module in Magento 2 we need the following files: app/code/Atwix/CategoryAttribute/etc/ module . xml app/code/Atwix/CategoryAttribute/registration.php app/code/Atwix/CategoryAttribute/composer.json <? xml version= "1.0" ?> <!-- file: app/code/Atwix/CategoryAttribute/etc/module.xml --> < config xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation = "urn:magento:framework:Module/etc/module.xsd" > < module name = "Atwix_CategoryAttribute" setup_version = "1.0.0" /> </ config > <?php /* file: app/code/Atwix/CategoryAttribute/registration.php */ \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Atwix_CategoryAttribute' , __DIR__ ); { "name" : "atwix/categoryattribute" , "description" : ...

Magento2: How to install custom theme in magento 2

magento2 | _ app | _ design | _ frontend | _ Custom | _theme | _Magento_Theme | _templates | _root . phtml - Copy of Luma | _registration . php | _theme . xml     Your path for registration.php is app\design\frontend\Custom\theme\registration.php   <? php \Magento\Framework\Component\ComponentRegistrar :: register ( \Magento\Framework\Component\ComponentRegistrar :: THEME , 'frontend/Custom/theme' , __DIR__ );       app\design\frontend\Custom\theme\theme . xml   <theme xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation = "urn:magento:framework:Config/etc/theme.xsd" > <title> Custom Theme </title> <parent> Magento/luma </parent> <media> <preview_image> media/preview.jpg </preview_image> </media...

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...