May 23, 2016 5:02 pm
image_round

Magento 2: How to delete all cached and generated files

Magento 2: How to delete all cached and generated files

The script need to be executed in Magento 2 root folder.

It deletes all the contents of var subfolder except var/session subfolder and except the.htaccess file

It deletes all the contents of pub/static subfolder except the.htaccess file

find var/* -type f -or -type d | grep -v 'session' | xargs rm -rf && \
rm -rf pub/static/*

Magento 2: How to delete all cached and generated files

Magento 2: How to delete all cached and generated files

The script need to be executed in Magento 2 root folder.

It deletes all the contents of var subfolder except var/session subfolder and except the.htaccess file

It deletes all the contents of pub/static subfolder except the.htaccess file

find var/* -type f -or -type d | grep -v 'session' | xargs rm -rf && \
rm -rf pub/static/*
May 18, 2016 1:50 pm
image_round

Magento 2 : how to get Product select attribute option value on product page

Step 1: Add Block for an attribute on catalog_product_view.xml

<block class="Magento\Catalog\Block\Product\View\Description" name="page.main.brand" template="Magento_Catalog::product/view/brand.phtml">
      	<arguments>
        <argument name="at_call" xsi:type="string">getBrand</argument>
        <argument name="at_code" xsi:type="string">brand</argument>
        <argument name="css_class" xsi:type="string">brand</argument>
        <argument name="at_label" xsi:type="string">default</argument>
        <argument name="add_attribute" xsi:type="string">itemprop="brand"</argument>
      </arguments>
      </block>

 

Step 2: move it to your location

<move element="page.main.brand" destination="product.info.main" after="page.main.title"/>

Step 3 : udpate phtml file with your custom html and code

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/**
 * Product description template
 *
 * @var $block \Magento\Catalog\Block\Product\View\Description
 */
?>
<?php
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_product = $block->getProduct();
 
$_code = $block->getAtCode();
$_className = $block->getCssClass();
$_attributeLabel = $block->getAtLabel();
$_attributeType = $block->getAtType();
$_attributeAddAttribute = $block->getAddAttribute();
 
if ($_attributeLabel && $_attributeLabel == 'default') {
$_attributeLabel = $_product->getResource()->getAttribute($_code)->getFrontendLabel();
}
 
$_attributeValue =$_product->getResource()->getAttribute($_code)->getFrontend()->getValue($_product);
 
?>
 
<?php if ($_attributeValue): ?>
<div class="product attibute <?php echo $_className?>">
<?php /*?><?php if ($_attributeLabel != 'none'): ?><strong class="type"><?php echo $_attributeLabel?></strong><?php endif; ?><?php */?>
<div class="value" <?php echo $_attributeAddAttribute;?>><?php echo $_attributeValue; ?></div>
</div>
<?php endif; ?>

Magento 2 : how to get Product select attribute option value on product page

Step 1: Add Block for an attribute on catalog_product_view.xml

<block class="Magento\Catalog\Block\Product\View\Description" name="page.main.brand" template="Magento_Catalog::product/view/brand.phtml">
      	<arguments>
        <argument name="at_call" xsi:type="string">getBrand</argument>
        <argument name="at_code" xsi:type="string">brand</argument>
        <argument name="css_class" xsi:type="string">brand</argument>
        <argument name="at_label" xsi:type="string">default</argument>
        <argument name="add_attribute" xsi:type="string">itemprop="brand"</argument>
      </arguments>
      </block>

 

Step 2: move it to your location

<move element="page.main.brand" destination="product.info.main" after="page.main.title"/>

Step 3 : udpate phtml file with your custom html and code

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/**
 * Product description template
 *
 * @var $block \Magento\Catalog\Block\Product\View\Description
 */
?>
<?php
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_product = $block->getProduct();
 
$_code = $block->getAtCode();
$_className = $block->getCssClass();
$_attributeLabel = $block->getAtLabel();
$_attributeType = $block->getAtType();
$_attributeAddAttribute = $block->getAddAttribute();
 
if ($_attributeLabel && $_attributeLabel == 'default') {
$_attributeLabel = $_product->getResource()->getAttribute($_code)->getFrontendLabel();
}
 
$_attributeValue =$_product->getResource()->getAttribute($_code)->getFrontend()->getValue($_product);
 
?>
 
<?php if ($_attributeValue): ?>
<div class="product attibute <?php echo $_className?>">
<?php /*?><?php if ($_attributeLabel != 'none'): ?><strong class="type"><?php echo $_attributeLabel?></strong><?php endif; ?><?php */?>
<div class="value" <?php echo $_attributeAddAttribute;?>><?php echo $_attributeValue; ?></div>
</div>
<?php endif; ?>
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