Jump to content

Get access to cart variables, array in custom module


prestashop_newuser
 Share

Recommended Posts

Hi,

 

In prestashop 1.6 I want to get the exact payment details step data in my payment module. So I just made copy the code from order-payment.tpl and used that in my payment execution smarty file. It showed couple of errors. So I just go through the codes and get those values from the cart and made it assign from my payment controller to the smarty template. Everything works fine but still I am getting one error named as

 

 

    Undefined index: gift_products .

 

 

When I checked the code in Cart.php I got it like this

 

$gift_products = array();

$gift_product = $product;

$gift_product['cart_quantity'] = 1;

$gift_product['price'] = 0;

$gift_product['price_wt'] = 0;

$gift_product['total_wt'] = 0;

$gift_product['total'] = 0;

$gift_product['gift'] = true;

$gift_products[] = $gift_product;

 

I tried to assign the value of gift_products to the smarty like this

 

public function initContent()

{

parent::initContent();

 

$cart = $this->context->cart;

$this->context->smarty->assign(array(

'gift_products' => $cart->gift_products,

));

 

$this->setTemplate('payment_execution.tpl');

}

 

 

 

But its not working at all. Can someone tell me how to use the variable/array of cart in my module? Any help and suggestions will be really appreciable. Thanks.

 

 

Share this post


Link to post
Share on other sites

 

Okay, try this code

$summary = $this->context->cart->getSummaryDetails();
$gift_products = $summary['gift_products'];

 

Thanks that worked. But also wanted to know how to assign total_tax to the smarty. I tried to assign 'total_tax' => $this->context->cart->getSummaryDetails(),

But its not working.

Share this post


Link to post
Share on other sites

Hi,

 

use the same variable $summary

$summary['total_tax']

Check the method getSummaryDetails in the Cart class, you'll see that it returns an array like this

return array(
	'delivery' => $delivery,
	'delivery_state' => State::getNameById($delivery->id_state),
	'invoice' => $invoice,
	'invoice_state' => State::getNameById($invoice->id_state),
	'formattedAddresses' => $formatted_addresses,
	'products' => array_values($products),
	'gift_products' => $gift_products,
	'discounts' => array_values($cart_rules),
	'is_virtual_cart' => (int)$this->isVirtualCart(),
	'total_discounts' => $total_discounts,
	'total_discounts_tax_exc' => $total_discounts_tax_exc,
	'total_wrapping' => $this->getOrderTotal(true, Cart::ONLY_WRAPPING),
	'total_wrapping_tax_exc' => $this->getOrderTotal(false, Cart::ONLY_WRAPPING),
	'total_shipping' => $total_shipping,
	'total_shipping_tax_exc' => $total_shipping_tax_exc,
	'total_products_wt' => $total_products_wt,
	'total_products' => $total_products,
	'total_price' => $base_total_tax_inc,
	'total_tax' => $total_tax,
	'total_price_without_tax' => $base_total_tax_exc,
	'is_multi_address_delivery' => $this->isMultiAddressDelivery() || ((int)Tools::getValue('multi-shipping') == 1),
	'free_ship' => $total_shipping ? 0 : 1,
	'carrier' => new Carrier($this->id_carrier, $id_lang),
);

Share this post


Link to post
Share on other sites

 

Hi,

 

use the same variable $summary

$summary['total_tax']

Check the method getSummaryDetails in the Cart class, you'll see that it returns an array like this

return array(
	'delivery' => $delivery,
	'delivery_state' => State::getNameById($delivery->id_state),
	'invoice' => $invoice,
	'invoice_state' => State::getNameById($invoice->id_state),
	'formattedAddresses' => $formatted_addresses,
	'products' => array_values($products),
	'gift_products' => $gift_products,
	'discounts' => array_values($cart_rules),
	'is_virtual_cart' => (int)$this->isVirtualCart(),
	'total_discounts' => $total_discounts,
	'total_discounts_tax_exc' => $total_discounts_tax_exc,
	'total_wrapping' => $this->getOrderTotal(true, Cart::ONLY_WRAPPING),
	'total_wrapping_tax_exc' => $this->getOrderTotal(false, Cart::ONLY_WRAPPING),
	'total_shipping' => $total_shipping,
	'total_shipping_tax_exc' => $total_shipping_tax_exc,
	'total_products_wt' => $total_products_wt,
	'total_products' => $total_products,
	'total_price' => $base_total_tax_inc,
	'total_tax' => $total_tax,
	'total_price_without_tax' => $base_total_tax_exc,
	'is_multi_address_delivery' => $this->isMultiAddressDelivery() || ((int)Tools::getValue('multi-shipping') == 1),
	'free_ship' => $total_shipping ? 0 : 1,
	'carrier' => new Carrier($this->id_carrier, $id_lang),
);

 

Thanks. That worked like  charm :)

Thank you for the help.

Share this post


Link to post
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
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More