Multiple Origin Zip Codes in Magento Checkout
I'm working on a Magento storefront for a client. They use dropshippers, so a single zip code doesn't do us much help. We do have it set for the most common zip code from which the client ships开发者_StackOverflow中文版, so, in many cases, it's ok.
However, in some cases, there is a different origin zip code that needs to be used. In more rare cases, we will have multiple origin zip codes. When there is a zip that differs from the main one, we have stored this in an attribute called 'origin zip' (creative, huh?)
Where should I be looking to make the modifications? We're only using the UPS shipping method, and what I'm looking to do is, before calculating shipping, to grab whatever origin zips may be in the cart (I think we've got this part), but then, depending on the results, I may need to iterate through the shipping calculation and add the values together - i.e. in the case they order one product with an origin zip code, and another product without an origin zip code, it would have to calculate the first, then the second, and then add them together.
If someone could point us in the correct direction of which php files or classes we'll need to modify, I would greatly appreciate it.
First of all you need to add your custom attributed to list of attributes that will be used in shopping cart. Follow these answer on stackoverflow: How to add custom uploaded images to cart in magento 1.4.1.1?
Then you need to create your custom shipping method, maybe extended from yours one. It should walk through items it receives from shipping request and check for different zip origin, then calculate rate for them separately.
I hope for you it will not be a problem to create a module that will extend existing shipping method functionality.
Cheers!
UPDATE For adding your attribute to product that is loaded in the cart item use such configuration:
<config>
<global>
<sales>
<quote>
<item>
<product_attributes>
<origin_zip />
</product_attributes>
</item>
</quote>
</sales>
</global>
</config>
Then in shipping method model use something like this (used USPS as example):
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;
}
The class usa/shipping_ups
is what handles these requests, and specifically the method setRequest
seems to have what you need:
if ($request->getOrigPostcode()) {
$r->setOrigPostal($request->getOrigPostcode());
} else {
$r->setOrigPostal(Mage::getStoreConfig('shipping/origin/postcode', $this->getStore()));
}
If you can add the orig_postcode to the shipping request, UPS will return a quote based on that origin.
One approach to this would be to override Mage_Shipping_Model_Rate_Request
and add a method called getOrigPostcode
. By virtue of being a real method, this would override the default Magento getter/setter behavior. Have this method query the contents of the request to find out which zip needs to be used.
Hope that helps!
Thanks, Joe
精彩评论