Displaying Product Availability Based on Customer Login Status in PrestaShop 9.0.2
Displaying Conditional Product Availability in PrestaShop 9.0.2
A PrestaShop merchant, pcsk, sought assistance on the PrestaShop forums regarding the display of product availability messages in PrestaShop 9.0.2. The merchant aimed to show different availability messages to logged-in customers versus guest users.
In previous PrestaShop versions (e.g., 1.7.7.8), the variables $product.available_now and $product.available_later were used to display availability messages based on product quantity and PrestaShop configuration. However, in PrestaShop 9.0.2, these variables are no longer directly available.
The original poster provided a code snippet intended to display different availability messages based on login status:
{block name='product_availability'}
{if !$customer.is_logged}
{$product.available_now}
{else}
{if $product.quantity > 0}
{$product.available_now}
{else}
{$product.available_later}
{/if}
{/if}
{/block}
The code attempts to use $product.available_now and $product.available_later, which are no longer functional in PrestaShop 9.0.2.
Solution and Explanation
wepresta responded to the query, clarifying that in PrestaShop 8 and 9, $product.available_now and $product.available_later are no longer exposed by default. The only readily available variable is $product.availability_message.
While the direct solution to conditionally display messages based on login status isn't provided in the thread, the response highlights the key change in PrestaShop 8/9 regarding product availability variables. To achieve the desired conditional display, developers would need to explore alternative approaches, such as:
- Custom Module Development: Creating a module to retrieve and expose the desired availability information based on customer login status.
- Template Overrides with Logic: Implementing custom logic within the theme's template files (e.g., product.tpl) to check customer login status and then fetch or construct the appropriate availability message. This might involve querying the database directly or using PrestaShop's API to retrieve product stock information.
- Using Hooks: Utilizing PrestaShop's hook system to modify the
$product.availability_messagevariable before it's displayed in the template.
This thread serves as a crucial reminder for PrestaShop developers to be aware of changes in variable availability when migrating or upgrading to newer versions and to adapt their code accordingly.