虽然另一个答案作为指导很好,但有几件事给我带来了一些麻烦,我想尝试澄清一下。我决定使用安装脚本创建产品属性。我将以下代码放在我的 mysql4-install-0.1.0.php 文件中:
$entity = $this->getEntityTypeId('catalog_product');
$this->addAttribute($entity, 'reduced_shipping', array(
'type' => 'varchar',
'input' => 'boolean',
'label' => 'Product Qualified For Free Shipping',
'visible' => true,
'default' => '0',
'required' => true,
'used_in_forms' => array(
'adminhtml_checkout',
'adminhtml_customer',
'admin_html_customer_address',
'checkout_register',
'customer_account_create',
'customer_account_edit',
'customer_address_edit',
'customer_register_address',
)
));
虽然在创建属性时 boolean 是 input 的有效形式,但它看起来不能用于存储属性 type。这可以通过使用 varchar 来解决。运行设置脚本后,管理员可以转到目录 -> 管理产品,在这里他们可以为每个项目的免费送货属性选择是或否。
最后,我在创建的 Carrier 的 collectRates() 方法中使用以下内容循环浏览客户购物车中的商品,并查看管理员是否选择了免费送货。
$quote = Mage::getModel('checkout/session')->getQuote();
$itemList = $quote->getAllVisibleItems();
foreach($itemList as $item)
{
$product = Mage::getModel('catalog/product')->load($item->getProductId());
if($product->getReducedShipping() == 1)
{
//update the price of shipping here
}
}
我已经浏览了几件事以防止这个答案变得太长,所以请告诉我是否有任何我可以澄清的部分。