Search code examples
phpprestashopsmartyprestashop-1.7

Prestashop - Full-Product-Description in Checkout


I thought this would be quite easy but it turned out to be a tough task.

My client requested me to implement the complete product description inside the order confirmation table inside the checkout.

At the moment we just have the short description:

File: themes/newtheme/templates/checkout/_partials/order-confirmation-table.tpl

  <div class="order-confirmation-table">
    {block name='order_confirmation_table'}
      {foreach from=$products item=product}
        <div class="order-line row">

          {$product->description_short nofilter}

        </div>
      {/foreach}
    {/block}
  </div>

I thought that based on this I would just need to change the attribute that I am accessing like: {$product->description nofilter}

But then it revealed that the $product class, inside the checkout is not the normal product class. It is combined with the abstraction layer LazyArray.

As described in the developer manual from prestashop they just had updated the LazyArrays in version 1.7.5 that you habe to access them via -> . Sadly already tried:

File: themes/newtheme/templates/checkout/_partials/order-confirmation-table.tpl

  <div class="order-confirmation-table">
    {block name='order_confirmation_table'}
      {foreach from=$products item=product}
        <div class="order-line row">

          {$product->description nofilter}

        </div>
      {/foreach}
    {/block}
  </div>

Does not work

Then I debug the $product->description attribute. Its an empty array.

So i would be really happy if someone could help me understanding the lazy array and get the description


Solution

  • The "description" field is not in the "whitelist" of the attributes allowed in front.

    If you open the file "src/Core/Filter/frontEndObject/ProductFilter.php", you will the the whitelist without "description".

    Src/core/ files cannot be overrided so you will have to create a module and register to the hook "ActionFrontControllerAfterInit"

    In your hook function, call the service, get the filters and add description to the whitelist :

    public function hookActionFrontControllerAfterInit()
    {
        $filterManager = $this->get('prestashop.core.filter.front_end_object.main');
    
        // get list of all filters applied to client-side data
        $filters = $filterManager->getFilters();
    
        // get list of all filters applied to the cart object
        $cartFilters = $filters['cart']->getFilters();
    
        // get list of filters applied to each product inside the cart object
        $productFilterQueue = $cartFilters['products']->getQueue();
    
        foreach ($productFilterQueue as $filter) {
            if ($filter instanceof PrestaShop\PrestaShop\Core\Filter\FrontEndObject\ProductFilter) {
                $filter->whitelist(array('description'));
            }
        }
    }
    

    You can find some documentation here http://build.prestashop.com/news/exposing-data-with-confidence/