Score:1

Programmatically update products

cf flag

I am building a site with Drupal 9 and Drupal Commerce 2 (Drupal Commerce Kickstart).

The site is complete and I have already entered all the products via Feed (there are over a million). Now I'm looking for a way to periodically update the products automatically (prices, availability, etc.)

In an old site that I had made and which was developed with Drupal 7 and did not use Drupal Commerce, it was very easy to update the nodes (products) through some scripts I had made, while in this case it is not that simple.

I thought about updating with Feed, but it's really too slow, in fact I can't update more than 20/30 products in each cron cycle; I therefore need to use a customized and faster solution. The first feed upload also took a long time.

I tried to create a php script that calls the entity of the nodes I want to modify like I did with the old site, but I can't figure out how to do it. I tried to follow this guide for Drupal 8, but I'm probably wrong to use some function or something is changed for Drupal 9.

Can someone help me?

Score:0
cn flag

Using cron to execute feeds is pretty slow, so what about executing feeds with drush?

Here is a simple drush script I use to import feeds.

Execute it like this:

drush php-script import_feeds.php

Note that you need to use the Drupal id of each feed to import by ID.

import_feeds.php

<?php

/**
 * @file
 * Import feeds.
 */

import_everything_in_order();
    
/**
 * Import all feeds in order of dependencies.
 */
function import_everything_in_order(): void {
  $id_user = 1;
  $id_media_image = 7;
  $id_page = 3;

  $feeds_to_import = [
    $id_user,
    $id_media_image,
    $id_page,
  ];
  foreach ($feeds_to_import as $feed_to_import) {
    unlock_and_import($feed_to_import);
  }
}

/**
 * Unlock a feed and then import it.
 *
 * This ensures that dependency import doesn't fail
 * due to locked feeds.
 */
function unlock_and_import(int $feed_id): void {
  exec("drush feeds:unlock $feed_id -y");
  exec("drush feeds:import $feed_id -y");
}
FraMan avatar
cf flag
Thanks, I tried drush feeds as you suggested, but the update speed of the feeds doesn't change. It is certainly a more reliable solution than using the UI, but from some initial tests it does not solve the problem of execution speed. I have to update thousands of products daily and this slowness is really a problem. @Patrick
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.