Dec 21, 2015

Magento How to filter product collection by product type_id

How to filter product collection by product type_id

$collectionSimple = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('type_id', array('eq' => 'simple'));

$collectionConfigurable = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('type_id', array('eq' => 'configurable'));

$collectionBundle = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('type_id', array('eq' => 'bundle'));

$collectionGrouped = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('type_id', array('eq' => 'grouped'));

$collectionVirtual = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('type_id', array('eq' => 'virtual'));

Magento How to Apply Patches on Magento WebSite

Hello Friends,

I am sharing this post because, I founded this Magecomp patches post very useful and safe way to install Magento patches.

Specially I liked  Method 3: because they provides the compressed files of all the updated patches and we just need to extract and upload on server in the root of your Magento.

Please note take backup of your files and db before applying this patches (Apply patches at your own risk). 

You can choose whichever you like or as per your convenience. Click link below to get the detail post.  

How to Install Magento Security Patches – The Ultimate Guide



Note: You can test/scan your site after applying patches  here https://www.magereport.com/scan/?s=https://www.urdomain.com/ to know your site security level.

Magento How to set cron job using SSH account

In this post I will show you how to set cron job for cron.php file to run in every 5 minutes.

























Please follows the steps below...

Step 1: Open SSH window and log in using your account detail.

Step 2: Check if already there any crons set for this server.

ssh>> export VISUAL=nano; crontab -e

You can see if there any cron after executing the above command.

Step 3: Copy paste below command and execute.

*/5 * * * * /usr/bin/php -f /var/www/urprojectfolder/www/cron.php

Step 4: Run below command to save and exit.

ssh>> CTRL+X

ssh>> Y

You are done!!!

Note::

You can set other crons same as above but please confirm your cron file path.

You can check if your cron set properly or not by running below command again.

ssh>> export VISUAL=nano; crontab -e



Dec 11, 2015

Magento How to add shipping charge to the cart automatically for logged in customer only

Sometime we needs to apply shipping charge on shopping cart page after adding products to cart directly but generally in Magento the shipping cost only apply when the customer manually run shipping cost estimator or in checkout page after selecting country.

In order to get this working, you need a custom module which hooks into the event ‘sales_quote_save_before’. 

Please consider following assumption to create custom module:

 - Namespace as 'Pg'
 - And Module name as 'Cartshipping'

Step 1: Create a custom module definition xml; in path app/etc/modules, create the file Pg_Cartshipping.xml and add code below.

<config>
<modules>
<Pg_Cartshipping>
<active>true</active>
<codePool>local</codePool>
</Pg_Cartshipping>
</modules>
</config>


Step 2: Now creates the following directory structure

app/code/local/Pg
app/code/local/Pg/Cartshipping
app/code/local/Pg/Cartshipping/etc
app/code/local/Pg/Cartshipping/Model


Step 3: Now under the folder "app/code/local/Pg/Cartshipping/etc", create a file "config.xml"" and add the following content:


<?xml version="1.0"?>
<config>
<modules>
<Pg_Cartshipping>
<version>0.1.0</version>
</Pg_Cartshipping>
</modules>
<global>
<models>
<pg_cartshipping>
<class>Pg_Cartshipping_Model</class>
</pg_cartshipping>
</models>
</global>
<frontend>
<events>
<checkout_cart_save_before>
<observers>
<pg_cartshipping_observer>
<type>singleton</type>
<class>pg_cartshipping/observer</class>
<method>addShippingCost</method>
</pg_cartshipping_observer>
</observers>
</checkout_cart_save_before>
</events>
</frontend>
</config>


Step 4: Now under the folder "app/code/local/Pg/Cartshipping/Model", create a file "Observer.php" and add the following content:

