Page 1 of 1

Update Easy Admin Specials

Posted: Tue Dec 05, 2023 8:46 am
by Pierre_P
I'm trying to update this admin specials to most recent changes but not getting past the dates that needs to be saved correctly into the database.
For some reason and ability i cant figure out where the issue are.
The start or end dates, if they are left empty it will save date as 0000-00-00 00:00:00 which is invalid and needs to be null if empty.

Anyone for a quick look perhaps?

Phoenix 1.0.8.20 with 8.21 updates, php8.2

Re: Update Easy Admin Specials

Posted: Tue Dec 05, 2023 10:53 am
by burt
This is one of those addons where the idea is great, the code is so old and just about impossible to properly update. It might be worth exploring to see if there are any other shopowners who might chip in to the cost and get the idea of the addon remade to modern coding standard.


Dates

The general idea with dates was to simplify simplify simplify using the HTML5 date system for display along with minor logic to write to and get from the database. The gremlin here is the TIME part of the date. The HTML datepicker requires DATE only, but Phoenix requires DATE & TIME, so inserting into the DB we add the time, then displaying we remove the time.

Example Display

Code: Select all

  <div class="form-group row" id="zDate">
      <label for="specialDate" class="col-form-label col-sm-3 text-left text-sm-right"><?= TEXT_SPECIALS_EXPIRES_DATE ?></label>
      <div class="col-sm-9">
        <?= new Input('expdate', ['min' => date('Y-m-d'), 'class' => 'form-control w-25', 'value' => substr($sInfo->expires_date ?? '', 0, 10), 'onfocus' => 'this.showPicker?.()'], 'date') ?>
      </div>
    </div>
This shows an date input box called "expdate"; 'expdate'
which is set to today as the earliest date that can be chosen; 'min' => date('Y-m-d')
set to the relevant "form" bootstrap class for display; 'class' => 'form-control w-25'
and showing the first 10 characters [in other words removing the time of the expiry date taken from the database [or Nothing if no date]; 'value' => substr($sInfo->expires_date ?? '', 0, 10)
and set to open the datepicker when the input box is clicked [this is not needed, but gives a slightly nicer experience]; 'onfocus' => 'this.showPicker?.()'

Inserting into Database

From the display of the datepicker, data would be _POST'd as date only (eg); 2023-12-25
You need to do two things here. Add in TIME, then SAVE to database.

Adding Time

Code: Select all

  $expdate = Text::input($_POST['expdate']);
  if (Text::is_empty($expdate)) {
    $expires_date = 'NULL';
  } else {
    $expires_date = date($expdate . ' H:i:s', strtotime('tomorrow -1 second'));
  }
First see if any DATE was _POST'd; if (Text::is_empty($expdate)) {
If NO, set expiry date to NULL; $expires_date = 'NULL';
If YES, add TIME; $expires_date = date($expdate . ' H:i:s', strtotime('tomorrow -1 second'));

That YES, is slightly esoteric, but it basically says;

a/ what is the _POST'd expiry date
b/ set that date to the day after
c/ remove 1 second

So, example;

a/ 2023-12-25
b/ 2023-12-26 00:00:00
c/ 2023-12-25 23:59:59

At this point, $expiry_date = 2023-12-25 23:59:59;
Which is the date the user chose, and logic sets the end of the day.

Now you can insert to DB;

Code: Select all

  $db->perform('specials', [
    'products_id' => (int)$products_id,
    'specials_new_products_price' => $specials_price,
    'specials_date_added' => 'NOW()',
    'expires_date' => $expires_date,
    'status' => 1,
  ]);
This piece; 'expires_date' => $expires_date,

will either be 'NULL'
Or be '2023-12-25 23:59:59'

I hope this helps.

Re: Update Easy Admin Specials

Posted: Tue Dec 05, 2023 1:07 pm
by Pierre_P
Thanks for this explanation @burt

For now when saving a special, database updates the NULL dates as empty values and not the NULL value giving me all zeros on date.

Let me take a short break and work through this step by step as you explained and see what i can come up with.

Re: Update Easy Admin Specials

Posted: Tue Dec 05, 2023 1:37 pm
by raiwa
I do not know this one, but maybe advanced specials can replace it:
https://phoenixcart.org/forum/app.php/ ... _specials/

Re: Update Easy Admin Specials

Posted: Wed Dec 06, 2023 6:34 am
by Pierre_P
Hey @raiwa
The only reason i have to fix this is because the column expires_date must not be saved as 0000-00-00 00:00:00
For the discount coupon addon, when the date is like this and you have your coupon code set to exclude specials then coupon can still be used on special items - too much too late ;)

Buying your Easy Discount addon(for me it is already outdated with the upcoming date picker) will save me many hours of figuring out how to get a date correctly displayed and saved and in the process i hopefully learn something new and apart from this i will modify it straight away to add filter for an addon Vendors whereby each item is assigned to a vendor.