首先,您需要将自定义属性添加到将在购物车中使用的属性列表。按照stackoverflow上的这些答案:
如何在magento 1.4.1.1中将自定义上传的图像添加到购物车?
然后您需要创建您的自定义运输方式,可能是从您的方式扩展而来。它应该遍历从运输请求中收到的物品并检查不同的 zip 来源,然后分别计算它们的费率。
我希望对您来说,创建一个扩展现有运输方式功能的模块不会有问题。
干杯!
更新
要将您的属性添加到购物车项目中加载的产品,请使用以下配置:
<config>
<global>
<sales>
<quote>
<item>
<product_attributes>
<origin_zip />
</product_attributes>
</item>
</quote>
</sales>
</global>
</config>
然后在运输方法模型中使用类似这样的东西(以 USPS 为例):
public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
if (!$this->getConfigFlag('active')) {
return false;
}
$defaultOriginZip = Mage::getStoreConfig('shipping/origin/postcode', $this->getStore());
$requestDataByOriginZip = array();
// Walking through quote items
foreach ($request->getAllItems() as $quoteItem) {
// If virtual or not shippable separately, it should be skipped
if ($quoteItem->isVirtual() || $quoteItem->isDummy(true)) {
continue;
}
// Retrieving origin zip code
if ($quoteItem->getProduct()->getOriginZip()) {
$zipCodeForCalculation = $quoteItem->getProduct()->getOriginZip();
} else {
$zipCodeForCalculation = $defaultOriginZip;
}
if (!isset($requestDataByOriginZip[$zipCodeForCalculation])) {
// Default values initialization for this zip code
$requestDataByOriginZip[$zipCodeForCalculation] = array(
'orig_postcode' => $zipCodeForCalculation,
'package_weight' => 0,
'package_value' => 0,
// etc...
);
}
$requestDataByOriginZip[$zipCodeForCalculation]['package_weight'] += $quoteItem->getRowWeight();
$requestDataByOriginZip[$zipCodeForCalculation]['package_value'] += $quoteItem->getBaseRowTotal();
// Etc...
}
$results = array();
foreach ($requestDataByOriginZip as $requestData) {
$requestByZip = clone $request; // Cloning to prevent changing logic in other shipment methods.
$requestByZip->addData($requestData);
$this->setRequest($requestByZip);
// Returns rate result for current request
$results[] = $this->_getQuotes();
}
$yourMergedResult = Mage::getModel('shipping/rate_result');
foreach ($results as $result) {
// Logic for merging the rate prices....
}
return $yourMergedResult;
}