December 31, 2009

How to embed Google Custom Search in Magento

For those of you who love Google Custom Search and would like to use it with the Magento, here is a little how to. Entire embed process is really easy. It all comes down to copy paste-ing few lines of code from Google to Magento. My idea is to create the static block in Magento CMS section and then use the custom CMS page from which I will call this statick block among other HTML content I might wish to throw into the CMS page.

For starters, we need to create Google Custom Search engine. Attached are few screenshots to see how it looks.

Once we have created the necesery search engine, we will copy-paste the embed code into the static block of Magento, lets say we login to Magento, and create static block with identifier “custom-google-search-engine”.

For our last step, we will create a CMS page, giving it SEF URL Identifier “custom-search”, and placing the following {{block type=”cms/block” block_id=”custom-google-search-engine”}} block call into the content area.

That’s it. Now when you visit the url like http://myshop.domain/custom-search you will see the result like shown on photo below.

CC Image credits.

Programatically add bundle product to cart in Magento

Sometimes, clients requirements exceed the defaults built into the Magento. Recently we came across a task that among other things required manipulation of bundle products in Magento. In order to help my coworker Tomas I wrote this little code snippet that programatically adds bundle product to cart.

$params = array(
'product' => 164,
'related_product' => null,
'bundle_option' => array(
21 => 58,
20 => 55,
11 => 28,
12 => array(
0 => 31,
),
13 => array(
0 => 32,
1 => 35,
),
),
'options' => array(
3 => 'olaaaaaaaa',
),
'qty' => 2,
);

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

$product = new Mage_Catalog_Model_Product();
$product->load(164);

$cart->addProduct($product, $params);
$cart->save();

Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

$message = $this->__('Custom message: %s was successfully added to your shopping cart.', $product->getName());
Mage::getSingleton('checkout/session')->addSuccess($message);

The above source code might need a bit explanation. There are two major things to keep an eye on bundle product view page, custom options and bundle items.

Custom options are set in the array

'options' => array(
3 => 'olaaaaaaaa',
),

And bundle item options are set in array

'bundle_option' => array(
21 => 58,
20 => 55,
11 => 28,
12 => array(
0 => 31,
),
13 => array(
0 => 32,
1 => 35,
),
),



In summary “all” you need to do is to send the proper field name-values as a params. You can then use this code from within controller or as a fast fix from within view file.

Magento: Get Bundled Items By Bundle Product

This post is just a small snippet that will help you to get all products that are used do construct any bundle product.

Of course, I’m not sure how useful can it be as is, so if you need optimized version of it, you will need to play with it for a while.

You will need some basic knowledge of PHP language in order to fix formatting issues that you may (not saying that you will) encounter due strange behavior of code display plugin in wordpress.

I tried to find it on google few days ago and I had no luck, so if anybody has better solution, please comment.

Here we go:

< ?php

$bundled_product = new Mage_Catalog_Model_Product();
$bundled_product->load(YOUR_BUNDLED_PRODUCT_ID);

$option_collection = $bundled_product->getTypeInstance(true)->getOptionsCollection($bundled_product);

$selectionCollection = $bundled_product->getTypeInstance(true)->getSelectionsCollection(
$bundled_product->getTypeInstance(true)->getOptionsIds($bundled_product), $bundled_product
);

$bundled_items = array();
foreach($selectionCollection as $option)
{
$bundled_items[] = $option->product_id;
}

print_r($bundled_items);

?>

That easy ^_^