Category Archives: Magento

July 8, 2016 1:49 pm
image_round

how to get skin url path magento 2

{{view url=''}} // In Block and CMS Pages

$block->getViewFileUrl() // in Phtml or on php

how to get skin url path magento 2

{{view url=''}} // In Block and CMS Pages

$block->getViewFileUrl() // in Phtml or on php
June 4, 2016 8:38 am
image_round

Magento 2 : How to add custom logs

$_product = $this->item->getProduct();
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info(print_r($_product->getData(), true));

Magento 2 : How to add custom logs

$_product = $this->item->getProduct();
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info(print_r($_product->getData(), true));
June 2, 2016 9:38 am
image_round

Magento 2 : How to add add custom link to toplinks

<referenceBlock name="top.links">
<block class="Magento\Framework\View\Element\Html\Link\Current" name="contact-us">
<arguments>
<argument name="label" xsi:type="string">Contact Us</argument>
<argument name="path" xsi:type="string">contact-us</argument>
</arguments>
</block>
</referenceBlock>

Magento 2 : How to add add custom link to toplinks

<referenceBlock name="top.links">
<block class="Magento\Framework\View\Element\Html\Link\Current" name="contact-us">
<arguments>
<argument name="label" xsi:type="string">Contact Us</argument>
<argument name="path" xsi:type="string">contact-us</argument>
</arguments>
</block>
</referenceBlock>
June 1, 2016 9:52 am
image_round

Magento 1.x fix Product Flat Data reindexing issue with row size too large

When you get some error like this

Product Flat Data index process unknown error: exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1118 Row size too large.
The maximum row size for the used table type, not counting BLOBs, is 65535.
You have to change some columns to TEXT or BLOBs'

Then you just have to update one file and 2 lines

app/code/core/Mage/Eav/Model/Entity/Attribute/Abstract.php, decreasing the value of the length element on line 604 from 255 characters to 64 in the following array:

$columns[$this->getAttributeCode()] = array(
   'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
   'length' => '64',
   'unsigned' => false,
   'nullable' => true,
   'default' => null,
   'extra' => null
);

and  Decrease the value of the type element on line 677  from 255 characters to 64 in the following array:

$columns[$this->getAttributeCode()] = array(
   'type' => 'varchar(64)',
   'unsigned' => false,
   'is_null' => true,
   'default' => null,
   'extra' => null
);

Magento 1.x fix Product Flat Data reindexing issue with row size too large

When you get some error like this

Product Flat Data index process unknown error: exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1118 Row size too large.
The maximum row size for the used table type, not counting BLOBs, is 65535.
You have to change some columns to TEXT or BLOBs'

Then you just have to update one file and 2 lines

app/code/core/Mage/Eav/Model/Entity/Attribute/Abstract.php, decreasing the value of the length element on line 604 from 255 characters to 64 in the following array:

$columns[$this->getAttributeCode()] = array(
   'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
   'length' => '64',
   'unsigned' => false,
   'nullable' => true,
   'default' => null,
   'extra' => null
);

and  Decrease the value of the type element on line 677  from 255 characters to 64 in the following array:

$columns[$this->getAttributeCode()] = array(
   'type' => 'varchar(64)',
   'unsigned' => false,
   'is_null' => true,
   'default' => null,
   'extra' => null
);
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 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);
?>