Thứ Ba, 8 tháng 3, 2011

How to Remove Product from cart on checkout page or by coding in magento

From magento cart you can delete product by clicking on delete button from cart page , but if you wish to delete any cart item in checkout page or by coding then you have write



$session= Mage::getSingleton('checkout/session');

$quote = $session->getQuote();



$cart = Mage::getModel('checkout/cart');

$cartItems = $cart->getItems();

foreach ($cartItems as $item)

{

    $quote->removeItem($item->getId())->save();

}



By writing the above code your all cart item will be delete.If you wish to delete a particular product from the cart session then instead of writing $item->getId() pass your Id of the product.


e.g: -

foreach ($cartItems as $item)

{

    if($item->getId()== 2)

    {

        $quote->removeItem($item->getId())->save();

    }

}

How to get all cart items from cart session in magento

This is one of the most usefull features for all magento developers.As it's very necessary while anybody working on Magento cart or checkout page.To get all cart item from cart session write the below code



$session= Mage::getSingleton('checkout/session');

foreach($session->getQuote()->getAllItems() as $item)

{

   $productid = $item->getProductId();

   $productsku = $item->getSku();

   $productname = $item->getName();

   $productqty = $item->getQty();

}

How to get shopping cart session in magento

While working on Cart page or checkout page, if you want to get the cart session value then You have to use this code in magento


$session= Mage::getSingleton('checkout/session');

Chủ Nhật, 27 tháng 2, 2011

How to get Magento Format Price with default currency symbol

Whenever we fetch all data of a product by using either product sku or by using product id, Price also we need . But if we fetch product price then it display price as number format without any currency . So if you want to fetch price like magento price format then you have to pass your price variable in the following code then you can get the dynamic currency also.


Let you got your price by




$price = $_Pdetails->getPrice();

// To fetch price like magento format price write

$magento_style_price = Mage::helper('core')->currency($price);

How to get product details using product sku in magento

If you know your product SKU you can get details of that products, e.g- you can get Product Id, Product Name, Product Description, Product Price etc.


To get details fo the product by sku please write the below code



$products = Mage::getModel('catalog/product')


$_Pdetails = $product->loadByAttribute('sku', 'product-sku');




or



$_Pdetails =
Mage::getModel('catalog/product')->loadByAttribute('sku','product-sku');


echo $_Pdetails->getName();

echo $_Pdetails->getDescription();

echo $_Pdetails->getPrice();

echo $_Pdetails->getProductUrl();



Chủ Nhật, 20 tháng 2, 2011

How to get absolute path of base directory in magento

While uploading a file in magento It's necessary to write the absolute path of your magento path, otherwise it will not upload. you can get your site absolute path from php.ini file, You can place this path by hardcording but later if you think to move your site to another host then the path will be change, So to make your absolute path dynamic or get the proper path in which your magento Installed, write the following code


<?php echo Mage::getBaseDir('media') . DS ;?>

It will definately work,

Thứ Sáu, 18 tháng 2, 2011

How to get access of magento core functions from outside the magento directory


This is the very important thing in magento while accessing data from outside of the magento, You also can use it to set cron job of any magento function, as cron job doen't work properly in magento.




require_once 'app/Mage.php'; // if your are not root folder then write the proper path like publichtml/magento/app/Mage.php

Mage::app('default');


Now you can create object of any classes and can access methods of those classes also.

Like the Below example



$obj = new Mage_Checkout_Model_Observer();

echo $obj->salesQuoteSaveAfter();