Browsing articles in "Magento"
Aug 13, 2013
kalpesh

Magento convert attribute type from TEXT to DROPDOWN

Magento convert attribute type from TEXT (varchar) to DROPDOWN / SELECT (int) in Backend.
Magento doesn’t have this in-built so we will have to do it in our own way. We will convert our existing attribute which is in TEXT type, to DROPDOWN (select). Let’s say our attribute code is “vendor”, you will need to replace it with your own attribute code in all the code below.

WARNING: All the coupon codes that uses this attribute for discounts/promotions will GO AWAY! Please note down all the coupon code Conditions and Actions (which uses this attribute) before converting the attribute.

Step 1.) Backup your database.

mysqldump -u mysqluser -p mysqldbname > mysqldbname_backup.sql

Step 2.) Export all the values of the attribute you want to convert in a CSV file. For that, create a file vendor.php (your_attribute.php) in your magento root with following code inside it:

require "app/Mage.php";
umask(0);
Mage::app();

$collection = Mage::getModel('catalog/product')
                        ->getCollection()
                        ->addAttributeToSelect('sku')
                        ->addAttributeToSelect('vendor'); //replace vendor with your attribute code

$data = $collection->getData();

foreach ($data as $product) { 
    echo $product['entity_id'] . "," . $product['vendor']."\n";
}

After saving the above code, run it from command line:

php vendor.php > vendor.csv

You should now have all the attribute’s data with product ID in the vendor.csv (your_attribute.csv) file.

Step 3.) Now get all the unique values you have in the attribute’s data, which we will be filling them as options of select dropdown.
Run following mysql command to get all the unique values and copy them in a new file:

select distinct value from catalog_product_entity_varchar where attribute_id in (select attribute_id from eav_attribute where attribute_code='vendor'); //replace vendor with your own attribute code

After this step, we have all the attribute’s data in CSV file, and all the UNIQUE attribute values.
Continue reading »

Aug 11, 2013
kalpesh

Magento how to remove order

First of all, removing live orders is not recommended. But if you are sure you want to remove orders (test orders?) then you can do so by custom script below. Get the order increment id(s) of the order you wish to delete from Magento. Remember, once order is deleted you can’t get any related information of that order.

Create a test PHP file in your Magento project root with below code:

require 'app/Mage.php';
Mage::app('admin')->setUseSessionInUrl(false);                                                                                      

$orderIncrementIDs = array('100000001','100000002'); //your order increment ids to delete, beware!
$rmvd = array();

foreach($orderIncrementIDs as $ordID){
    try{
        Mage::getModel('sales/order')->loadByIncrementId($ordID)->delete();
        $rmvd[] = $ordID;
    } catch(Exception $e){
        echo 'Error: '.$e->getMessage().'<br />';
    }
}
echo "Following Orders Removed!<br />" . implode(", ",$rmvd);

and run the file to delete the order(s).
Continue reading »

Jul 21, 2013
kalpesh

Magento get current url with and without parameters

You can get the current page URL and it’s parameters (if any) by using getCurrentUrl() method in Magento. Below code will show you how to use it. Consider for example you have this url:

http://loca.lho.st/review/product/list/id/27/name/sony

To get this (current) URL in your module:

$currentUrl = $this->helper('core/url')->getCurrentUrl();
//Gives: http://loca.lho.st/review/product/list/id/27/name/sony

To get current URL parameters:

$params = $this->getRequest()->getParams(); //all the parameters
//Gives: Array ( [id] => 27 [name] => sony )
$param = $this->getRequest()->getParam('name'); //parameter "name"
//Gives: sony

To get only URL without parameters:

$request = $this->getRequest();
$urlWithoutParameters = $this->getBaseUrl() . $request->getRouteName() .DS. $request->getControllerName() .DS. $request->getActionName();
//Gives: http://loca.lho.st/review/product/list
Jul 21, 2013
kalpesh

Magento get all categories of a product

Get all the categories a product belongs to in Magento. Below code will get you all the categories with details the product is attached to. Product can be shown under more than one category, so you may get more than one category ID. Either get the category collection from product, or get all the category IDs and then load them using catalog category collection model.

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

First way,

$catCollection = $_product->getCategoryCollection();
foreach($catCollection as $cat){
  print_r($cat->getData());
  //echo $cat->getName();
  //echo $cat->getUrl();
}

Another way, Continue reading »

Jul 21, 2013
kalpesh

Magento redirect from observer

Redirection in observer doesn’t work normally as it do in Blocks, templates and controllers. Also there is no standard code to redirect from observer that works in every situation.

You will need an argument to achieve redirect when using below code:

public function observingMethod(Varien_Event_Observer $observer)
{
    $observer->getRequest()->setParam('return_url',$urlToRedirect);
}

Note that $observer object should have getRequest() method to make above code work. You may need to use $observer->getEvent()->getFront()->getRequest() otherwise, or simply var_dump/Mage::log $observer to get better idea what methods the object have.

Or you can use below code which is not recommended: Continue reading »

Pages:«1...78910111213...29»

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications

Honor

Recognition

Magento top 50 contributors

Magento top 50 contributors

Contributions