0

How can I set an expiration date on a product variation?

I am building a webshop and my goal is to have an expiration date on a product variation, meaning the after a scheduled date, the product would no longer be available to sell, so it would automatically unpublish. Also, vice versa, the product variation might have a publishing date.

Currently I am looking at scheduler, but that is for nodes, not for products. Is there another way of doing this?

2
  • i guess, you should create custom cron, retrieve product data and test on expiration date field and unpublish all products has expiration date in the past, and then configure it to be executed for example each day or less, if your products expire fast, i think there is no contrib module for this take a look drupal.org/project/commerce/issues/3097086
    – berramou
    Commented Oct 23, 2020 at 15:37
  • 1
    @NeonBlank Please refrain from asking What's the best.... As that depends who you ask, and also what's "best" may vary per project requirements.
    – No Sssweat
    Commented Oct 24, 2020 at 4:21

1 Answer 1

0

Job Scheduler

Provides an API to schedule jobs. Where you can programmatically unpublish product variation in worker callback.

Simple example:

// Declare scheduler.

  function example_cron_job_scheduler_info() {
    $schedulers = [];
    $schedulers['example_unpublish'] = [
      'worker callback' => 'example_unpublish_nodes',
    ];
    return $schedulers;
  }

// Add a job.

  $job = [
    'name' => 'example_unpublish',
    'type' => 'story',
    'id' => 12, // must be unique
    'crontab' => * 3 * * 0, // Every Sunday at 3 AM
    'periodic' => TRUE, // set to false if you want to just run once.
  ];
  $service = \Drupal::service('job_scheduler.manager');
  $service->set($job);

// Work off a job.

  function example_update_users_total(\Drupal\job_scheduler\Entity\JobSchedule $job) {
    // Do stuff.
  }

There is more examples & info in the READ.ME, so read it.


Ultimate Cron

Provides a UI where you can schedule all the cron jobs. You can implement your own hook_cron and then schedule it using Ultimate Cron.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.