Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use renderers for shipping methods in Magento
<dl class="sp-methods">
<?php $shippingCodePrice = array(); ?>
<?php $_sole = count($_shippingRateGroups) == 1; foreach ($_shippingRateGroups as $code => $_rates): ?>
<?php echo Mage::helper('your_module/shipping')->getShippingRatesHtml($code, $_rates, $_sole); ?>
<?php endforeach; ?>
</dl>
<?php
class Your_Module_Block_Shipping_Method_Renderer_Default extends Mage_Checkout_Block_Onepage_Shipping_Method_Available
{
const DEFAULT_TEMPLATE = 'checkout/onepage/shipping_method/default.phtml';
protected $_soleFlag = true;
protected $_code = '';
/**
* Set carrier code
*
* @param string $code
* @return Your_Module_Block_Shipping_Method_Renderer_Default
*/
public function setCode($code)
{
$this->_code = $code;
return $this;
}
/**
* Get carrier code
*
* @return string
*/
public function getCode()
{
return $this->_code;
}
/**
* Set rates
*
* @param array $rates
* @return Your_Module_Block_Shipping_Method_Renderer_Default
*/
public function setRates($rates)
{
$this->_rates = $rates;
return $this;
}
/**
* Set sole flag
*
* @param bool $flag
* @return Your_Module_Block_Shipping_Method_Renderer_Default
*/
public function setSoleFlag($flag)
{
$this->_soleFlag = $flag;
return $this;
}
/**
* Set sole flag
*
* @return bool
*/
public function isSole()
{
return $this->_soleFlag;
}
protected function _beforeToHtml()
{
$codeTemplate = 'checkout/onepage/shipping_method/' . strtolower($this->getCode()) . '.phtml';
$this->setTemplate($codeTemplate);
$templateFile = Mage::getBaseDir('design') . DS . $this->getTemplateFile();
if (!file_exists($templateFile)) {
$this->setTemplate(self::DEFAULT_TEMPLATE);
}
return parent::_beforeToHtml();
}
}
<?php
class Your_Module_Helper_Shipping extends Mage_Core_Helper_Abstract
public function getShippingRatesHtml($code, $rates, $sole = true)
{
$defaultBlockClass = 'Your_Module_Block_Shipping_Method_Renderer_Default';
$codeBlockClass = 'Your_Module_Block_Shipping_Method_Renderer_' . ucfirst($code);
$block = Mage::app()->getLayout()->createBlock($codeBlockClass);
if(!$block) {
$block = Mage::app()->getLayout()->createBlock($defaultBlockClass);
}
$block->setCode($code)
->setRates($rates)
->setSoleFlag($sole)
;
return $block->toHtml();
}
}
@herveguetin

This comment has been minimized.

Copy link
Owner Author

@herveguetin herveguetin commented Apr 2, 2014

Update your template file checkout/onepage/shipping_method/available.phtml and replace the <dl class="sp-methods"> with the one from shipping_method_renderer_available.phtml.

@herveguetin

This comment has been minimized.

Copy link
Owner Author

@herveguetin herveguetin commented Apr 17, 2014

Class in shipping_method_renderer_default_block.php is responsible for instanciating a renderer for a given carrier code.
If carrier has it own block class (Your_Module_Block_Shipping_Method_Renderer_[carrier_code]), it will be used.
Otherwise, default block class is used : Your_Module_Block_Shipping_Method_Renderer_Default

@herveguetin

This comment has been minimized.

Copy link
Owner Author

@herveguetin herveguetin commented Apr 17, 2014

shipping_method_renderer_helper.php is the default shipping method renderer.
If there is a template for the carrier of the shipping method like checkout/onepage/shipping_method/[carrier_code].phtml, it will be used.
Otherwise, checkout/onepage/shipping_method/default.phtml is used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment