Search code examples
phpmodulecartprestashop-1.7discount

Fetch discounts list using PHP in Prestashop 1.7


The site I'm working on requires me to fetch the available discount for a product and let the customer select the discount from the available options. I tried the below code from How to get the discount amount code of voucher applied in prestashop to fetch the discounts but I'm getting an empty array.

Below being the code on Prestashop controller for fetching discounts.

$context = Context::getContext();
$result = $context->cart->getCartRules(10);
$response = array('status' => true, "message" => $module->l('It works !'), 'result' => $result);

I'm getting an empty array in the result.

enter image description here

I'm not sure what am I doing wrong.


Solution

  • I figured out a way to do it without the ajax call. What must be done is just create a discount for a product in the Prestashop admin backend. And then display the discounts in the front end template files using the cart class.

    {foreach from=$cart.discounts item=discount}
      <li class="cart-summary-line">
        <input type="radio" name="trade-in-discount" value="{$discount.code}"><span class="label">{$discount.name}</span>
      </li>
    {/foreach}