2012-06-06 91 views
9

我正在运行Magento 1.7。为报价商品添加自定义选项(购物车中的商品)?

我想在sales_flat_quote_item_option中添加一个自定义的报价项选项。

我尝试过addOption和addCustomOption函数,但似乎没有查询启动到数据库。

这是我的代码现在(在自定义模块的辅助):

public function assignDocumentToQuoteItem(Mage_Sales_Model_Quote_Item $quoteItem, $documentid) 
{ 
    if (is_numeric($documentid) && $documentid > 0) { 
     /** @var Mage_Catalog_Model_Product */ 
     $product = $quoteItem->getProduct(); 

     $quoteItem->addOption(array(
      'product_id' => $product->getId(), 
      'product' => $product, 
      'code'  => 'documentid', 
      'value'  => $documentid 
     )); 
     $quoteItem->save(); 
     return true; 
    } 

    throw new Exception(__METHOD__.' - Document id has to be a numeric value.'); 
} 
+4

这可能是回答(很好)在这里:http://stackoverflow.com/a/9344336/833795 [Magento的变化自定义选项值的 – benmarks

+1

可能重复它添加到购物车前](http://stackoverflow.com/questions/9334115/magento-change-custom-option-value-before-adding-it-to-cart) – Alex

+0

你尝试加入try catch异常来保存()吗? – srgb

回答

2

是的,这是可能的,你需要使用观察者

我一直在添加交货日期在每个产品订单

因此,您可以将其更改为要添加到每个产品左右的选件。

  <controller_action_predispatch_checkout> 
      <observers> 
       <options_observer> 
        <class>YOUR_CLASS_NAME</class> 
        <method>setProductInfo</method> 
       </options_observer> 
      </observers> 
     </controller_action_predispatch_checkout> 

public function setProductInfo($observer) 
{ 
    if ('checkout_cart_add' != $observer->getEvent()->getControllerAction()->getFullActionName()) { 
     return; 
    } 
    $request = Mage::app()->getRequest(); 
    $prId = $request->getParams(); 
    $product = Mage::getModel('catalog/product')->load($prId['product']); 
    // fixed spelling of cofigurable/configurable 
    if ($product->getTypeId() == 'configurable') { 
     return $this; 
    } 

    if (!$product->getHasOptions()) { 
     $optionID = $this->saveProductOption($product); 
    } else { 
     $options = $product->getOptions(); 
     if ($options) { 
      foreach ($options as $option) { 
       if ($option->getTitle() == 'Delivery Date') { 
        $optionID = $option->getOptionId(); 
       } 
      } 
     } 
     if (empty($optionID)) { 
      $optionID = $this->saveProductOption($product); 
     } 
    } 

    $deliveryDate = $prId['delivery_date']; 
    if (!empty($deliveryDate)) { 
     $opt['options'] = array($optionID => $deliveryDate); 
     $request->setParams($opt); 
    } 

    return $this; 
} 

function saveProductOption($product) 
{ 

    $store = Mage::app()->getStore()->getId(); 
    $opt = Mage::getModel('catalog/product_option'); 
    $opt->setProduct($product); 
    $option = array(
     'is_delete' => 0, 
     'is_require' => false, 
     'previous_group' => 'text', 
     'title' => 'Delivery Date', 
     'type' => 'field', 
     'price_type' => 'fixed', 
     'price' => '0.0000' 
    ); 
    $opt->addOption($option); 
    $opt->saveOptions(); 
    Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID)); 
    $product->setHasOptions(1); 
    $product->save(); 

    $options = $product->getOptions(); 
    if ($options) { 
     foreach ($options as $option) { 
      if ($option->getTitle() == 'Delivery Date') { 
       $optionID = $option->getOptionId(); 
      } 
     } 
    } 
    Mage::app()->setCurrentStore(Mage::getModel('core/store')->load($store)); 
    return $optionID; 
} 
相关问题