Last active
August 8, 2023 08:38
-
-
Save herveguetin/9944646 to your computer and use it in GitHub Desktop.
Use renderers for shipping methods in Magento
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
} |
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
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