You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Observer方法间数据传递:Magento订单与报价自定义数据同步

Solution for Syncing Custom stoque Data from One-Step Checkout to Quote/Order

Let's walk through how to get your custom stoque data flowing from the one-step checkout to your quote and order correctly. First, I spot a quick fix needed in your config.xml, then we'll set up observers to handle the data transfer, and verify everything works.

Step 1: Fix Your config.xml Fieldset Mapping

Your current fieldsets configuration uses a generic <attributename> node, but this needs to match your actual attribute name (stoque). Update that section to:

<fieldsets>
    <sales_convert_quote>
        <stoque>
            <to_order>*</to_order>
        </stoque>
    </sales_convert_quote>
    <sales_convert_order>
        <stoque>
            <to_quote>*</to_quote>
        </stoque>
    </sales_convert_order>
</fieldsets>

This tells Magento to automatically copy the stoque value between quotes and orders during conversion.

Step 2: Add Event Observers to Capture Checkout Data

Next, we'll add observers to capture the checkbox value from the checkout form and save it to the quote, then ensure it gets copied to the order. Add this <events> block inside the <global> node of your config.xml:

<events>
    <!-- Capture stoque value when shipping method is saved -->
    <checkout_controller_onepage_save_shipping_method>
        <observers>
            <vendor_module_save_stoque_to_quote>
                <class>modulename/observer</class>
                <method>saveStoqueToQuote</method>
            </vendor_module_save_stoque_to_quote>
        </observers>
    </checkout_controller_onepage_save_shipping_method>
    
    <!-- Fallback: Ensure stoque is copied to order during quote submission -->
    <sales_model_service_quote_submit_before>
        <observers>
            <vendor_module_copy_stoque_to_order>
                <class>modulename/observer</class>
                <method>copyStoqueToOrder</method>
            </vendor_module_copy_stoque_to_order>
        </observers>
    </sales_model_service_quote_submit_before>
</events>

Step 3: Create the Observer Class

Create the observer file at app/code/local/Vendor/Module/Model/Observer.php with this code:

<?php
class Vendor_Module_Model_Observer
{
    /**
     * Save stoque checkbox value to quote when shipping method is saved
     */
    public function saveStoqueToQuote(Varien_Event_Observer $observer)
    {
        $request = $observer->getEvent()->getRequest();
        $shippingPostData = $request->getPost('shipping');
        
        // Check if our stoque value exists in the POST data
        if (isset($shippingPostData['stoque'])) {
            $quote = Mage::getSingleton('checkout/session')->getQuote();
            // Convert checkbox value to integer (1 = checked, 0 = unchecked)
            $quote->setStoque((int)$shippingPostData['stoque']);
            $quote->save();
        }
        
        return $this;
    }

    /**
     * Manually copy stoque from quote to order as a fallback
     */
    public function copyStoqueToOrder(Varien_Event_Observer $observer)
    {
        $quote = $observer->getEvent()->getQuote();
        $order = $observer->getEvent()->getOrder();
        
        if ($quote->getStoque() !== null) {
            $order->setStoque($quote->getStoque());
        }
        
        return $this;
    }
}

Step 4: Verify Your Checkout Template Input Name

Make sure the checkbox in your shipping method template uses the correct name attribute so the data is included in the shipping POST request:

<input type="checkbox" name="shipping[stoque]" id="stoque-checkbox" value="1" />
<label for="stoque-checkbox"><?php echo $this->__('Que to service station') ?></label>

Step 5: Test the Flow

  1. Clear your Magento cache (System > Cache Management)
  2. Go through the one-step checkout, check the "Que to service station" checkbox
  3. After placing the order, check your sales_flat_quote and sales_flat_order tables—you should see the stoque column set to 1 (or 0 if you left it unchecked)

This setup ensures your custom data is captured from the checkout, saved to the quote, and synced to the order reliably.

内容的提问来源于stack exchange,提问作者Alexander Zelenchuk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:55:48