Browsing articles tagged with "magento Archives - Page 8 of 21 - Kalpesh Mehta"
Aug 13, 2013

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";

$collection = Mage::getModel('catalog/product')
                        ->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

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';

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

foreach($orderIncrementIDs as $ordID){
        $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

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:

To get this (current) URL in your module:

$currentUrl = $this->helper('core/url')->getCurrentUrl();

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();
Jul 21, 2013

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){
  //echo $cat->getName();
  //echo $cat->getUrl();

Another way, Continue reading »

Jul 21, 2013

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)

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 »


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!




Magento top 50 contributors

Magento top 50 contributors