Magento: Add Manufacturer Column to Admin Product Grid

Magento Icon

A client requested to add a column to the product grid in the admin so that they could filter by manufacturer. It would be great if Magento made this more simple to do, but it’s not that bad to just do it manually. Instead of creating a whole module to do this, I’m just overriding one file. You can make a module with it quite easily if you want.

The following was used and tested with Magento 1.3.2.4

Step 1:
Copy this file: /app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php and paste it here: /app/code/local/Mage/Adminhtml/Block/Catalog/Product/Grid.php. This will override the core file with your own without actually modifying core code.

Step 2:
By looking at this file, you will clearly see how Magento is setting up the columns. Find where you want to put the Manufacturer column, and paste the following code:

$manufacturer_items = Mage::getModel('eav/entity_attribute_option')->getCollection()->setStoreFilter()->join('attribute','attribute.attribute_id=main_table.attribute_id', 'attribute_code');
        foreach ($manufacturer_items as $manufacturer_item) :
            if ($manufacturer_item->getAttributeCode() == 'manufacturer') 
                $manufacturer_options[$manufacturer_item->getOptionId()] = $manufacturer_item->getValue();
        endforeach;
 
        $this->addColumn('manufacturer',
            array(
                'header'=> Mage::helper('catalog')->__('Manufacturer'),
                'width' => '100px',
                'type'  => 'options',
                'index' => 'manufacturer',
                'options' => $manufacturer_options
        ));

Step 3:
Enjoy your new column. Props to wolfdog85 for helping me figure out the code. The original forum post can be found here.

Magento: Add Javascript to Home Page Only (Or Any CMS Page)

Magento Icon

Have you ever needed to include a javascript file to the home page only to run that sweet slideshow you have displayed (or any other reason)? Fortunately, you don’t have to modify any of the layout files, or add it to your head.phtml file. CMS pages in Magento allow you to modify the layout XML specific to that page. So, all you need to do is open up the CMS page in Magento that you want to add the javascript to, click on the “Custom Design” tab on the left, and in the “Layout Update XML” field add the following:

<reference name="head">
  <action method="addItem">
    <type>skin_js</type><script>yourfile.js</script>
  </action>
</reference>

Or, if you want to just put the javascript in the /js/ directory of your app, you can change the “skin_js” to just “js”.

Magento: Import Tier Pricing using API

Magento Icon

Yet another thing that would be nice if Magento allowed you to import through the admin interface. But, at least there is the API to utilize, and fortunately importing Tier Pricing for products is quite easy. I will provide you with a script and CSV format to get started quickly.

Important Note: Be sure that wherever you are running the script, you have SOAP installed/enabled. You can run it from anywhere that has it, as you are providing the script with a URL of a Magento installation. If your server doesn’t have it, you can install PHP and SOAP on your local machine and run it off of there if you’d like.

First thing you are going to need to do, if you haven’t done it already, is to setup an API user in your installation of Magento. Go to System >> Web Services >> Users and setup an account. Be sure that the role you give that user has access to tier pricing.

Next, create a PHP file where ever you want. I chose to put it in /api-scripts/ and I called it “tier-pricing-import.php”. Be sure to replace the four values I’ve highlighted with your own values:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
 
$proxy = new SoapClient('http://[YOUR-WEBSITE-URL]/api/soap/?wsdl');
$sessionId = $proxy->login('[YOUR-WEBSERVICE-USERNAME]', '[YOUR-WEBSERVICE-PASSWORD]');
 
$row = 1;
 
$handle = fopen('[YOUR-CSV-FILENAME].csv', 'r');
 
while (($data = fgetcsv($handle, 30, ',')) !== false) {
    if ($row != 1) {
        $tierPrices = '';
        $sku = $data[2];
 
        $tierPrices[] = array(
            'website'           => $data[0],
            'customer_group_id' => $data[1],
            'qty'               => $data[3],
            'price'             => $data[4]
        );
 
        try {
            $proxy->call($sessionId, 'product_tier_price.update', array($sku, $tierPrices));
        } catch (Exception $e) {
            $errors .= 'Error for SKU ' . $sku . ': ' . $e->getMessage() . "\n";
        }
    }
    $row++;
}

Finally, create your CSV. You’ll want 5 columns:

  • website_id: Integer value for your website id. Use ‘all’ for all websites
  • cust_group: Integer value for the customer group it will be applied to. Use ‘all’ to apply it to all groups
  • sku: The product sku that the tier pricing will be applied to
  • price_qty: The quantity the price is applied to (x and above)
  • price: The price

Be sure to include the header row. Here is an example of how the csv should look:

website_id cust_group sku price_qty price
1 3 XB60 10 25.99
1 3 XB60 50 20.99
all all RDX300 15 8

Just include the csv in the same directory as your php file, then in your browser, type in the URL of the php file and run it. It will import your tier pricing very quickly. I haven’t built in any sort of messages that let you know when it’s done, or if there were any errors or anything. This is as bare-bones as it gets, but it works as needed. I hope you find it useful.

Magento 1.4 Released

Magento Icon

It’s a bit late to be posting this, as 1.4 was released at least a couple weeks ago now. But, I just wanted to at least comment on it. I am extremely thrilled at some of the improvements. There are too many to list and discuss now, so if you want a full list, you can check it out on Magento’s website.

Perhaps the best improvement has been done with the addition of the WYSIWYG built-in to Magento (we finally don’t have to use the Fontis or Bouncing Orange extensions!). The widgets are also awesome, and really extends the usability and flexibility for clients.

Another much appreciated, but small fix, is the ability for Magento to remember what you were trying to do in the backend. Pre-1.4, if your admin session timed out and you had to log back in, you would be sent to the dashboard, no matter where you were at before. 1.4 now knows what you were clicking on, and sends you there after you login.

They also finally added what they call “custom variables”. This allows you to create variables such as a company phone number or address. You can then use the variable call on your transactional email templates and if you ever need to edit the phone number or address, you only have to do it in one place, instead of on every email.

There will always be a list of things that the community wishes Magento had that 1.4 still doesn’t have. But, let’s look at what it does have (which is quite impressive), remember that it just keeps getting better over time, and also remember that the price tag is $0. Don’t forget that you don’t have to use OSCommerce or ZenCart anymore too.

Trying It Again

My boss today encouraged all of us to make a point to contribute more frequently to the web community at our staff meeting. I’ve been back in more development, especially with Magento lately, so it seems that I should try and revive the blog. This will be my second attempt to get back into regular updates, so no promises. I am glad to see though that over 200 people regularly visit my website each day, so it’s still useful. That is encouraging.

←Older