in Magento, how can i put shipping cost on the product page for a configurable product?
I have this code on my products view.phtml file: I am getting a shipping estimate and basing the lowest cost to a UK address and rendering this on the products page. Rather than having a full blown shipping estimator on the pages.
<?php
if($_product->isSaleable())
{
$quote = Mage::getModel('sales/quote');
$quote->getShippingAddress()->setCountryId('GB');
$quote->addProduct($_product);
$quote->getShippingAddress()->collectTotals();
$quote->getShippingAddress()->setCollectShippingRates(true);
$quote->getShipp开发者_如何转开发ingAddress()->collectShippingRates();
$rates = $quote->getShippingAddress()->getShippingRatesCollection();
$minPrice = PHP_INT_MAX;
foreach ($rates as $rate) {
$minPrice = min($minPrice, $rate->getPrice());
}
if ($minPrice == 0) {
echo ('This item qualifies for FREE shipping');
}
elseif ($minPrice < PHP_INT_MAX) {
echo ('Shipping to UK from £' . money_format('%i', $rate->getPrice()) . "\n");
}
}
?>
Which works fine for simple products. however on configurable products, it always displays free shipping! How can get around this?
This is just a guess, but a configurable product is just a container to organize a bunch of simple products. When you add a product to the cart (for real), there is a simple product in the cart in addition to the configurable one. I'm guessing that this second product provides the weight and information for shipping, etc.
That would imply that, to get your shipping estimate, you'll need to specify one of the simple products underneath the configurable (either directly, or by passing some options and pretending to have "selected" a product) in order to see your shipping estimates.
精彩评论