假设我们的购物车中有Magento可配置产品,我们想要修改它的选项但又不愿意删除或者重新添加它,那么怎么办呢?其实,按照下面的步骤,实现起来还是很简单的。

首先,打开你主题文件夹下的template/checkout/cart/item/default.phtml,找到下面的代码:

<?php if ($_options = $this->getOptionList()):?>

在这行代码下面添加:

< ?php
if($this->getProduct()->isConfigurable()){
$_product = Mage::getModel('catalog/product')->load($this->getProduct()->getId());
Mage::getBlockSingleton('catalog/product_view_type_configurable')->unsetData();
$_configurable = Mage::getBlockSingleton('catalog/product_view_type_configurable')->setData('product', $_product);
$_cdata = json_decode($_configurable->getJsonConfig());
$_current = array();
foreach((array)$this->getOptionList() as $_option) {
$_current[$_option['label']]=$_option['value'];
}
foreach($_cdata->attributes as $attribute) {
?>
<strong>< ?php echo $attribute->label; ?></strong>
<select style="width:150px;" name="cart[<?php echo $_item->getId() ?>][option][< ?php echo $attribute->id ?>]">
< ?php
foreach($attribute->options as $option) {
?>
<option <?php echo ($_current[$attribute->label]==$option->label) ? ' selected' : '' ?> value="< ?php echo $option->id ?>">< ?php echo $option->label ?> < ?php echo $this->helper('checkout')->formatPrice($option->price+$_item->getProduct()->getPrice()) ?></option>
< ?php
}
?>
</select>
< ?php
}
} else {
// THIS IS PLACE WHERE EXISTING CODE from [*] goes
}
?>

现在你已经完成了模板,你可以以你喜欢的方式来装饰它。下一步,你需要在local下创建你的“Model”和“etc”。etc中的config.xml代码是:

< ?xml version="1.0"?>
<config>
<frontend>
<events>
<checkout_cart_update_items_before>
<observers>
<your_firm_module_name_event>
<type>singleton</type>
<class>YOUR_FIRM_MODULE_NAME_Model_Card</class>
<method>update</method>
</your_firm_module_name_event>
</observers>
</checkout_cart_update_items_before>
</events>
</frontend>
</config>

“Model”中的Card.php代码为:

< ?php
 
class YOUR_FIRM_MODULE_NAME_Model_Card
{
public function update($e)
{
$_this = $e->cart;
$data = $e->info;
 
foreach ($data as $itemId => $itemInfo) {
$item = $_this->getQuote()->getItemById($itemId);
 
if (!$item) continue;
if (!isset($itemInfo['option']) or empty($itemInfo['option'])) continue;
 
foreach ($item->getOptions() as $option){
 
if($option->getCode()=='info_buyRequest'){
 
$unserialized = unserialize($option->getValue());
$unserialized['super_attribute'] = $itemInfo['option'];
$option->setValue(serialize($itemInfo['option']));
 
}elseif ($option->getCode()=='attributes'){
$option->setValue(serialize($itemInfo['option']));
}
 
}
$item->save();
}
}
 
}
?>

最后在app/etc中用下面的代码创建YOUR_FIRM_MODULE_NAME.xml。

< ?xml version="1.0"?>
<config>
<modules>
<your_firm_module_name>
<codepool>local</codepool>
<active>true</active>
</your_firm_module_name>
</modules>
</config>

360magento提供专业的基于magento系统的电商网站开发服务,如有需求或相关咨询,请与我们联系