class Pg_Cartshipping_Model_Observer {

private $_shippingCode = 'tablerate_tablerate'; // change your default shipping method code here
private $_country = '';

public function addShippingCost($params = null) {

$customer = Mage::getSingleton('customer/session')->getCustomer();
$customerAddressId = $customer->getDefaultShipping();
if ($customerAddressId)
{
$address = Mage::getModel('customer/address')->load($customerAddressId);
$cust_data = $address->getData();
$this->_country = $cust_data['country_id'];
}
if($this->_country != "")
{
if (Mage::registry('checkout_addShipping')) {
Mage::unregister('checkout_addShipping');
return;
}
Mage::register('checkout_addShipping',true);
 
$cart = Mage::getSingleton('checkout/cart');
$quote = $cart->getQuote();
 
if ($quote->getCouponCode() != '') {
$c = Mage::getResourceModel('salesrule/rule_collection');
$c->getSelect()->where("code=?", $quote->getCouponCode());
foreach ($c->getItems() as $item) { $coupon = $item; }
 
if ($coupon->getSimpleFreeShipping() > 0) {
$quote->getShippingAddress()->setShippingMethod($this->_shippingCode)->save();
return true;
}
}
 
try {
$method = $quote->getShippingAddress()->getShippingMethod();
if ($method) return; // don't overwrite if default set
 
if ($quote->getShippingAddress()->getCountryId() == '') {
$quote->getShippingAddress()->setCountryId($this->_country);
}
 
$quote->getShippingAddress()->setCollectShippingRates(true);
$quote->getShippingAddress()->collectShippingRates();
 
$rates = $quote->getShippingAddress()->getAllShippingRates();
$allowed_rates = array();
foreach ($rates as $rate) {
array_push($allowed_rates,$rate->getCode());
}
 
if (!in_array($this->_shippingCode,$allowed_rates) && count($allowed_rates) > 0) {
$shippingCode = $allowed_rates[0];
}

if (!empty($shippingCode)) {
$address = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress();
if ($address->getCountryId() == '') $address->setCountryId($this->_country);
if ($address->getCity() == '') $address->setCity('');
if ($address->getPostcode() == '') $address->setPostcode('');
if ($address->getRegionId() == '') $address->setRegionId('');
if ($address->getRegion() == '') $address->setRegion('');
$address->setShippingMethod($this->_shippingCode)->setCollectShippingRates(true);
Mage::getSingleton('checkout/session')->getQuote()->save();
} else {
$address = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress();
if ($address->getCountryId() == '') $address->setCountryId($this->_country);
if ($address->getCity() == '') $address->setCity('');
if ($address->getPostcode() == '') $address->setPostcode('');
if ($address->getRegionId() == '') $address->setRegionId('');
if ($address->getRegion() == '') $address->setRegion('');
$address->setShippingMethod($this->_shippingCode)->setCollectShippingRates(true);
Mage::getSingleton('checkout/session')->getQuote()->save();
}
 
Mage::getSingleton('checkout/session')->resetCheckout();
 
}
catch (Mage_Core_Exception $e) {
Mage::getSingleton('checkout/session')->addError($e->getMessage());
}
catch (Exception $e) {
Mage::getSingleton('checkout/session')->addException($e, Mage::helper('checkout')->__('Load customer quote error'));
}
}


}

public function getQuote() {
        if (empty($this->_quote)) {
            $this->_quote = Mage::getSingleton('checkout/session')->getQuote();
        }
        return $this->_quote;
    }

}



## Done. Clear Cache ##

Magento How to get product group price

How to get product group price in Magento

Here is the code:

$product=Mage::getModel('catalog/product')->load($productId);

