dongyuduan1890 2012-12-11 11:04
浏览 62

如何从观察者事件访问magento中产品的自定义选项

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

  • 写回答

3条回答 默认 最新

  • duanna1407 2012-12-11 12:58
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100