ModifyMage - 25 June 2012 11:34 AM
Hi Josh,
I’m terribly sorry it took so long to get back to you. Here is new Flatrate.php code that should fix your problem. It’s a hack on top of the original hack that was this code, but it works in all cases as far as I can tell. This code embarrasses me now, and I don’t really have the time to make it right for the forum (we’ll do it right as an extension). It’s pretty much a matter of style, though. The code should still function properly in all cases (I hope).
Please let me know if you still have any problems.
David
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Mage
* @package Mage_Shipping
* @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Flat rate shipping model
*
* @category Mage
* @package Mage_Shipping
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mmsmods_ItemShipping_Model_Flatrate
extends Mage_Shipping_Model_Carrier_Abstract
implements Mage_Shipping_Model_Carrier_Interface
{
protected $_code = 'flatrate';
/**
* Enter description here...
*
* @param Mage_Shipping_Model_Rate_Request $data
* @return Mage_Shipping_Model_Rate_Result
*/
public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
if (!$this->getConfigFlag('active')) {
return false;
}
$freeBoxes = 0;
if ($request->getAllItems()) {
foreach ($request->getAllItems() as $item) {
if ($item->getFreeShipping() && !$item->getProduct()->getTypeInstance()->isVirtual()) {
$freeBoxes+=$item->getQty();
}
}
}
$this->setFreeBoxes($freeBoxes);
$result = Mage::getModel('shipping/rate_result');
if ($this->getConfigData('type') == 'O') { // per order
$shippingPrice = $this->getConfigData('price');
} elseif ($this->getConfigData('type') == 'I') { // per item
$shippingPrice = '0.00';
$config_qty = 0;
foreach ($request->getAllItems() as $item) {
if (!$item->getFreeShipping()) {
$productId = $item->getProduct()->getId();
$productList = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('ship_price')
->addIdFilter($productId);
foreach($productList as $product) {
$quantity = $item->getQty();
if ($product->getTypeId() == 'configurable') {
$config_qty = $item->getQty();
} elseif ($quantity <= $config_qty) {
$quantity = $config_qty;
$config_qty = 0;
}
if ($product->getShipPrice()) {
$shippingPrice+=$product->getShipPrice() * $quantity;
} else {
$shippingPrice+=$this->getConfigData('price') * $quantity;
}
}
}
}
} else {
$shippingPrice = false;
}
$shippingPrice = $this->getFinalPriceWithHandlingFee($shippingPrice);
if ($shippingPrice !== false) {
$method = Mage::getModel('shipping/rate_result_method');
$method->setCarrier('flatrate');
$method->setCarrierTitle($this->getConfigData('title'));
$method->setMethod('flatrate');
$method->setMethodTitle($this->getConfigData('name'));
if ($request->getFreeShipping() === true || $request->getPackageQty() == $this->getFreeBoxes()) {
$shippingPrice = '0.00';
}
$method->setPrice($shippingPrice);
$method->setCost($shippingPrice);
$result->append($method);
}
return $result;
}
public function getAllowedMethods()
{
return array('flatrate'=>$this->getConfigData('name'));
}
}