if(!is_null($product->getGroupPrice()){
$groupPrice = $product->getGroupPrice();
}

Dec 10, 2015

Magento How To Delete/Truncate All Test Product, Orders, Sales, Customer & Newsletter Subscribers Data Using SQL Script

Warning: Before run below script take backup of your database and then try.

## DELETE Product Script ##

SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE TABLE `catalog_product_bundle_option`;
TRUNCATE TABLE `catalog_product_bundle_option_value`;
TRUNCATE TABLE `catalog_product_bundle_selection`;
TRUNCATE TABLE `catalog_product_entity_datetime`;
TRUNCATE TABLE `catalog_product_entity_decimal`;
TRUNCATE TABLE `catalog_product_entity_gallery`;
TRUNCATE TABLE `catalog_product_entity_int`;
TRUNCATE TABLE `catalog_product_entity_media_gallery`;
TRUNCATE TABLE `catalog_product_entity_media_gallery_value`;
TRUNCATE TABLE `catalog_product_entity_text`;
TRUNCATE TABLE `catalog_product_entity_tier_price`;
TRUNCATE TABLE `catalog_product_entity_varchar`;
TRUNCATE TABLE `catalog_product_link`;
TRUNCATE TABLE `catalog_product_link_attribute`;
TRUNCATE TABLE `catalog_product_link_attribute_decimal`;
TRUNCATE TABLE `catalog_product_link_attribute_int`;
TRUNCATE TABLE `catalog_product_link_attribute_varchar`;
TRUNCATE TABLE `catalog_product_link_type`;
TRUNCATE TABLE `catalog_product_option`;
TRUNCATE TABLE `catalog_product_option_price`;
TRUNCATE TABLE `catalog_product_option_title`;
TRUNCATE TABLE `catalog_product_option_type_price`;
TRUNCATE TABLE `catalog_product_option_type_title`;
TRUNCATE TABLE `catalog_product_option_type_value`;
TRUNCATE TABLE `catalog_product_super_attribute_label`;
TRUNCATE TABLE `catalog_product_super_attribute_pricing`;
TRUNCATE TABLE `catalog_product_super_attribute`;
TRUNCATE TABLE `catalog_product_super_link`;
TRUNCATE TABLE `catalog_product_enabled_index`;
TRUNCATE TABLE `catalog_product_website`;
TRUNCATE TABLE `catalog_category_product_index`;
TRUNCATE TABLE `catalog_category_product`;
TRUNCATE TABLE `cataloginventory_stock_item`;
TRUNCATE TABLE `cataloginventory_stock_status`;
TRUNCATE TABLE `cataloginventory_stock`;
INSERT  INTO `catalog_product_link_type`(`link_type_id`,`code`) VALUES (1,'relation'),(2,'bundle'),(3,'super'),(4,'up_sell'),(5,'cross_sell');
INSERT  INTO `catalog_product_link_attribute`(`product_link_attribute_id`,`link_type_id`,`product_link_attribute_code`,`data_type`) VALUES (1,2,'qty','decimal'),(2,1,'position','int'),(3,4,'position','int'),(4,5,'position','int'),(6,1,'qty','decimal'),(7,3,'position','int'),(8,3,'qty','decimal');
INSERT  INTO `cataloginventory_stock`(`stock_id`,`stock_name`) VALUES (1,'Default');
TRUNCATE TABLE `catalog_product_entity`;
SET FOREIGN_KEY_CHECKS = 1;


## Delete all Orders, Sales & Customer Data Script ##

SET FOREIGN_KEY_CHECKS=0;

##############################
# SALES RELATED TABLES
##############################
TRUNCATE `sales_flat_creditmemo`;
TRUNCATE `sales_flat_creditmemo_comment`;
TRUNCATE `sales_flat_creditmemo_grid`;
TRUNCATE `sales_flat_creditmemo_item`;
TRUNCATE `sales_flat_invoice`;
TRUNCATE `sales_flat_invoice_comment`;
TRUNCATE `sales_flat_invoice_grid`;
TRUNCATE `sales_flat_invoice_item`;
TRUNCATE `sales_flat_order`;
TRUNCATE `sales_flat_order_address`;
TRUNCATE `sales_flat_order_grid`;
TRUNCATE `sales_flat_order_item`;
TRUNCATE `sales_flat_order_payment`;
TRUNCATE `sales_flat_order_status_history`;
TRUNCATE `sales_flat_quote`;
TRUNCATE `sales_flat_quote_address`;
TRUNCATE `sales_flat_quote_address_item`;
TRUNCATE `sales_flat_quote_item`;
TRUNCATE `sales_flat_quote_item_option`;
TRUNCATE `sales_flat_quote_payment`;
TRUNCATE `sales_flat_quote_shipping_rate`;
TRUNCATE `sales_flat_shipment`;
TRUNCATE `sales_flat_shipment_comment`;
TRUNCATE `sales_flat_shipment_grid`;
TRUNCATE `sales_flat_shipment_item`;
TRUNCATE `sales_flat_shipment_track`;
TRUNCATE `sales_invoiced_aggregated`;            # ??
TRUNCATE `sales_invoiced_aggregated_order`;        # ??
TRUNCATE `log_quote`;

ALTER TABLE `sales_flat_creditmemo_comment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_creditmemo_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_creditmemo_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_invoice` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_invoice_comment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_invoice_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_invoice_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_address` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_payment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_status_history` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_address` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_address_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_item_option` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_payment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_shipping_rate` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment_comment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment_track` AUTO_INCREMENT=1;
ALTER TABLE `sales_invoiced_aggregated` AUTO_INCREMENT=1;
ALTER TABLE `sales_invoiced_aggregated_order` AUTO_INCREMENT=1;
ALTER TABLE `log_quote` AUTO_INCREMENT=1;

#########################################
# DOWNLOADABLE PURCHASED
#########################################
TRUNCATE `downloadable_link_purchased`;
TRUNCATE `downloadable_link_purchased_item`;

ALTER TABLE `downloadable_link_purchased` AUTO_INCREMENT=1;
ALTER TABLE `downloadable_link_purchased_item` AUTO_INCREMENT=1;

#########################################
# RESET ID COUNTERS
#########################################
TRUNCATE `eav_entity_store`;
ALTER TABLE  `eav_entity_store` AUTO_INCREMENT=1;


##############################
# CUSTOMER RELATED TABLES
##############################
TRUNCATE `customer_address_entity`;
TRUNCATE `customer_address_entity_datetime`;
TRUNCATE `customer_address_entity_decimal`;
TRUNCATE `customer_address_entity_int`;
TRUNCATE `customer_address_entity_text`;
TRUNCATE `customer_address_entity_varchar`;
TRUNCATE `customer_entity`;
TRUNCATE `customer_entity_datetime`;
TRUNCATE `customer_entity_decimal`;
TRUNCATE `customer_entity_int`;
TRUNCATE `customer_entity_text`;
TRUNCATE `customer_entity_varchar`;
TRUNCATE `tag`;
TRUNCATE `tag_relation`;
TRUNCATE `tag_summary`;
TRUNCATE `tag_properties`;            ## CHECK ME
TRUNCATE `wishlist`;
TRUNCATE `log_customer`;

ALTER TABLE `customer_address_entity` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_datetime` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_decimal` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_int` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_text` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_varchar` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_datetime` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_decimal` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_int` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_text` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_varchar` AUTO_INCREMENT=1;
ALTER TABLE `tag` AUTO_INCREMENT=1;
ALTER TABLE `tag_relation` AUTO_INCREMENT=1;
ALTER TABLE `tag_summary` AUTO_INCREMENT=1;
ALTER TABLE `tag_properties` AUTO_INCREMENT=1;
ALTER TABLE `wishlist` AUTO_INCREMENT=1;
ALTER TABLE `log_customer` AUTO_INCREMENT=1;


##############################
# ADDITIONAL LOGS
##############################
TRUNCATE `log_url`;
TRUNCATE `log_url_info`;
TRUNCATE `log_visitor`;
TRUNCATE `log_visitor_info`;
TRUNCATE `report_event`;
TRUNCATE `report_viewed_product_index`;
TRUNCATE `sendfriend_log`;
### ??? TRUNCATE `log_summary`

ALTER TABLE `log_url` AUTO_INCREMENT=1;
ALTER TABLE `log_url_info` AUTO_INCREMENT=1;
ALTER TABLE `log_visitor` AUTO_INCREMENT=1;
ALTER TABLE `log_visitor_info` AUTO_INCREMENT=1;
ALTER TABLE `report_event` AUTO_INCREMENT=1;
ALTER TABLE `report_viewed_product_index` AUTO_INCREMENT=1;
ALTER TABLE `sendfriend_log` AUTO_INCREMENT=1;
### ??? ALTER TABLE `log_summary` AUTO_INCREMENT=1;


TRUNCATE catalogsearch_query;
TRUNCATE catalogsearch_fulltext;
TRUNCATE catalogsearch_result;
ALTER TABLE catalogsearch_query AUTO_INCREMENT=1;
ALTER TABLE catalogsearch_fulltext AUTO_INCREMENT=1;
ALTER TABLE catalogsearch_result AUTO_INCREMENT=1;

TRUNCATE TABLE sales_bestsellers_aggregated_daily;
TRUNCATE TABLE sales_bestsellers_aggregated_monthly;
TRUNCATE TABLE sales_bestsellers_aggregated_yearly;
ALTER TABLE sales_bestsellers_aggregated_daily AUTO_INCREMENT=1;
ALTER TABLE sales_bestsellers_aggregated_monthly AUTO_INCREMENT=1;
ALTER TABLE sales_bestsellers_aggregated_yearly AUTO_INCREMENT=1;

SET FOREIGN_KEY_CHECKS=1;



## Delete Newsletter Subscribers Data Script ##

SET FOREIGN_KEY_CHECKS=0;

TRUNCATE newsletter_problem;
TRUNCATE newsletter_queue;
TRUNCATE newsletter_queue_link;
TRUNCATE newsletter_queue_store_link;
TRUNCATE newsletter_subscriber;
TRUNCATE newsletter_template;

ALTER TABLE newsletter_problem AUTO_INCREMENT=1;
ALTER TABLE newsletter_queue AUTO_INCREMENT=1;
ALTER TABLE newsletter_queue_link AUTO_INCREMENT=1;
ALTER TABLE newsletter_queue_store_link AUTO_INCREMENT=1;
ALTER TABLE newsletter_subscriber AUTO_INCREMENT=1;
ALTER TABLE newsletter_template AUTO_INCREMENT=1;

SET FOREIGN_KEY_CHECKS=1;



## Done ##

Magento Adminside Access Denied errors for all custom modules after installing Security Patches SUPEE-6285


If your Admin account is restricted and some menus of third party extensions or your custom modules might not work any more.

The reason behind this is that the default return value of Mage_Adminhtml_Controller_Action::_isAllowed() has been changed from 'true' to Mage::getSingleton('admin/session')->isAllowed('admin').

Extensions or custom modules that do not override this method in their custom modules or extensions admin controllers because they don't use the ACL, now need the "ALL" privilege.

The solution for this is to patch the extensions or custom modules and add this method to all their admin controllers, please note admin controller not the front-end:

protected function _isAllowed()
{
    return true;
}

Or if they actually have an ACL resource defined in etc/adminhtml.xml:

protected function _isAllowed()
{
    return Mage::getSingleton('admin/session')->isAllowed('ENTER RESOURCE IDENTIFIER HERE');
}


For more detail you can refer post of stackexchange http://magento.stackexchange.com/questions/73646/access-denied-errors-after-installing-supee-6285

Javascript How To Set Cookie Globally for all pages

Javascript How To Set Cookie Globally for all pages 

For one page we can set Cookie as follows:

document.cookie = "myVarName=YourValue;";


For global we can set Cookie as follows just added extra 'path=/':

document.cookie = "myVarName=YourValue; path=/";

Dec 6, 2015

Magento How to get category and child/sub categories by category ID

This is one of the easy way to get category and child or sub categories using category id.
You can use following code anywhere in Magento module files.


<?php
$parentCategoryId = 4; // Note: Change you category id here
$allCats = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('is_active','1')
->addAttributeToFilter('include_in_menu','1')
->addAttributeToFilter('parent_id',array('eq' => $parentCategoryId))
->addAttributeToSort('position', 'asc');

foreach($allCats as $_category) {
try {
$_subcategories = $category->getChildrenCategories();
if (count($_subcategories) > 0){
echo 'Parent Category Name: '.$_category->getName();
echo 'Parent Category Id: '.$_category->getId();      
foreach($_subcategories as $_subcategory){
echo 'Child Category Name: '. $_subcategory->getName();
echo 'Child Category Id: '.$_subcategory->getId();
}
}
} catch (Exception $ex) {// log your exception here}
}
?>

Dec 4, 2015

Magento How to add unit price and product image in new order email template

In new order confirmation email template product unit price does not show by default. Sometimes we need customization in order email template as per our client requirements. In this post, I will guide you to customize order email template in easy steps.

The output of the customization will look like as follows:




Here we will change 3 files, you have to copy these files into the folder of your current theme in same folder structure.

1) app/design/frontend/base/default/template/email/order/items.phtml

See line 32, find the following code (it might be different if you already customized it):

    <tr>
    <th align="left" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Item') ?></th>
    <th align="left" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Sku') ?></th>
    <th align="center" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Qty') ?></th>
    <th align="right" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Subtotal') ?></th>
    </tr>

And replace with following code

    <thead>
    <tr>
<th align="left" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px">&nbsp;</th>
    <th align="left" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Item') ?></th>
    <th align="left" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Sku') ?></th>
    <th align="left" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Unit Price') ?></th>
    <th align="center" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Qty') ?></th>
    <th align="right" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Subtotal') ?></th>
    </tr>
    </thead>

2. app/design/frontend/base/default/template/email/order/items/order/default.phtml

See line 28, below this line of code (it might be different if you already customized it):

<?php $_order = $this->getItem()->getOrder() ?>

Add the following code

<!-- Show product image -->
<?php
$productid = $_item->getProductId();
$product_id = $productid;
$parentIds = Mage::getModel('catalog/product_type_grouped')->getParentIdsByChild($_item->getProductId()); // check for grouped product
if(count($parentIds)>0 && isset($parentIds[0]))
{
$product_id = isset($parentIds[0])?$parentIds[0]:$productid;
}
        $_product = Mage::getModel('catalog/product')
        ->setStoreId($_item->getOrder()->getStoreId())
        ->load($product_id);
?>
<!-- End Show product image -->

See line 30, above this line of code (it might be different if you already customized it):

<td align="left" valign="top" style="font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;">
        <strong style="font-size:11px;"><?php echo $this->escapeHtml($_item->getName()) ?></strong>

Add the following code

<!-- Show product image -->
<td style="vertical-align: middle;">
<a href="<?php echo $_product->getProductUrl(); ?>" ><img src="<?php echo Mage::helper('catalog/image')->init($_product, 'image')->resize(80); ?>" height="80" alt="" /></a>
</td>
<!-- End Show product image -->


See line 48, below this line of code (it might be different if you already customized it):

    <td align="left" valign="top" style="font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;"><?php echo $this->htmlEscape($this->getSku($_item)) ?></td>

Add the following code

    <td align="center" valign="top" style="font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;">
    <!-- Show Unit Price -->
    <?php
    if ($this->helper('tax')->displaySalesPriceInclTax($_order->getStore())) {
    $itemprice = $this->helper('checkout')->getPriceInclTax($_item) ;
    echo $this->helper('checkout')->formatPrice($itemprice);
    } else {
    echo $this->helper('checkout')->formatPrice($_item->getPrice()) ;
    }
    ?>
    <!-- END Unit Price -->
    </td>

3. app/design/frontend/base/default/layout/sales.xml

Open the above file and find for "sales_email_order_items".

    <action method="setLabelProperties"><value>colspan="3" align="right" style="padding:3px 9px"</value></action>

and replaced with following or just change colspan as per your extra columns added.

    <action method="setLabelProperties"><value>colspan="5" align="right" style="padding:3px 9px"</value></action>

Upload all files, clear cache and get expected output! :)

