Jump to content

[SOLVED] Preview product shipping cost in product page - Prestashop 1.6


Recommended Posts

Hi, Prestashop community. I'm trying to preview the product shipping cost from the default shipping carrier.

 

So far, I've been able to find these 2 solutions, but none of them work in prestashop 1.6:

 

1.- add this code to product.php:

 

$carrier = new Carrier(33, intval($cookie->id_lang));
$shippingCost = $carrier->getDeliveryPriceByWeight($product->weight, 7);
$smarty->assign('shippingCost', $shippingCost);
 
and then this to product.tpl:
 
{convertPrice price=$shippingCost}
 
2.- add this code to productcontroller.php:
 
$id_carrier = Configuration::get('PS_CARRIER_DEFAULT');
  $carrier = new Carrier((int)($id_carrier), Configuration::get('PS_LANG_DEFAULT'));
  $carrierTax = Tax::getCarrierTaxRate((int)$carrier->id, (int)$this->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
  $defaultCountry = new Country(Configuration::get('PS_COUNTRY_DEFAULT'), Configuration::get('PS_LANG_DEFAULT'));
  $id_zone = (int)$defaultCountry->id_zone;
  $portes = $carrier->getDeliveryPriceByWeight($this->product->weight, $id_zone)*1;
  $portes = $portes *(1+ $carrierTax/100);
   self::$smarty->assign(array(
    'portes' => $portes
   ));
 
and then this to product.tpl:
 
{$portes}
 
The problem seems to be which smarty variables are been called for this. Hopefully someone else has a clue so this can be achieved.
 
Thanks in advance.

 

Edited by moy2010 (see edit history)
Link to comment
Share on other sites

Solution provided by PascalVG (Thanks, pal!):

 

$default_carrier =new Carrier((int)Configuration::get('PS_CARRIER_DEFAULT'));
$carrier_zones = $default_carrier->getZones();
if (isset($carrier_zones) && !empty($carrier_zones)) {
    $first_carrier_zone = $carrier_zones[0]['id_zone'];
    $delivery_price = $default_carrier->getDeliveryPriceByWeight($this->product->weight, $first_carrier_zone);
}
else 
    $delivery_price = 'not found';
 
$this->context->smarty->assign(array(
    'stock_management' => Configuration::get('PS_STOCK_MANAGEMENT'),
    'customizationFields' => ($this->product->customizable) ? $this->product->getCustomizationFields($this->context->language->id) : false,
    'accessories' => $this->product->getAccessories($this->context->language->id),
    'return_link' => $return_link,
    'product' => $this->product,
    'product_manufacturer' => new Manufacturer((int)$this->product->id_manufacturer, $this->context->language->id),
    'token' => Tools::getToken(false),
    'features' => $this->product->getFrontFeatures($this->context->language->id),
    'attachments' => (($this->product->cache_has_attachments) ? $this->product->getAttachments($this->context->language->id) : array()),
    'allow_oosp' => $this->product->isAvailableWhenOutOfStock((int)$this->product->out_of_stock),
    'last_qties' =>  (int)Configuration::get('PS_LAST_QTIES'),
    'HOOK_EXTRA_LEFT' => Hook::exec('displayLeftColumnProduct'),
    'HOOK_EXTRA_RIGHT' => Hook::exec('displayRightColumnProduct'),
    'HOOK_PRODUCT_OOS' => Hook::exec('actionProductOutOfStock', array('product' => $this->product)),
    'HOOK_PRODUCT_ACTIONS' => Hook::exec('displayProductButtons', array('product' => $this->product)),
    'HOOK_PRODUCT_TAB' =>  Hook::exec('displayProductTab', array('product' => $this->product)),
    'HOOK_PRODUCT_TAB_CONTENT' =>  Hook::exec('displayProductTabContent', array('product' => $this->product)),
    'display_qties' => (int)Configuration::get('PS_DISPLAY_QTIES'),
    'display_ht' => !Tax::excludeTaxeOption(),
    'currencySign' => $this->context->currency->sign,
    'currencyRate' => $this->context->currency->conversion_rate,
    'currencyFormat' => $this->context->currency->format,
    'currencyBlank' => $this->context->currency->blank,
    'jqZoomEnabled' => Configuration::get('PS_DISPLAY_JQZOOM'),
    'ENT_NOQUOTES' => ENT_NOQUOTES,
    'outOfStockAllowed' => (int)Configuration::get('PS_ORDER_OUT_OF_STOCK') ,  // <- add comma here!
    'delivery_price' => $delivery_price
    ));
 

 

 

 

Then, in themes/<your theme folder>product.tpl, add this code wherever you need it:

 

{if $delivery_price}
    <div class = "estimated_delivery_price">
        {l s='Initial delivery costs estimate :'}{convertPrice price=$delivery_price}
    </div>
{/if}

 

  • Like 1
Link to comment
Share on other sites

  • 3 months later...

 

Solution provided by PascalVG (Thanks, pal!):

 

$default_carrier =new Carrier((int)Configuration::get('PS_CARRIER_DEFAULT'));
$carrier_zones = $default_carrier->getZones();
if (isset($carrier_zones) && !empty($carrier_zones)) {
    $first_carrier_zone = $carrier_zones[0]['id_zone'];
    $delivery_price = $default_carrier->getDeliveryPriceByWeight($this->product->weight, $first_carrier_zone);
}
else 
    $delivery_price = 'not found';
 
$this->context->smarty->assign(array(
    'stock_management' => Configuration::get('PS_STOCK_MANAGEMENT'),
    'customizationFields' => ($this->product->customizable) ? $this->product->getCustomizationFields($this->context->language->id) : false,
    'accessories' => $this->product->getAccessories($this->context->language->id),
    'return_link' => $return_link,
    'product' => $this->product,
    'product_manufacturer' => new Manufacturer((int)$this->product->id_manufacturer, $this->context->language->id),
    'token' => Tools::getToken(false),
    'features' => $this->product->getFrontFeatures($this->context->language->id),
    'attachments' => (($this->product->cache_has_attachments) ? $this->product->getAttachments($this->context->language->id) : array()),
    'allow_oosp' => $this->product->isAvailableWhenOutOfStock((int)$this->product->out_of_stock),
    'last_qties' =>  (int)Configuration::get('PS_LAST_QTIES'),
    'HOOK_EXTRA_LEFT' => Hook::exec('displayLeftColumnProduct'),
    'HOOK_EXTRA_RIGHT' => Hook::exec('displayRightColumnProduct'),
    'HOOK_PRODUCT_OOS' => Hook::exec('actionProductOutOfStock', array('product' => $this->product)),
    'HOOK_PRODUCT_ACTIONS' => Hook::exec('displayProductButtons', array('product' => $this->product)),
    'HOOK_PRODUCT_TAB' =>  Hook::exec('displayProductTab', array('product' => $this->product)),
    'HOOK_PRODUCT_TAB_CONTENT' =>  Hook::exec('displayProductTabContent', array('product' => $this->product)),
    'display_qties' => (int)Configuration::get('PS_DISPLAY_QTIES'),
    'display_ht' => !Tax::excludeTaxeOption(),
    'currencySign' => $this->context->currency->sign,
    'currencyRate' => $this->context->currency->conversion_rate,
    'currencyFormat' => $this->context->currency->format,
    'currencyBlank' => $this->context->currency->blank,
    'jqZoomEnabled' => Configuration::get('PS_DISPLAY_JQZOOM'),
    'ENT_NOQUOTES' => ENT_NOQUOTES,
    'outOfStockAllowed' => (int)Configuration::get('PS_ORDER_OUT_OF_STOCK') ,  // <- add comma here!
    'delivery_price' => $delivery_price
    ));
 

 

 

 

Then, in themes/<your theme folder>product.tpl, add this code wherever you need it:

 

{if $delivery_price}
    <div class = "estimated_delivery_price">
        {l s='Initial delivery costs estimate :'}{convertPrice price=$delivery_price}
    </div>
{/if}

 

Where does the first part needs to be added?

Link to comment
Share on other sites

  • 5 months later...
  • 2 months later...
  • 8 months later...
  • 4 months later...
  • 2 years later...
On 4/19/2017 at 7:52 AM, learner88 said:

I want to add a specific line (i.e free delivery) on some products on product page, how can i do that??

please help me out.

Hi if you want to display a banner on a product page depending of the product price, you can add code in header.tpl or product.tpl (depending where you want to display the banner) with a :

{if $productPrice > 600}
<div id="my-banner">Free delivery !</div>
{/if}

 

Link to comment
Share on other sites

  • 10 months later...
On 4/6/2015 at 9:08 AM, moy2010 said:

Solution provided by PascalVG (Thanks, pal!):

 

$default_carrier =new Carrier((int)Configuration::get('PS_CARRIER_DEFAULT'));
$carrier_zones = $default_carrier->getZones();
if (isset($carrier_zones) && !empty($carrier_zones)) {
    $first_carrier_zone = $carrier_zones[0]['id_zone'];
    $delivery_price = $default_carrier->getDeliveryPriceByWeight($this->product->weight, $first_carrier_zone);
}
else 
    $delivery_price = 'not found';
 
$this->context->smarty->assign(array(
    'stock_management' => Configuration::get('PS_STOCK_MANAGEMENT'),
    'customizationFields' => ($this->product->customizable) ? $this->product->getCustomizationFields($this->context->language->id) : false,
    'accessories' => $this->product->getAccessories($this->context->language->id),
    'return_link' => $return_link,
    'product' => $this->product,
    'product_manufacturer' => new Manufacturer((int)$this->product->id_manufacturer, $this->context->language->id),
    'token' => Tools::getToken(false),
    'features' => $this->product->getFrontFeatures($this->context->language->id),
    'attachments' => (($this->product->cache_has_attachments) ? $this->product->getAttachments($this->context->language->id) : array()),
    'allow_oosp' => $this->product->isAvailableWhenOutOfStock((int)$this->product->out_of_stock),
    'last_qties' =>  (int)Configuration::get('PS_LAST_QTIES'),
    'HOOK_EXTRA_LEFT' => Hook::exec('displayLeftColumnProduct'),
    'HOOK_EXTRA_RIGHT' => Hook::exec('displayRightColumnProduct'),
    'HOOK_PRODUCT_OOS' => Hook::exec('actionProductOutOfStock', array('product' => $this->product)),
    'HOOK_PRODUCT_ACTIONS' => Hook::exec('displayProductButtons', array('product' => $this->product)),
    'HOOK_PRODUCT_TAB' =>  Hook::exec('displayProductTab', array('product' => $this->product)),
    'HOOK_PRODUCT_TAB_CONTENT' =>  Hook::exec('displayProductTabContent', array('product' => $this->product)),
    'display_qties' => (int)Configuration::get('PS_DISPLAY_QTIES'),
    'display_ht' => !Tax::excludeTaxeOption(),
    'currencySign' => $this->context->currency->sign,
    'currencyRate' => $this->context->currency->conversion_rate,
    'currencyFormat' => $this->context->currency->format,
    'currencyBlank' => $this->context->currency->blank,
    'jqZoomEnabled' => Configuration::get('PS_DISPLAY_JQZOOM'),
    'ENT_NOQUOTES' => ENT_NOQUOTES,
    'outOfStockAllowed' => (int)Configuration::get('PS_ORDER_OUT_OF_STOCK') ,  // <- add comma here!
    'delivery_price' => $delivery_price
    ));
 

 

 

 

Then, in themes/<your theme folder>product.tpl, add this code wherever you need it:

 

{if $delivery_price}
    <div class = "estimated_delivery_price">
        {l s='Initial delivery costs estimate :'}{convertPrice price=$delivery_price}
    </div>
{/if}

 

My product have attribute with impact on weight, how to calcute the shipping price based also on attribute weight?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...