Magento Themes and Magento Extensions

Load A Category or Product by an Attribute

Share
Posted on April 28th, 2013 | Posted by admin

New developers often ask me how to load a product by a particular attribute. This is quite easy to accomplish in Magento and can be done using Magento collections.

Load a Product by SKU

Unlike most product attributes, SKU is a static attribute. This means that instead of being stored in one of the products EAV tables, it is stored in the main product entity table (catalog_product_entity). This makes loading a product based on it’s SKU much easier and efficient.

1
2
3
4
5
6
7
8
9
10
11
<?php
$sku = 'my-product-sku';
$product = Mage::getModel('catalog/product')->load($sku, 'sku');
if ($product->getId()) {
echo $product->getName();
}
else {
echo 'Product not found with SKU of ' . $sku;
}

The above code makes use of the load method, which is available for all EAV entities in Magento. By default this method takes two parameters: a value that’s used to match the product and the field that the values is checked against. If the second parameter is empty, Magento will use the primary key, which is usually the model ID. By passing a static attribute (SKU is the only useful static attribute for this context), it is possible to load a product model on a different value.

Observers, reality or fiction

Share
Posted on April 20th, 2013 | Posted by admin

Observers are very powerful instrument in Magento. They allow us to bind our functionality to some Magento events. Many of you know about dispatchEvent function. This function calls all observers registered for this event. For example, we have the following code snippet placed in this app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php file.

1
Mage::dispatchEvent('catalog_product_collection_load_after', array('collection' => $this));

It means, that we can bind any event we want to identifier ‘catalog_product_collection_load_after’ and operate with $this variable (it’s a product collection in our case). But it is important to know what event choose for your functionality. Therefore, Magento runs your observer after collection loading.
Now, we need to bind our observer to ‘catalog_product_collection_load_after’ event. See below how we can do this

how to add magento products to google base

Share
Posted on April 18th, 2013 | Posted by admin

Its been log i have done this but forgot to share with all visitors. This article will guide you on how to add magento products to google base. This works only for first time but if you wish to submit automatically then yes its possible.

Step 1: Magento Google Base Feed Account Configuration
go to System >> Configuration >> Google API >> Google Base.
Enter details of the account you wish to use to publish your products feed on. Select the default target country of your feed and select Yes in ‘Update Google Base Item when Product is updated’.

Magento Dev Best Practices: Modifying 3rd Party Extensions

Share
Posted on April 16th, 2013 | Posted by admin

Magento Dev Best Practices: Modifying 3rd Party Extensions
Often times there are occasions where you need to modify the functionality in a 3rd party module. If you find yourself in that situation, the best practice here is to not modify it directly. Write a module that extends the 3rd party extension! In doing this, you save headaches if you ever need to upgrade the extension, or if you find that you need to revert your changes and go back to the original, it’s easy.

Perhaps the most important aspect of properly extending a module is by setting the dependency:

Dependency

In your modules xml file in app/etc/modules, you can add a block of code to make your module depend on the 3rd party extension you are trying to extend. By doing this, you cause your new module to be loaded after the extension you are adding the dependency for. This is good because your config.xml information will be loaded afterwards, and therefore your rewrites, event observers, etc. will take priority, so there’s no need to comment out any code in the 3rd party extension’s config.xml file. Here is an example of your xml file if you were extending an extension named Awesome_Extension:

Creating Magento products from a script

Share
Posted on April 15th, 2013 | Posted by admin

Magento allows several different ways to add products to the catalog, including manual editing, upload via spreadsheet and the web services Magento Core API. For developers who want to add products through PHP however, there are a few caveats to be aware of for the product to be displayed on the frontend.

The basics of adding a new product are trivial – you get a new copy of the appropriate model, fill in the details and call the save() method:

Page 1 of 6612345»102030...Last »