Sep 24, 2015

How to get Dropdown / select Attribute option value in product page in Magento

How to get Dropdown / select Attribute option value in product page in Magento


$attribute_name = $_product->getAttributeText('attribute_name');

Sep 23, 2015

How to create a custom product dropdown / select attribute and it's custom Options in Magento



1. Create a setup file in your module's sql folder. For example : mysql4-install-1.0.0 or mysql4-upgrade-1.0.0-1.0.1 if you are upgrading sql script.

2. Add a new dropdown / select product attribute and it's custom options as follows


    $installer = $this;
    $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
    $installer->startSetup();
    //Create Attribute
    $installer->addAttribute('catalog_product', 'backorder_time', array(
    'group'             => 'General',//Group name to add this attribute
    'type'              => 'text',
    'backend'           => '',
    'frontend'          => '',
    'label'             => 'Back-Order Duration',
    'input'             => 'select',
    'class'             => '',
    'source'            => '',
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible'           => true,
    'required'          => false,
    'user_defined'      => false,
    'default'           => '',
    'searchable'        => false,
    'filterable'        => false,
    'comparable'        => false,
    'visible_on_front'  => false,
    'unique'            => false,
    'apply_to'          => '',
    'is_configurable'   => false,
    'used_in_product_listing' => true,
    'sort_order'        => 500,
    ));
    //Add custom Back-order duration options
    $initialOptions = array('1 - 2 days','1 day','14 - 16 days','2 - 4 days','3 - 5 days','5 - 8 days','7 - 14 days','to 28 days');
    $entityTypeId     = $installer->getEntityTypeId('catalog_product');
    $code ='backorder_time';
    $attributeId = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'backorder_time')->getId();
    foreach($initialOptions as $k=>$v){
$installer->addAttributeOption(array(
'attribute_id' => $attributeId,
'order' => array($k),
'value' => array(array($v)
)
));
    }
    $installer->endSetup();




