If the shipping method is estimated on the cart page or checkout page, you should be able to get the title of the shipping method with:
Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingDescription();
Please keep in mind that this will return an empty string if it wasn't chosen on the cart page or checkout.
I added the title on top of the cart. in
app/design/frontend/rwd/default/template/checkout/cart.phtml
echo "<pre>" . Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingDescription() . "</pre><br />";
The solution es this:
First I created a helper to manage the options (shipping methods) And then use it in the Model.
Helper:
namespace TCK\ASM\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use TCK\ASM\Model\Source\Method;
/**
* Class Group
* @package TCK\ASM\Helper
*/
class Group extends AbstractHelper
{
protected $resourceConnection;
protected $asm;
public function __construct(
\Magento\Framework\App\ResourceConnection $resourceConnection,
\TCK\ASM\Model\Source\Method $asm
)
{
$this->resourceConnection = $resourceConnection;
$this->asm = $asm;
}
/**
* Stub
* @return string
*/
public function getGroupData($id)
{
$db = $this->resourceConnection->getConnection();
$result = $db->query('SELECT * FROM customer_group WHERE customer_group_id=' . $id);
$row = $result->fetch();
$values=$row['shipping_method'];
$split=explode(",",$values);
$count = count($split);
$metodos=$this->asm->toOptionArray();
$i=1;
$s=0;
for ($i; $i <= $count; $i++) {
$info[$i]=$metodos[$split[$s]];
$s++;
}
return $info;
}
public function getAllMethods()
{
$metodos=$this->asm->toOptionArray();
return $metodos;
}
}
Model:
namespace TCK\ASM\Model\Carrier;
use Magento\Quote\Model\Quote\Address\RateRequest;
use Magento\Shipping\Model\Rate\Result;
use Magento\Checkout\Model\Cart;
use Magento\Shipping\Model\Carrier\AbstractCarrier;
use Magento\Shipping\Model\Carrier\CarrierInterface;
use TCK\ASM\Model\Source\Method;
use TCK\ASM\Helper\Group;
class ASMShipping extends \Magento\Shipping\Model\Carrier\AbstractCarrier implements
\Magento\Shipping\Model\Carrier\CarrierInterface
{
/**
* Code of the carrier
* @var string
*/
protected $_code='ASMShipping';
/**
* @var \Magento\Shipping\Model\Rate\ResultFactory
*/
protected $_rateResultFactory;
/**
* @var \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory
*/
protected $_rateMethodFactory;
/**
* Rate result data
*
* @var Result
*/
protected $_result;
/**
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory
* @param \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory
* @param Cart $cart
* @param \TCK\ASM\Helper\Group $helper
* @param \Magento\Framework\App\Filesystem\DirectoryList $directory_list
* @param array $data
*/
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory,
\Psr\Log\LoggerInterface $logger,
\Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory,
\Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory,
\Magento\Checkout\Model\Cart $cart,
\Magento\Framework\App\Filesystem\DirectoryList $directory_list,
\TCK\ASM\Helper\Group $helper,
\Magento\Framework\App\ResourceConnection $resourceConnection,
array $data = []
) {
$this->_rateResultFactory = $rateResultFactory;
$this->_rateMethodFactory = $rateMethodFactory;
$this->directory_list = $directory_list;
$this->cart = $cart;
$this->helper = $helper;
$this->resourceConnection = $resourceConnection;
parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
}
/**
* Returns array of key-value pairs of all available methods
* @return array
*/
public function getAllowedMethods()
{
$group_info=$this->helper->getAllMethods();
$carriers=array();
foreach ($group_info as &$carrier) {
$carriers[$group_info['label']]=$group_info['label'];
}
return $carriers;
}
/**
* @param RateRequest $request
* @return bool|Result|null
*/
public function collectRates(RateRequest $request)
{
if (!$this->getConfigFlag('active')) {
return false;
}
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->create('Magento\Customer\Model\Session');
if ($customerSession->isLoggedIn()) {
$gid=$customerSession->getCustomerGroupId();
$group_info=$this->helper->getGroupData($gid);
}
$err = null;
$envio = array();
/** @var \Magento\Shipping\Model\Rate\Result $result */
//$result = $this->_rateResultFactory->create();
$this->_result = $this->_rateResultFactory->create();
/** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
$method = $this->_rateMethodFactory->create();
$method->setCarrier('ASMShipping');
$method->setCarrierTitle($this->getConfigData('title'));
$method->setMethod('ASMShipping');
$method->setMethodTitle($this->getConfigData('name'));
$amount=0;
//Shipping logic
$ASM10=$this->_getQuote(0,$precioPeso, $archivo, $usarTableRate, $cp_cliente, $envio["peso"], $pais_cliente);
$ASM14=$this->_getQuote(1,$precioPeso, $archivo, $usarTableRate, $cp_cliente, $envio["peso"], $pais_cliente);
$ASM24=$this->_getQuote(2,$precioPeso, $archivo, $usarTableRate, $cp_cliente, $envio["peso"], $pais_cliente);
$ECONOMY=$this->_getQuote(3,$precioPeso, $archivo, $usarTableRate, $cp_cliente, $envio["peso"], $pais_cliente);
return $this->getResult();
}
/**
* Get result of request
* @return Result
*/
public function getResult()
{
return $this->_result;
}
/**
* @return $this
*/
protected function _getQuote($grupo, $precioPeso, $archivo, $usarTableRate, $cp_cliente, $peso, $pais_cliente)
{
$group_info=$this->helper->getAllMethods();
$servicio = $group_info[$grupo]['label'];
$envio["metodo"]["servicio"]=$group_info[$grupo]['servicio'];
$envio["metodo"]["horario"]=$group_info[$grupo]['horario'];
/*Tarifas*/
$tarifas = array();
//Logic stuff
$rate = $this->_rateMethodFactory->create();
$rate->setCarrier($this->_code);
$rate->setCarrierTitle($this->getConfigData('title'));
$rate->setMethodTitle($group_info[$grupo]['label']);
$rate->setPrice($importe);
$rate->setCost(0);
$this->_result->append($rate);
return $this;
}
}
Best Answer
If you open this class you can able to see some getter/setter for address.
So inside shipping method you have RateRequest $request object. You can access this by following way:
etc.