Pages

Tuesday, September 10, 2013

How to add categories to product by product sku programmatically in Magento

<?php

$rootDir = ""; //root path
include($rootDir."app/Mage.php");
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
Mage::app("default");
$baseUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);

// $productsSkus is an array of the products you want to modify.
// Create it however you want, I did it like this...
$productsSkus = array('100246','800649','017261','006403');

//update product categories
for($i=0;$i<count($productsSkus);$i++)
{
$productModel = Mage::getModel('catalog/product');
$productId = $productModel->getIdBySku($productsSkus[$i]);
$product = $productModel->load($productId);
$categoryArr = $product->getCategoryIds();
// Array of category_ids to add.
$categoryArr[] = 842;
$product->setCategoryIds(array($categoryArr));
$product->save();

}
?>
Hope you will enjoy!

Thursday, September 5, 2013

How to get customer name, customer email on success.phtml/order confirmation page in Magento

<?php
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
if($order->getCustomerId() === NULL)
{
$customerName  = $order->getBillingAddress()->getName();
$customerEmail = $order->getBillingAddress()->getEmail();
}
else
{
$customer      = Mage::getModel('customer/customer')->load($order->getCustomerId());
$customerName  = $order->getCustomerName();
$customerEmail = $order->getCustomerEmail();
}
?>

Wednesday, September 4, 2013

How to get total quantity of product sold in Magento

<?php
$id = 100001; // Enter your product ID here
$_productCollection = Mage::getResourceModel('reports/product_collection')
    ->addOrderedQty()
    ->addAttributeToFilter('id', $id)
    ->setOrder('ordered_qty', 'DESC')
    ->getFirstItem();
$product = $_productCollection;
echo 'Total Sold'.$product->ordered_qty;
?>

Sunday, September 1, 2013

How to disable Google Checkout button in Magento

Use the following steps to disable google checkout button from cart page as given below:

Login into Admin first then

Steps 1: Go to System->Configuration

Steps 2: Click on "Gopple API" under "Sales Tab" from left side.

Steps 3: Click on "Google Checkout" and select Enable "No".

Steps 4: Click on "Save Config" button at the top.

Wednesday, August 28, 2013

Sort products by 2 attributes in magento

Sort products by 2 attributes in magento

In magento site suppose you have the requirement to sort the product listings by 2 attributes like "in stock" and "popularity"

After some research, it seems to work.

Use the following code given below:

<?php
     $Collection->setOrder(array('attribute1', 'attribute2'), asc); //the setOrder() method accepts an array.
?>

Modified this line in Toolbar.php in the Catalogue/Product/List/ directory.

<?php
 if ($this->getCurrentOrder())
 {
    $this->_collection->setOrder(array($this->getCurrentOrder(), 'in stock'), $this->getCurrentDirection());
 }
?>

Hope this will work!




Monday, August 26, 2013

Enable template path hint in Magento

To enable magento path hint for your magento website

Login into admin section and go to System->Configuration->Advanced->Developer

Inside this, click on "Debug" option.

If the Current Configuration Scope is "Default" (left top), you will be not able to set template path hints for your magento website.

So you have to select the magento website from the dropdown and then change the template Path Hints to "yes" .

Now your template path hint has been enabled. You can view template path hint on the frontend of your website.

if some how due to cache you are not getting the path hint.

Then go to System->Cache management and "flush" or "refresh" the cache.

Friday, August 23, 2013

Magento: Difference between order states and statuses

If you are building website in Magento, you may have noticed that there are two columns insales_flat_order table which are confusing. These are state and status. You might think what is the difference between these two, both having same meaning.

Well, this is not the case. They both are different. State is used by magento to tell if the order is new, processing, complete, holded, closed, canceled, etc.; while Statuses are the one that YOU would be defining at the backend in System -> Order Statuses. Magento displays order STATUSES and not STATES in the backend order detail page to let you know which status is assigned as per your mapping. Remember, multiple statuses can be mapped with one state, while vice versa is not possible.

Consider an example, your customer places an order as Cash on Delivery, you will need something like COD_Pending as the order status so that you know it is not yet paid. Magento will have state new for this, which makes you unpredictable of what kind of transaction is this, COD or Prepaid. The STATUS can be anything, as you define, for your understanding; while STATE is something which Magento needs to understand, internally.

In short, Magento uses order state internally for processing order, whereas order status are used by store owners to understand the exact order flow where one state can be assigned to multiple statuses.

To understand this in detail, have a look at this

Thursday, August 22, 2013

How to create categories tree structure in Magento programmatically

This is simple example to get nested category list. We can easily create a left navigation and a drop-down menu with the Retrieved HTML. Follow the code :

<?php

set_time_limit(0);
error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors",'On');

$rootDir = ""; // Root Directory Path
include($rootDir."app/Mage.php");
Mage::app("default");

$rootcatId= Mage::app()->getStore()->getRootCategoryId(); // get default store root category id
$categories = Mage::getModel('catalog/category')->getCategories($rootcatId); // else use default category id =2

function  show_categories_tree($categories) {
    $array= '<ul>';
    foreach($categories as $category) {
        $cat = Mage::getModel('catalog/category')->load($category->getId());
        $count = $cat->getProductCount();
        $array .= '<li>'.
        '<a href="' . Mage::getUrl($cat->getUrlPath()). '">' .
                  $category->getName() . "(".$count.")</a>\n";
        if($category->hasChildren()) {
            $children = Mage::getModel('catalog/category')->getCategories($category->getId());
             $array .=  show_categories_tree($children);
            }
         $array .= '</li>';
    }
    return  $array . '</ul>';
}
echo  show_categories_tree($categories);

?>

Wednesday, August 21, 2013

How to add/update category in magento from csv programmatically

<?php
set_time_limit(0);
ini_set(‘memory_limit’,’1024M’);
require_once ‘../../app/Mage.php’;
Mage::app();

//read data from csv file
$row = 1; $cat_arr = array();
if (($handle = fopen(“category.csv”, “r”)) !== FALSE)
{
while (($data = fgetcsv($handle, 100000, “,”)) !== FALSE)
{
if($row > 1)
{

/* $data[0] is csv column in csv file
$data[1],$data[2],$data[3],$data[4] is category Id column */

$cat_arr[trim($data[0])] = $data[1].”,”.$data[2].”,”.$data[3].”,”.$data[4].”,”.$data[5].”,”.$data[6].”,”.$data[7]; // $data[0] column represent sku

}

$row++;
}
fclose($handle);
}

$products = Mage::getResourceModel(‘catalog/product_collection’);
$i = 1 ;
foreach ( $products as $index => $productModel )
{
$_product = Mage::getModel(‘catalog/product’)->load($productModel->getId());
$cnids = $cat_arr[$_product->getSku()];
$ids = explode(“,”,$cnids);
$_product->setCategoryIds($ids);
$_product->save();
$i++;
}

?>

How to call category list in footer in magento

<?php $helper = $this->helper('catalog/category') ?>

<?php foreach ($helper->getStoreCategories() as $_category): ?>
<a href="<?php echo Mage::getModel('catalog/category')                                                                                                                                                                                                                                                                                            ->setData($_category->getData())->getUrl(); ?>" title="<?php echo $_category->getName() ?>">   <?php echo $_category->getName() ?></a>
<?php endforeach ?>