3. You can also remove the created above product attribute from remove codes as follows

 
    $setup = new Mage_Eav_Model_Entity_Setup('core_setup');

    //Remove code
    $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
    $setup->removeAttribute( 'catalog_product', 'backorder_time' );
 


Apr 15, 2015

Link Your Aadhaar Number with Your Voting Card EPIC Number for INDIAN



Vidarbhalive has posted a new item, 'Link Your Aadhaar Number with Your Voting Card EPIC
Number'

Know How to link Your Aadhaar Number with Your EPIC Number
Through Web Portal

        Visit portal http://nvsp.in

Search your name by clicking the box "Search Your Name box" by filling your name, name of your relation, state and the constituency or searching your name by filling your EPIC Number.

        As you see your details, please click the button ‘View’ [...]

You may view the latest post at
http://vidarbhalive.com/link-your-aadhaar-number-with-your-epic-number/

Mar 20, 2015

Magento2: How to create or add a menu or link at customer myaccount left navigation in Magento2

In this post, I will show you how we can add a menu link of our custom module page in myaccount left navigation page.

You can create it by following below steps:

Step 1: Create a customer account handle xml in path app\code\Mynamespace\HelloWorld\view\frontend\layout\customer_account.xml, if already not created and add the below code insidei <body> tag in xml file.

