May 18, 2016 11:56 am
image_round

Magento add hidden Product attribute

Magento add hidden Product attribute

$setup->addAttribute('catalog_product', 'seller_name', array(
  'group'     	=> 'Meta Information',
  'input'         => 'hidden',
    'type' 			=> 'text',
    'label'         => 'Seller Name',
  'backend'       => '',
  'visible'       => 0,
  'required'		=> 0,
  'user_defined' => 1,
  'searchable' => 1,
  'filterable' => 0,
  'comparable'	=> 0,
  'visible_on_front' => 1,
  'visible_in_advanced_search'  => 0,
  'is_html_allowed_on_front' => 0,
    'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));

 

Magento add hidden Product attribute

Magento add hidden Product attribute

$setup->addAttribute('catalog_product', 'seller_name', array(
  'group'     	=> 'Meta Information',
  'input'         => 'hidden',
    'type' 			=> 'text',
    'label'         => 'Seller Name',
  'backend'       => '',
  'visible'       => 0,
  'required'		=> 0,
  'user_defined' => 1,
  'searchable' => 1,
  'filterable' => 0,
  'comparable'	=> 0,
  'visible_on_front' => 1,
  'visible_in_advanced_search'  => 0,
  'is_html_allowed_on_front' => 0,
    'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));

 

May 18, 2016 6:50 am
image_round

Magento2 How to load product collection by id

I got product collection in my custom template using following code

<?php 
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');

foreach ($productCollection as $product) {
           $product_id=$product['entity_id'];
           echo $_product->getName();
}?>

Magento2 How to load product collection by id

I got product collection in my custom template using following code

<?php 
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');

foreach ($productCollection as $product) {
           $product_id=$product['entity_id'];
           echo $_product->getName();
}?>
May 17, 2016 9:36 am
image_round

Magento get just one column value or get multiple column values by name

Magento get just one column value or get multiple column values by name

Only want a particular field?
 
print_r($collection->getColumnValues('name'));
The team at Magento have even given us some rudimentary filtering capabilities.
 
print_r($collection->getItemsByColumnValue('name','fieldname'));

 

Magento get just one column value or get multiple column values by name

Magento get just one column value or get multiple column values by name

Only want a particular field?
 
print_r($collection->getColumnValues('name'));
The team at Magento have even given us some rudimentary filtering capabilities.
 
print_r($collection->getItemsByColumnValue('name','fieldname'));

 

May 16, 2016 5:36 am
image_round

Magento 2: indexing using Command prompt

f we need to do whole index management in the site we will run the following command in project
In root magento directory

php bin/magento indexer:reindex

in root/bin directory

php magento indexer:reindex

Execute the command from command line and be happy. Indexing will work perfectly.

Magento 2: indexing using Command prompt

f we need to do whole index management in the site we will run the following command in project
In root magento directory

php bin/magento indexer:reindex

in root/bin directory

php magento indexer:reindex

Execute the command from command line and be happy. Indexing will work perfectly.

May 14, 2016 6:03 am
image_round

Magento 2 : How to get formatted price with currency

Magento 2 : How to get formatted price with currency

<?php
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of Object Manager
    $priceHelper = $objectManager->create('Magento\Framework\Pricing\Helper\Data'); // Instance of Pricing Helper
    $price =  1000; //Your Price
    $formattedPrice = $priceHelper->currency($price, true, false);
?>

 

Magento 2 : How to get formatted price with currency

Magento 2 : How to get formatted price with currency

<?php
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of Object Manager
    $priceHelper = $objectManager->create('Magento\Framework\Pricing\Helper\Data'); // Instance of Pricing Helper
    $price =  1000; //Your Price
    $formattedPrice = $priceHelper->currency($price, true, false);
?>

 

May 10, 2016 4:13 pm
image_round

Magento 2 : How to get current category from registry

Magento 2 : How to get current category from registry

<?php

$registry = $objectManager->get('Magento\Framework\Registry');
$category = $objectManager->create('Magento\Catalog\Model\Category')->load($registry->registry('current_category')->getId());

?>

OR

<?php 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
    echo $category->getId();
    echo $category->getName();
?>

 

Magento 2 : How to get current category from registry

Magento 2 : How to get current category from registry

<?php

$registry = $objectManager->get('Magento\Framework\Registry');
$category = $objectManager->create('Magento\Catalog\Model\Category')->load($registry->registry('current_category')->getId());

?>

OR

<?php 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
    echo $category->getId();
    echo $category->getName();
?>

 

May 8, 2016 12:33 pm
image_round

Magento 2 : translation steps

step 1: get the translation texts

php bin/magento i18n:collect-phrases --output=zh_Hans_CN.csv --magento .

Step 2: on root folder there must be translation file of this name

Step 3: Create folders

mkdir app/i18n
mkdir app/i18n/ngage/
mkdir app/i18n/ngage/zh_Hans_CN/

Step 4 :Create the file composer.json with the following contents:

{
 "name": "test/nl_nl",
   "description": "Chinese Language",
   "version": "100.0.1",
   "license": [
     "OSL-3.0",
     "AFL-3.0"
   ],
   "require": {
     "magento/framework": "100.0.*"
   },
   "type": "magento2-language",
   "autoload": {
     "files": [
       "registration.php"
     ]
   }
   ,"extra": {"map": "*", "ngage/zh_Hans_CN"}
 }

Stpe 5 : Create the file language.xml with the following contents:

<?xml version="1.0"?>
<language xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/Language/package.xsd">
  zh_Hans_CN
  <vendor>ngage</vendor>
  <package>zh_hans_cn</package>
</language>

Step 6: Create the file registration.php with the following content:

<?php
 /**
  * Copyright © 2015 Magento. All rights reserved.
  * See COPYING.txt for license details.
  */  
 \Magento\Framework\Component\ComponentRegistrar::register(
     \Magento\Framework\Component\ComponentRegistrar::LANGUAGE,
     'ngage_zh_hans_cn',
     __DIR__
 );

Step 7: deploy setup

php bin/magento setup:static-content:deploy
php bin/magento setup:static-content:deploy nl_NL
php bin/magento indexer:reindex
php bin/magento cache:clean
php bin/magento cache:flush

 

 

Magento 2 : translation steps

step 1: get the translation texts

php bin/magento i18n:collect-phrases --output=zh_Hans_CN.csv --magento .

Step 2: on root folder there must be translation file of this name

Step 3: Create folders

mkdir app/i18n
mkdir app/i18n/ngage/
mkdir app/i18n/ngage/zh_Hans_CN/

Step 4 :Create the file composer.json with the following contents:

{
 "name": "test/nl_nl",
   "description": "Chinese Language",
   "version": "100.0.1",
   "license": [
     "OSL-3.0",
     "AFL-3.0"
   ],
   "require": {
     "magento/framework": "100.0.*"
   },
   "type": "magento2-language",
   "autoload": {
     "files": [
       "registration.php"
     ]
   }
   ,"extra": {"map": "*", "ngage/zh_Hans_CN"}
 }

Stpe 5 : Create the file language.xml with the following contents:

<?xml version="1.0"?>
<language xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/Language/package.xsd">
  zh_Hans_CN
  <vendor>ngage</vendor>
  <package>zh_hans_cn</package>
</language>

Step 6: Create the file registration.php with the following content:

<?php
 /**
  * Copyright © 2015 Magento. All rights reserved.
  * See COPYING.txt for license details.
  */  
 \Magento\Framework\Component\ComponentRegistrar::register(
     \Magento\Framework\Component\ComponentRegistrar::LANGUAGE,
     'ngage_zh_hans_cn',
     __DIR__
 );

Step 7: deploy setup

php bin/magento setup:static-content:deploy
php bin/magento setup:static-content:deploy nl_NL
php bin/magento indexer:reindex
php bin/magento cache:clean
php bin/magento cache:flush

 

 

May 7, 2016 11:35 am
image_round

Magento 2 file permission

File system permissions and ownership

Use the following steps:

  1. If you haven’t already done so, log in to your Magento server as, or switch to, the Magento file system owner.
  2. Change to the Magento installation directory:
    cd <web server docroot>/<magento2 base dir>

    The base directory is typically a subdirectory named magento2 under your web server’s docroot. Need help locating the docroot? Click here.

    Examples:

    Ubuntu: /var/www/magento2
    CentOS: /var/www/html/magento2

     

  3. Set ownership:
    chown -R :<your web server group name> .

     
    Typical examples:

    CentOS: chown -R :apache .
    Ubuntu: chown -R :www-data .

     

  4. Set permissions:
    find . -type d -exec chmod 770 {} \; && find . -type f -exec chmod 660 {} \; && chmod u+x bin/magento

     
    If you must enter the commands as sudo, use:

    sudo find . -type d -exec chmod 770 {} \; && sudo find . -type f -exec chmod 660 {} \; && sudo chmod u+x bin/magento

     

Magento 2 file permission

File system permissions and ownership

Use the following steps:

  1. If you haven’t already done so, log in to your Magento server as, or switch to, the Magento file system owner.
  2. Change to the Magento installation directory:
    cd <web server docroot>/<magento2 base dir>

    The base directory is typically a subdirectory named magento2 under your web server’s docroot. Need help locating the docroot? Click here.

    Examples:

    Ubuntu: /var/www/magento2
    CentOS: /var/www/html/magento2

     

  3. Set ownership:
    chown -R :<your web server group name> .

     
    Typical examples:

    CentOS: chown -R :apache .
    Ubuntu: chown -R :www-data .

     

  4. Set permissions:
    find . -type d -exec chmod 770 {} \; && find . -type f -exec chmod 660 {} \; && chmod u+x bin/magento

     
    If you must enter the commands as sudo, use:

    sudo find . -type d -exec chmod 770 {} \; && sudo find . -type f -exec chmod 660 {} \; && sudo chmod u+x bin/magento

     

May 7, 2016 9:35 am
image_round

Clear Theme/CSS Cache in Magento 2

Step 1: Clear pub folder and cache

rm -rf pub/static/frontend/* var/view_preprocessed/ var/*cache/*

Step 2: Deploy files to pub folder

php bin/magento setup:static-content:deploy

 

Clear Theme/CSS Cache in Magento 2

Step 1: Clear pub folder and cache

rm -rf pub/static/frontend/* var/view_preprocessed/ var/*cache/*

Step 2: Deploy files to pub folder

php bin/magento setup:static-content:deploy

 

May 6, 2016 10:32 am
image_round

Magento 2 add static block in layout

Magento 2 add static block to layout XML

<referenceContainer name="footer-container">
      <block class="Magento\Cms\Block\Block" name="footerstaticblock" after="-" >
        <arguments>
          <argument name="block_id" xsi:type="string">footerstaticblock</argument>
        </arguments>
      </block>
    </referenceContainer>

Magento 2 add static block to TEMPLATE

<?php echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_identifier')->toHtml();?>

Magento 2 add static block to CMS

{{block class="Magento\\Cms\\Block\\Block" block_id="block_identifier"}}

 

Magento 2 add static block in layout

Magento 2 add static block to layout XML

<referenceContainer name="footer-container">
      <block class="Magento\Cms\Block\Block" name="footerstaticblock" after="-" >
        <arguments>
          <argument name="block_id" xsi:type="string">footerstaticblock</argument>
        </arguments>
      </block>
    </referenceContainer>

Magento 2 add static block to TEMPLATE

<?php echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_identifier')->toHtml();?>

Magento 2 add static block to CMS

{{block class="Magento\\Cms\\Block\\Block" block_id="block_identifier"}}