Jump to content

Exactly how would I use the hooks hookActionPresentProduct and hookActionPresentProductListing?


Recommended Posts

Both those hooks seem very useful to manipulate product data before displaying the products, but the 'product' and 'presentedProduct' elements of $params are private and protected respectively. There don't seem to be any setters available either to change or add values, so how would I use those hooks to manipulate the data?

 

Link to comment
Share on other sites

Posted (edited)
41 minutes ago, Daresh said:

There are methods to use, look for offsetSet method of the presented product.

Can you be a little more specific?


For instance, I'm looking to set 'show_price' to false for some specific products under certain conditions.

 

But
$params['presentedProduct']->offsetSet('show_price' false);

or 

$params['presentedProduct']['show_price'] = false;

will both raise an exception, since 'show_price' is 'blocked' by a getter on the ProductLazyArray.

 

PrestaShop\PrestaShop\Core\Exception\CoreException: Trying to set the index show_price of the LazyArray PrestaShop\PrestaShop\Adapter\Presenter\Product\ProductLazyArray already defined by a method is not allowed

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

54 minuty temu, zunxunz napisał:

Can you be a little more specific?


For instance, I'm looking to set 'show_price' to false for some specific products under certain conditions.

 

But
$params['presentedProduct']->offsetSet('show_price' false);

or 

$params['presentedProduct']['show_price'] = false;

will both raise an exception, since 'show_price' is 'blocked' by a getter on the ProductLazyArray.

 

PrestaShop\PrestaShop\Core\Exception\CoreException: Trying to set the index show_price of the LazyArray PrestaShop\PrestaShop\Adapter\Presenter\Product\ProductLazyArray already defined by a method is not allowed

For that you should check Filter hooks.
filterCategoryContent
filterProductContent
filterProductSearch
Usually there hooks are called right before displaying. 

For offsetSet check AbstractLazyArray.

Cytat

    public function offsetSet($offset, $value, $force = false): void

    {

        if (!$force && $this->arrayAccessList->offsetExists($offset)) {

            $result = $this->arrayAccessList->offsetGet($offset);

            if ($result['type'] !== 'variable') {

                throw new RuntimeException('Trying to set the index ' . print_r($offset, true) . ' of the LazyArray ' . get_class($this) . ' already defined by a method is not allowed');

            }

        }

        $this->arrayAccessList->offsetSet($offset, [

            'type' => 'variable',

            'value' => $value,

        ]);

    }

 

Link to comment
Share on other sites

On 8/18/2025 at 12:44 PM, zunxunz said:

Both those hooks seem very useful to manipulate product data before displaying the products, but the 'product' and 'presentedProduct' elements of $params are private and protected respectively. There don't seem to be any setters available either to change or add values, so how would I use those hooks to manipulate the data?

 

You receive a ProductLazyArray in $params['presentedProduct']

To change a value you use offsetSet() because the properties are protected
 

public function hookActionPresentProductListing($params)
{
    $product = $params['presentedProduct'];
    $product->offsetSet('show_price', false);
}

This allows you to change or inject values before the product is rendered on the front end

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...