Code to Add:

 <referenceBlock name="customer_account_navigation">  
  <block class="Magento\Framework\View\Element\Html\Link\Current" name="customer-account-navigation-helloworld-list-link">  
  <arguments>  
   <argument name="path" xsi:type="string">helloworld</argument>  
   <argument name="label" xsi:type="string">HelloWorld List</argument>  
  </arguments>  
  </block>  
 </referenceBlock>  


Step 2: Now open your custom module handle xml file for example here I am taking helloworld module with index controller and index action.
app\code\Mynamespace\HelloWorld\view\frontend\layout\helloworld_index_index.xml
and add the code

Code to Add:
 <update handle="customer_account"/>   
above the <body> starting tag.

Step 3: Refresh Cache and you have done it.

Magento2: How to add custom link at top header links after LogIn Or Register link in Magento2

Here I will show you how we can add new custom link at top header links after LogIn Or Register links.

Please refer my previous post to create a custom link at header but displayed after logged in account,
if you want to show your custom link before logged in or just want to show link any time at header after Register link follow below steps.

Step 1: Refer my previous post to create a custom link at header but displayed after logged in account. Click Here...

Step 2: Now open app\code\Mynamespace\Mymodule\view\frontend\layout\default.xml file and add below code after <referenceBlock name="top.links"></referenceBlock>

Code To Add:
 <move element="test-link" destination="header.links"/>  


Here "test-link" is the name of your test link block which created in step 1.

Step 3: Refresh Cache and you have done it.

Mar 16, 2015

How to add custom link with custom html li, class and achor tag in top links after my account link in Magento2

In Magento2, we have seen earlier in my previous post 'How to add custom link in top links after my account link in Magento2' with default html link.

But sometime we needs to add our cutom html tags in link. For example if we want to add a css class in our custom link then we can use the following trick to get it work.

Same as the previous post, consider, we wish to add a 'Test Link' link and assuming custom module name as 'Mynamespace/Mymodule'.

Step 1: Create a block for your custom link  app\code\Mynamespace\Mymodule\Block\Link.php then add the code below in your block file.

Code:
 <?php  
 namespace Mynamespace\Mymodule\Block;  
 class Link extends \Magento\Framework\View\Element\Html\Link  
 {  
  protected $_template = 'Mynamespace_Mymodule::link.phtml';   
   public function getHref()  
   {  
     return $this->getUrl('test');  
   }  
   public function getLabel()  
   {  
     return __('Test Link');  
   }  
 }  
 ?>  



Step 2: Now create app\code\Mynamespace\Mymodule\view\frontend\layout\default.xml, if not already created and add code below in body tag

Code:
 <body>      
  <referenceBlock name="top.links">  
  <block class="Mynamespace\Mymodule\Block\Link" name="test-link" after="my-account-link"/>  
  </referenceBlock>      
 </body>  



Step 3: Create a phtml file in path app\code\Mynamespace\Mymodule\view\frontend\templates\link.phtml and add code below or modify the html as per your requirement

Code:
 <li class="Test-Class">  
   <a <?php echo $block->getLinkAttributes() ?>><?php echo $block->escapeHtml($block->getLabel())?>  
     <?php echo($block->getCounter()) ? '<span class="counter qty">' . $block->escapeHtml($block->getCounter()) . '</span>' : ''; ?>  
   </a>  
 </li>  



Step 4: Clear All Cache and you are done.

How to add custom link in top links after my account link in Magento2

In Magento2, we can add a custom link in top links as follows, please check the steps one by one.

Consider, we wish to add a 'Test Link' after My Account link and assuming you have your custom module name as 'Mynamespace/Mymodule'.

Step 1: Create a block for your custom link  app\code\Mynamespace\Mymodule\Block\Link.php then add the code below in your block file.

Code:

 <?php  
 namespace Mynamespace\Mymodule\Block;  
 class Link extends \Magento\Framework\View\Element\Html\Link  
 {  
   public function getHref()  
   {  
     return $this->getUrl('test');  
   }  
   public function getLabel()  
   {  
     return __('Test Link');  
   }  
 }  
 ?>  


