Le spese di spedizione sono un rompicapo sia per i programmatori sia per gli esercenti che sono alla continua ricerca di un fornitore di spedizioni che sia adatti alle esigenze aziendali, sia economiche ma anche di flussi di lavoro.
E’ per questo che molte a volte, quando il mio cliente non ha ancora valutato accordi commerciali con spedizionieri, o è nuovo del settore degli ecommerce, io propongo, per semplificare la vita allo sviluppatore, alle pratiche amministrative ed al calcolo del denaro richiesto per le spedizioni due modalità di spedizioni:
- Spedizione ordinaria – tasso fisso con arrivo in n-giorni lavorativi (l’esempio del pacco ordinario di Poste Italiane che arriva in 5 giorni lavorativi al costo di 7 euro)
- Spedizione prioritaria – tasso fisso con arrivo in 1 giorno lavorativo dopo quello di consegna (questo è l’esempio del pacco celere 1 al costo di 12 euro)
Magento permette di impostare una spedizione a tasso fisso, chiamata “flat rate” che permette di impostare un prezzo per le spedizioni; questo è un caso assai semplice e viene usato per semplificare le modalità di spedizione. E’ chiaro che per poter applicare delle tariffe flat è necessario che i prodotti siano entro un certo range di peso e volume e che le spedizioni siano relegate dentro il territorio nazionale. Molte volte però questo metodo viene usato anche come semplificazione, facendo una media delle spese di spedizione sugli acquisti.
Altre volte è necessaio impostare 2 tipi di spedizioni flat e per questo è necesarrio duplicare le spedizioni flat rate di magento. Andremo adesso a mostrarvi come fare.
Come duplicare le spedizioni flat rate
La procedura è molto semplice e consiste in 4 passi. Chiameremo la nostra seconda modalità di spedizionie flatrate2
- Creare il file app/code/core/Mage/AdminHtml/Model/System/Config/Source/Shipping/Flatrate2.php con il seguente contenuto (ho duplicato il file Flatrate.php e sostituito Flatrate con Flatrate2, questa azione sarà fatta anche nei seguenti file creati da uno preesistente)
[sourcecode language=”php”]
class Mage_Adminhtml_Model_System_Config_Source_Shipping_Flatrate2
{
public function toOptionArray()
{
return array(
array(‘value’=>”, ‘label’=> Mage::helper(‘adminhtml’)->__(‘None’)),
array(‘value’=>’O’, ‘label’=>Mage::helper(‘adminhtml’)->__(‘Per Order’)),
array(‘value’=>’I’, ‘label’=>Mage::helper(‘adminhtml’)->__(‘Per Item’)),
);
}
}
[/sourcecode]
- Creare il file app/code/core/Mage/AdminHtml/Model/System/Config/Source/Shipping/Flatrate2.php con il seguente contenuto (ho duplicato il file Flatrate.php e sostituito Flatrate con Flatrate2)
[sourcecode language=”php”]
class Mage_Shipping_Model_Carrier_Flatrate2
extends Mage_Shipping_Model_Carrier_Abstract
implements Mage_Shipping_Model_Carrier_Interface
{
protected $_code = ‘flatrate2’;
protected $_isFixed = true;
/**
* 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->getProduct()->isVirtual() || $item->getParentItem()) {
continue;
}
if ($item->getHasChildren() && $item->isShipSeparately()) {
foreach ($item->getChildren() as $child) {
if ($child->getFreeShipping() && !$child->getProduct()->isVirtual()) {
$freeBoxes += $item->getQty() * $child->getQty();
}
}
} elseif ($item->getFreeShipping()) {
$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 = ($request->getPackageQty() * $this->getConfigData(‘price’)) – ($this->getFreeBoxes() * $this->getConfigData(‘price’));
} else {
$shippingPrice = false;
}
$shippingPrice = $this->getFinalPriceWithHandlingFee($shippingPrice);
if ($shippingPrice !== false) {
$method = Mage::getModel(‘shipping/rate_result_method’);
$method->setCarrier(‘flatrate2’);
$method->setCarrierTitle($this->getConfigData(‘title’));
$method->setMethod(‘flatrate2’);
$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(‘flatrate2’=>$this->getConfigData(‘name’));
}
}
[/sourcecode]
- Modificare il file app/code/core/Mage/Shipping/etc/System.xm aggiungendo il seguente codice subito dopo la chiusura del tag flatrate all’interno di (carriers/groups)
[sourcecode language=”xml”]
<flatrate2 translate="label">
<label>Flat Rate</label>
<frontend_type>text</frontend_type>
<sort_order>2</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<fields>
<active translate="label">
<label>Enabled</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>1</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</active>
<name translate="label">
<label>Method Name</label>
<frontend_type>text</frontend_type>
<sort_order>3</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</name>
<price translate="label">
<label>Price</label>
<frontend_type>text</frontend_type>
<sort_order>5</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</price>
<handling_type translate="label">
<label>Calculate Handling Fee</label>
<frontend_type>select</frontend_type>
<source_model>shipping/source_handlingType</source_model>
<sort_order>7</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</handling_type>
<handling_fee translate="label">
<label>Handling Fee</label>
<frontend_type>text</frontend_type>
<sort_order>8</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</handling_fee>
<sort_order translate="label">
<label>Sort Order</label>
<frontend_type>text</frontend_type>
<sort_order>100</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</sort_order>
<title translate="label">
<label>Title</label>
<frontend_type>text</frontend_type>
<sort_order>2</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</title>
<type translate="label">
<label>Type</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_shipping_flatrate2</source_model>
<sort_order>4</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</type>
<sallowspecific translate="label">
<label>Ship to Applicable Countries</label>
<frontend_type>select</frontend_type>
<sort_order>90</sort_order>
<frontend_class>shipping-applicable-country</frontend_class>
<source_model>adminhtml/system_config_source_shipping_allspecificcountries</source_model>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</sallowspecific>
<specificcountry translate="label">
<label>Ship to Specific Countries</label>
<frontend_type>multiselect</frontend_type>
<sort_order>91</sort_order>
<source_model>adminhtml/system_config_source_country</source_model>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
<can_be_empty>1</can_be_empty>
</specificcountry>
<showmethod translate="label">
<label>Show Method if Not Applicable</label>
<frontend_type>select</frontend_type>
<sort_order>92</sort_order>
<source_model>adminhtml/system_config_source_yesno</source_model>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</showmethod>
<specificerrmsg translate="label">
<label>Displayed Error Message</label>
<frontend_type>textarea</frontend_type>
<sort_order>80</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</specificerrmsg>
</fields>
</flatrate2>
[/sourcecode]
- Modificare il file xml file app/code/core/Mage/Shipping/etc/config.xml aggiungendo il seguente codice all’interno di default/carriers subito dopo la definizione di flatrate:
[sourcecode language=”xml”]
<flatrate2>
<active>1</active>
<sallowspecific>0</sallowspecific>
<model>shipping/carrier_flatrate2</model>
<name>Fixed</name>
<price>5.00</price>
<title>Flat Rate 2</title>
<type>I</type>
<specificerrmsg>This shipping method is currently unavailable. If you would like to ship using this shipping method, please contact us.</specificerrmsg>
<handling_type>F</handling_type>
</flatrate2>
[/sourcecode]
Cancellate la cache, andate in sistem->configuration->shipping methods e trovere due spedizioni flat rate che potrete configurare come spedizione ordinaria e prioritaria.