[英]Extending Magento Shopping Cart
I need to extend the Magento shopping cart to include an extra step for a store locator. 我需要扩展Magento购物车,以包括商店定位器的额外步骤。 I understand that I need to overwrite the core OnePage controller (
Mage_Checkout_OnepageController
) and blocks ( Mage_Checkout_Block_Onepage
) but what needs to be done with regards to keeping track of the extra information (eg user's selected options from my custom step). 我知道我需要覆盖核心的OnePage控制器(
Mage_Checkout_OnepageController
)和块( Mage_Checkout_Block_Onepage
),但是在跟踪额外信息方面需要做些什么(例如,用户从我的自定义步骤中选择的选项)。
There are a number of steps required here to get the whole solution. 要获得整个解决方案,这里需要执行许多步骤。
Firstly, create a new module. 首先,创建一个新模块。 Use the ModuleCreator if you wish.
如果需要,请使用ModuleCreator 。
Then, write a setup script in your module to add the new fields to Magento's attribute structure, eg : 然后,在模块中编写一个设置脚本 ,以将新字段添加到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));
Note the use of the Mage_Sales_Model_Mysql4_Setup
to add the fields to the relevant sales_flat_quote
and sales_flat_order
tables. 注意使用
Mage_Sales_Model_Mysql4_Setup
将字段添加到相关的sales_flat_quote
和sales_flat_order
表中。
Now, insert the following values in your module's config.xml file: 现在,将以下值插入模块的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>
That will instruct Magento to copy the values of your custom field from quote to order to invoice and credit_memo, etc. 这将指示Magento将自定义字段的值从报价复制到订单,再复制到发票和credit_memo等。
Then in your custom block/controller code, you will be able to use Magento's magic getters and setters to persist the values. 然后,在您自定义的块/控制器代码中,您将能够使用Magento的魔术获取器和设置器来保存这些值。
$oQuote = Mage::getSingleton('checkout/session')->getQuote();
$oQuote->setMyAttribute('some_value');
$oQuote->save();
You should see the new column and value saved in sales_flat_quote
. 您应该看到新列和值保存在
sales_flat_quote
。 Then once the customer completes checkout, the same value should be saved in sales_flat_order
. 然后,客户完成结帐后,应将相同的值保存在
sales_flat_order
。
Note that the above code can be extended to work for quote_item
and order_item
by just changing quote
to quote_item
etc, however, if you wish to save attribute values that have been set on your products, then some extra work is required. 请注意,只需将
quote
更改为quote_item
等,即可将上述代码扩展为适用于quote_item
和order_item
。但是,如果要保存已在产品上设置的属性值,则需要进行一些额外的工作。
Insert a new block of XML into your config.xml (again inside the global node): 将新的XML块插入config.xml中(同样在全局节点中):
<sales>
<quote>
<item>
<product_attributes>
<my_attribute />
</product_attributes>
</item>
</quote>
</sales>
Where my_attribute
is the attribute code on the product model. 其中
my_attribute
是产品模型上的属性代码。 That will make the my_attribute available on the linked product, so you can access it via 这将使my_attribute在链接的产品上可用,因此您可以通过以下方式访问它
$oQuoteItem->getProduct()->getMyAttribute()
without needing to perform a full Mage::getModel('catalog/product')->load($oQuoteItem->getProductId())
. 无需执行完整的
Mage::getModel('catalog/product')->load($oQuoteItem->getProductId())
。 This is much more efficient. 这样效率更高。
Then, you will need an observer to copy the values from the product object to the quote_item object. 然后,您将需要一个观察者来将值从产品对象复制到quote_item对象。 So, declare your observer in the config.xml:
因此,在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>
and write code in your observer class like this: 并在您的观察者类中编写代码,如下所示:
public function setCustomDataOnQuoteItem($oObserver){
$oProduct = $oObserver->getProduct();
$oQuoteItem = $oObserver->getQuoteItem();
foreach(array('my_attribute') as $vAttributeCode){
$oQuoteItem->setData($vAttributeCode,$oProduct->getData($vAttributeCode));
}
}
Here is a complete working module.. its (almost) the same as the above code of Johnatan. 这是一个完整的工作模块。其(几乎)与上面的Johnatan代码相同。 You will find it here: https://bitbucket.org/vovsky/adding-custom-product-attribute-to-quote-and-order-items-in/
您将在这里找到它: https : //bitbucket.org/vovsky/adding-custom-product-attribute-to-quote-and-order-items-in/
And full explanation of every step here: http://www.atwix.com/magento/custom-product-attribute-quote-order-item/ 并在此处对每个步骤进行完整说明: http : //www.atwix.com/magento/custom-product-attribute-quote-order-item/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.