Step 2: Now create app\code\Mynamespace\Mymodule\view\frontend\layout\default.xml, if not already created and add code below in body tag

Code:

 <body>      
  <referenceBlock name="top.links">  
  <block class="Mynamespace\Mymodule\Block\Link" name="test-link" after="my-account-link"/>  
  </referenceBlock>      
 </body>  


Step 3: Clear All Cache and you are done.

Mar 13, 2015

How to update or change page layout in Magento2

In Magento 2, we have page layout same as the page layouts in Magento1 but the implementation is slightly different.

To demonstrate, we choose the customer layout (customer.xml) file which show you how to update 1column.phtml page layout,

In Magento1 we used to update page layout like:

 <customer_account_login translate="label">   
  <reference name="root">  
  <action method="setTemplate"><template>page/1column.phtml</template></action>  
  </reference>   
 </customer_account_login>  


In Magento 2: Open file \Magento\Customer\view\frontend\layout\customer_account_login.xml and add layout="1column" in page tag

For Example:

 <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">  


Note: You can use other layout options like 2columns-left, 2columns-right and 3columns instead of 1column.
Note: You can use your custom module layout xml file to update layout.

Feb 13, 2015

How to create users with custom field in Appcelerator API using PHP CURL?

If you want to creates a new user with custom field then it's not a big deal. Follows the instructions and use the code below.

Instructions: When creating a user, you must specify either:
    * username
    * email address, first name, and last name

A user can have both an email address and username specified. However, if username is blank then email address, first name and last name are required.

We are creating here a user just by using the username, we are not using email here. So we just need username, password and password_confirmation.

Role: Whatever you want to add. I am taking as 'teacher'. It is not mendatory.
Custom Field: 'pranay' having value equal to '23'. It can be anything. Ex: Age, Gender.

PHP CURL Code: 
$key = 'YourAppceleratorKey';
$Curl_Session = curl_init("https://api.cloud.appcelerator.com/v1/users/create.json?key=" . $key.'&pretty_json=true&role=teacher&custom_fields={"pranay":23}');
 
curl_setopt($Curl_Session, CURLOPT_RETURNTRANSFER, true);    
$post_array = array('username' => "pgtest3", 'password' => "pgtest3" , 'password_confirmation' => "pgtest3");
curl_setopt($Curl_Session, CURLOPT_POSTFIELDS, $post_array);
$resp = curl_exec($Curl_Session);
curl_close($Curl_Session);

Reference Document: http://docs.appcelerator.com/cloud/latest/#!/api/Users-method-create

How to query on user's object fields and custom fields in Appcelerator API using PHP CURL?

Appcelerator Cloud Services provides APIs for querying and searching ACS objects. The query APIs allow you to perform custom database-style searches, while search APIs perform a full text search using the ACS search engine.

1. Query On Custom Field. For Example, My custom field is 'pranay'.
Scenario: Now fetch the user who having custom field value pranay = 23.

PHP CURL Code: 

$key = 'YourAppceleratorKeyHere';
$curl_handle = curl_init("https://api.cloud.appcelerator.com/v1/users/query.json?key=" . $key.'&pretty_json=true&count=true&where={"pranay":{"$eq":23}}');
curl_setopt($Curl_Session, CURLOPT_RETURNTRANSFER, true); $resp = curl_exec($curl_handle);
curl_close($curl_handle);
print_r($resp);

2. Query on predefined field like: username, first_name...etc.

PHP CURL Code: 

$curl_handle = curl_init("https://api.cloud.appcelerator.com/v1/users/query.json?key=" . $key.'&pretty_json=true&count=true&where={"username":"pgtest3"}');
curl_setopt($Curl_Session, CURLOPT_RETURNTRANSFER, true); $resp = curl_exec($curl_handle);
curl_close($curl_handle);
print_r($resp);

Note: In query you can use multiple operators sign like above used "$eq" which tends "equalto". please refer below link for more details.


Reference Document: http://docs.appcelerator.com/cloud/latest/#!/guide/search_query-section-query-on-custom-field-results-in-descending-order

Jan 29, 2015

How to use MySQL FIND_IN_SET() in Magento Collection

In Magento, we have "finset" keyword to filter collection with comma separated column field value.
For example: If myid field have string comma separated value like 2,4,65,22 and we want the record having 4 in myid field.

Then follows the below example to get the match records:

$collection = Mage::getResourceModel('modulename/modelname_collection');

$collection->addFieldToFilter('main_table.myid',array("finset"=>array('4')));