【问题标题】:Extending Magento Shopping Cart扩展 Magento 购物车
【发布时间】:2012-06-21 00:33:15
【问题描述】:

我需要扩展 Magento 购物车,以包含一个额外的商店定位器步骤。我知道我需要覆盖核心 OnePage 控制器 (Mage_Checkout_OnepageController) 和块 (Mage_Checkout_Block_Onepage),但是在跟踪额外信息方面需要做些什么(例如,用户从我的自定义步骤中选择的选项)。

【问题讨论】:

    标签: php magento checkout extend


    【解决方案1】:

    这里需要许多步骤来获得整个解决方案。

    首先,创建一个新模块。如果您愿意,请使用ModuleCreator

    然后,在你的模块中写一个setup script 以将新字段添加到 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 的魔法 getter 和 setter 来持久化这些值。

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

    您应该会看到 sales_flat_quote 中保存的新列和值。然后,一旦客户完成结帐,相同的值应保存在 sales_flat_order 中。

    请注意,只需将quote 更改为quote_item 等,上述代码可以扩展为适用于quote_itemorder_item,但是,如果您希望保存已在您的产品上设置的属性值,然后需要一些额外的工作。

    在您的 config.xml 中插入一个新的 XML 块(同样在全局节点内):

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

    其中my_attribute 是产品型号上的属性代码。这将使 my_attribute 在链接产品上可用,因此您可以通过

    访问它
    $oQuoteItem->getProduct()->getMyAttribute()
    

    无需执行完整的Mage::getModel('catalog/product')-&gt;load($oQuoteItem-&gt;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));
        }
    }
    

    【讨论】:

    • 为什么没有人像您一样创建清晰完整的博客文章?我花了 2 小时寻找你的答案!谢谢!
    【解决方案2】:

    这是一个完整的工作模块。它(几乎)与上面的 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/

    【讨论】:

      猜你喜欢
      • 2015-12-13
      • 1970-01-01
      • 2015-07-06
      • 2013-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-09
      • 1970-01-01
      相关资源
      最近更新 更多