I have 2 custom option text fields associated with a product called Location To and Location From,
I have set up a Module that has an Observer for checkout_cart_add_product_complete
with a method called getLocationCoords, how would I get access to these two fields from the event object that passed to my method.
I need these 2 fields so I can reverse geocode them which I think I will be able to do myself.
Once I get the 2 sets of coordinates how would I go about storing them with the product so I would be able to see them associated with the product when an order is paced?
======================= edit addded ============================
What i have tried so far
public function getLocationCoords(Varien_Event_Observer $observer)
{
// Retrieve the product being updated from the event observer
$product = $observer->getEvent()->getProduct();
// Write a new line to var/log/product-updates.log
$name = $product->getName();
$sku = $product->getSku();
Mage::log(
"{$name} ({$sku}) updated",
null,
'product-updates.log'
);
foreach ($product->getOptions() as $o) {
$type = "Custom Option TYPE: " . $o->getType();
$title = "Custom Option TITLE: " . $o->getTitle();
Mage::log(
"{$type} ({$title}) custom option {$o}",
null,
'product-updates.log'
);
}
}
It all works except getting the values of the custom options, i will try the answer below and see where i get
when i print out the var dump of the option object this is what i get
2012-12-11T15:19:45+00:00 DEBUG (7): array (
'option_id' => '1',
'product_id' => '1',
'type' => 'field',
'is_require' => '1',
'max_characters' => '0',
'sort_order' => '0',
'default_title' => 'Location To',
'title' => 'Location To',
'default_price' => '0.0000',
'default_price_type' => 'fixed',
'price' => '0.0000',
'price_type' => 'fixed',
)
there is no value to the object
I believe that by the time that event is dispatched you will need to access the sales quote object from the cart session, which smells bad to me. Try the checkout_cart_product_add_after
[link] event and get access to the quote item as follows:
public function geoConversion ($obs)
{
$item = $obs->getQuoteItem();
/* @var $item Mage_Sales_Model_Quote_Item */
//I believe the custom options will be in here, not sure
$buyRequest = $item->getBuyRequest();
// your logic
}
I haven't checked this, but it's a bit better an event to use. However, there are lots of considerations when customizing the add to cart process (admin-created orders and updates of existing cart items for example), so test, test, test. Also, there is another event which may be useful/more appropriate: sales_quote_product_add_after
[link].
Further, this behavior could also be achieved by giving these products a custom type model based on - or by observing events dispatched in - Mage_Catalog_Model_Product_Type_Abstract
.
It's dizzying.
There is also this event
$eventName = sprintf('catalog_product_type_prepare_%s_options', $processMode);
Mage::dispatchEvent($eventName, array(
'transport' => $transport,
'buy_request' => $buyRequest,
'product' => $product
));
With processMode either full or lite. The buy_request should have your details and you can then add a new option on the product
$product->addCustomOption('my_option','my_value');
Not tested but do let us know how you are getting on.
I was able to get to get access to quote item and from that i was able to access the values of the custom attributes. I had two options and after some debugging i realised my options were being called option_1 and option_2 , from that i was able to get the value and set them to my variables.
public function getLocationCoords(Varien_Event_Observer $observer)
{
$item = $observer->getQuoteItem();
$locationFrom = '';
$locationTo = '';
foreach ($item->getOptions() as $o) {
if($o['code'] == 'option_2'){
$locationFrom = $o['value'];
}
if($o['code'] == 'option_1'){
$locationTo = $o['value'];
}
}
Mage::log("the location from = ".$locationFrom);
Mage::log("the location to = ".$locationTo);
}