簡體   English   中英

擴展Magento購物車

[英]Extending Magento Shopping Cart

我需要擴展Magento購物車,以包括商店定位器的額外步驟。 我知道我需要覆蓋核心的OnePage控制器( Mage_Checkout_OnepageController )和塊( Mage_Checkout_Block_Onepage ),但是在跟蹤額外信息方面需要做些什么(例如,用戶從我的自定義步驟中選擇的選項)。

要獲得整個解決方案,這里需要執行許多步驟。

首先,創建一個新模塊。 如果需要,請使用ModuleCreator

然后,在模塊中編寫一個設置腳本 ,以將新字段添加到Magento的屬性結構中,例如:

 $setup = new Mage_Sales_Model_Mysql4_Setup('core_setup');
 $setup->startSetup();

 $setup->addAttribute('quote', 'my_attribute', array('type' => 'varchar', 'visible' => false, 'required' => false));
 $setup->addAttribute('order', 'my_attribute', array('type' => 'varchar', 'visible' => false, 'required' => false));
 $setup->addAttribute('invoice', 'my_attribute', array('type' => 'varchar', 'visible' => false, 'required' => false));
$setup->addAttribute('creditmemo', 'my_attribute', array('type' => 'varchar', 'visible' => false, 'required' => false));

注意使用Mage_Sales_Model_Mysql4_Setup將字段添加到相關的sales_flat_quotesales_flat_order表中。

現在,將以下值插入模塊的config.xml文件中:

<global>

    <fieldsets>
        <sales_convert_quote>
            <my_attribute>
                <to_order>*</to_order>
            </my_attribute>
        </sales_convert_quote>
        <sales_convert_order>
            <my_attribute>
                <to_cm>*</to_cm>
                <to_invoice>*</to_invoice>
            </my_attribute>
        </sales_convert_order>
    </fieldsets>

這將指示Magento將自定義字段的值從報價復制到訂單,再復制到發票和credit_memo等。

然后,在您自定義的塊/控制器代碼中,您將能夠使用Magento的魔術獲取器和設置器來保存這些值。

$oQuote = Mage::getSingleton('checkout/session')->getQuote();
$oQuote->setMyAttribute('some_value');
$oQuote->save();

您應該看到新列和值保存在sales_flat_quote 然后,客戶完成結帳后,應將相同的值保存在sales_flat_order

請注意,只需將quote更改為quote_item等,即可將上述代碼擴展為適用於quote_itemorder_item 。但是,如果要保存已在產品上設置的屬性值,則需要進行一些額外的工作。

將新的XML塊插入config.xml中(同樣在全局節點中):

   <sales>
        <quote>
            <item>
                <product_attributes>
                    <my_attribute />
                </product_attributes>
            </item>
        </quote>
    </sales>

其中my_attribute是產品模型上的屬性代碼。 這將使my_attribute在鏈接的產品上可用,因此您可以通過以下方式訪問它

$oQuoteItem->getProduct()->getMyAttribute()

無需執行完整的Mage::getModel('catalog/product')->load($oQuoteItem->getProductId()) 這樣效率更高。

然后,您將需要一個觀察者來將值從產品對象復制到quote_item對象。 因此,在config.xml中聲明您的觀察者:

    <events>
        <sales_quote_item_set_product>
            <observers>
                <quoteitem_set_custom_data>
                    <type>singleton</type>
                    <class>mymodule/observer</class>
                    <method>setCustomDataOnQuoteItem</method>
                </quoteitem_set_custom_data>
            </observers>
        </sales_quote_item_set_product>
    <events>

並在您的觀察者類中編寫代碼,如下所示:

public function setCustomDataOnQuoteItem($oObserver){
    $oProduct = $oObserver->getProduct();
    $oQuoteItem = $oObserver->getQuoteItem();
    foreach(array('my_attribute') as $vAttributeCode){
        $oQuoteItem->setData($vAttributeCode,$oProduct->getData($vAttributeCode));
    }
}

這是一個完整的工作模塊。其(幾乎)與上面的Johnatan代碼相同。 您將在這里找到它: https : //bitbucket.org/vovsky/adding-custom-product-attribute-to-quote-and-order-items-in/

並在此處對每個步驟進行完整說明: http : //www.atwix.com/magento/custom-product-attribute-quote-order-item/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM