将自定义字段的值保存到sales_flat_order_address表中

时间:2014-08-09 10:03:41

标签: magento

我在后端有一个自定义字段(new_address_code)用于客户地址。当通过myaddress或结帐页面添加新地址时,观察者用于为字段分配自动生成的值

<customer_address_save_after>
      <observers>
         <myautoincrement>
            <class>samp/observer</class>
            <method>saveadrsmethod</method>
         </myautoincrement>
      </observers>
</customer_address_save_after>

在观察者类

$customerAddress = $observer->getCustomerAddress();
$id= (string)$customerAddress->getId();
$ObcustomerAddress = $observer->getCustomerAddress();
if ($customerAddress->getId()) {
            $customerAddress->setData('new_address_code','aftertesting-'.$customerAddress->getData('firstname').'=myid'.$id);
             $customerAddress->getResource()->saveAttribute($ObcustomerAddress, 'new_address_code');
   }

这里的问题是,在结帐时添加新地址时,自动生成的自定义字段值不会保存到sales_flat_quote_address表中。所以,我尝试编写一个观察者,如下所示

<sales_order_place_before>
    <observers>
       <myautoincrement>
          <class>samp/observer</class>
          <method>beforeorder</method>
       </myautoincrement>
    </observers>
</sales_order_place_before>

在观察者班级

   public function beforeorder($observer)
{
    $order = $observer->getEvent()->getOrder();
    $customerAddress = $observer->getCustomerAddress();
    if ($customerAddress->getId()) {
        $address=Mage::getModel('sales/order_address')->setData('new_address_code',$customerAddress->getData('new_address_code'));
        $order->setBillingAddress($address);
        $order->setShippingAddress($address);
    }
}

但它没有保存在sales_flat_order_address表中。任何人都可以告诉我正确的方法。

1 个答案:

答案 0 :(得分:3)

我已经给出了解决方案

事件是

<checkout_submit_all_after>
    <observers>
        <auto_set_custom_field>
            <class>check/observer</class>
            <method>autosetBillingShipfield</method>
        </auto_set_custom_field>
    </observers>
</checkout_submit_all_after>

观察者功能代码是

public function autosetBillingShipfield($Observer){
    $Order=$Observer->getEvent()->getOrder();
    $Quote=$Observer->getEvent()->getQuote();
    if ($Quote->getBillingAddress()->getSaveInAddressBook()&& !is_null($Order->getBillingAddress()->getCustomerAddressId())) {
                 $address = Mage::getModel('customer/address')->load((int)$Order->getBillingAddress()->getCustomerAddressId());
        $SaleaOrderAdd=Mage::getModel('sales/order_address')->load($Order->getBillingAddress()->getId());       
        $SaleaOrderAdd->setData('myaddr_new_code','after-testingnew-'.$Order->getBillingAddress()->getData('firstname').'--'.$address->getId());
        $SaleaOrderAdd->getResource()->saveAttribute($SaleaOrderAdd, 'myaddr_new_code');


    }
    if ($Quote->getShippingAddress()->getSaveInAddressBook()&& !is_null($Order->getShippingAddress()->getCustomerAddressId())) {
                 $address = Mage::getModel('customer/address')->load((int)$Order->getShippingAddress()->getCustomerAddressId());
        $SaleaShipOrderAdd=Mage::getModel('sales/order_address')->load($Order->getShippingAddress()->getId());      

        $SaleaShipOrderAdd->setData('myaddr_new_code','after-testingnew-'.$Order->getShippingAddress()->getDataFirstname().'--'.$address->getId());
        $SaleaShipOrderAdd->getResource()->saveAttribute($SaleaShipOrderAdd, 'myaddr_new_code');

    }

}