Enable Time Picking When Setting Specific Price

Enable Time Picking When Setting Specific Price

Sometime if you browse the category page when you have so many out of stock items, there may be a lot of out of stock products are dominate the page and this looks bad to a customer perhaps.

Let’s see how can we place them to the last page.

TL;DR

Interestingly, this function was available in PrestaShop 1.6, but somehow they took it away in PrestaShop 1.7

In PrestaShop 1.7, this is actually an open issue since 2018. Unfortunately, PrestaShop still did not include this function in their update.

However, we can achieve this by ourselves with revising the core code.

Please note the below

  • For now, the only way to achieve this is to modify core code. Please ONLY do this if you have coding concept
  • The modification may be removed upon updating PrestaShop
  • We recommend you to do a backup first

1. Open modules/ps_facetedsearch/src/Adapter/MySQL.php

Change from

{{ form_widget(form.sp_from) }}

To

{# {{ form_widget(form.sp_from) }} #}
<div class="input-group">
    <input value="{{ form.sp_from.vars.value }}" type="datetime-local" class="form-control" id="form_step2_specific_price_sp_from" name="form[step2][specific_price][sp_from]" />
    <div class="input-group-append"></div>
</div>

Change from

{{ form_widget(form.sp_to) }}

To

{# {{ form_widget(form.sp_to) }} #}
<div class="input-group">
    <input value="{{ form.sp_from.vars.value }}" type="datetime-local" class="form-control" id="form_step2_specific_price_sp_to" name="form[step2][specific_price][sp_to]" />
    <div class="input-group-append"></div>
</div>

2. Open src/Adapter/Product/AdminProductWrapper.php, Line 240

Change from

$from = $specificPriceValues['sp_from'];
if (!$from) {
    $from = '0000-00-00 00:00:00';
}
$to = $specificPriceValues['sp_to'];
if (!$to) {
    $to = '0000-00-00 00:00:00';
}

To

if ($specificPriceValues['sp_from']) {
    $dateFrom = new \DateTime($specificPriceValues['sp_from']);
    $from = $dateFrom->format("Y-m-d H:i:s");
} else {
    $from = '0000-00-00 00:00:00';
}
if ($specificPriceValues['sp_to']) {
    $dateTo = new \DateTime($specificPriceValues['sp_to']);
    $to = $dateTo->format("Y-m-d H:i:s");
} else {
    $to = '0000-00-00 00:00:00';
}

Below we will talk about the details

Situation

Backoffice:

  • Only date can be input in the period of specific price setting. Even if you manually type the time, it would not be processed.

Database:

  • Actually date and time is valid to be stored in the field.

Action We Need to Take

  1. Make the field able for time picking
  2. Format the date & time submitted into valid format before entering database

Enable Time Picking

In PrestaShop, there are 2 kinds of php template language used, whch are Smarty and Twig. Where Twig is newer and PrestaShop are turning all Smarty code into Twig step by step.

Recently, it is common that Smarty are mostly be used in front end and Twig are mostly be used in back end. The related template file for the specific price form is in a Twig file which located at src/PrestaShopBundle/Resources/views/Admin/Product/Include/form-specific-price.html.twig

Scroll down to line 86 & 93, there are 2 shortcodes there which will be rendered to the “From” and “To” field:

{{ form_widget(form.sp_from) }}
{{ form_widget(form.sp_to) }}

We would want to hardcode the rendering and change the type to datetime-local, so that time picking will be available

See more: https://www.w3schools.com/tags/att_input_type_datetime-local.asp

{# {{ form_widget(form.sp_from) }} #}
<div class="input-group">
    <input value="{{ form.sp_from.vars.value }}" type="datetime-local" class="form-control" id="form_step2_specific_price_sp_from" name="form[step2][specific_price][sp_from]" />
    <div class="input-group-append"></div>
</div>
{# {{ form_widget(form.sp_to) }} #}
<div class="input-group">
    <input value="{{ form.sp_from.vars.value }}" type="datetime-local" class="form-control" id="form_step2_specific_price_sp_to" name="form[step2][specific_price][sp_to]" />
    <div class="input-group-append"></div>
</div>

Format The Date & Time into Valid Format

After the form submission, the data will be passed to a controller. The controller is located at src/Adapter/Product/AdminProductWrapper.php.

The corresponding data is stored in the array $specificPriceValues, where we can access the “from” & “to” data by $specificPriceValues['sp_from'] $specificPriceValues['sp_to']

However, the format will be like 2022-01-11T18:45, which we will need to format it to like 2022-01-11 18:45:00

PHP DateTime object can help. We will use the format function.

See more: https://www.php.net/manual/en/class.datetime.php

We go to the line 240 of AdminProductWrapper.php and look for:

$from = $specificPriceValues['sp_from'];
if (!$from) {
    $from = '0000-00-00 00:00:00';
}
$to = $specificPriceValues['sp_to'];
if (!$to) {
    $to = '0000-00-00 00:00:00';
}

And we change it to:

if ($specificPriceValues['sp_from']) {
    // Date & time format
    $dateFrom = new \DateTime($specificPriceValues['sp_from']);
    $from = $dateFrom->format("Y-m-d H:i:s");
} else {
    // if the field is empty
    $from = '0000-00-00 00:00:00';
}
if ($specificPriceValues['sp_to']) {
    // Date & time format
    $dateTo = new \DateTime($specificPriceValues['sp_to']);
    $to = $dateTo->format("Y-m-d H:i:s");
} else {
    // if the field is empty
    $to = '0000-00-00 00:00:00';
}

After the modification on these 2 files, you can now pick the time when creating specific price!



Start typing and press Enter to search