0

I have an XML to import products to Drupal Commerce site. But some products are duplicates. Duplicate products are having "=" at the end of their title, but NOT all products with "=" at the end of their title are duplicates.

What I need is, on importing XML, check whether product exists, if exist, do not import (easy), if product has "=" at the end of title, check whether product WITHOUT "=" at the end of the title exist. If exist, do not import, else import.

I am totally lost how to do that, should I write some feeds tamper module to handle such a behaviour or go completely outside the drupal and parse XML, compare it to current product database of drupal and export new XML to import to drupal? Any advice would be greatly appreciated!

1

Set product title as unique

First, you'll need to set the product title as unique. This way, Feeds will only import one product per title.

Is the "=" at the end important to the product title? If not, you'll need to find a way to remove that sign from the product title. This way, Feeds will treat a product title with an "=" as equal to the same product title without the "=".

Find replace Tamper plugin (D7 and D8)

If you are certain that the sign "=" is only used at the end of the product title (or meaningless if it appears anywhere else), you can use the easiest solution: Use the Tamper plugin "Find replace" to replace "=" with "". This plugin is available for both D7 and D8.

Find replace REGEX Tamper plugin (currently D7 only)

If the "=" sign can appear elsewhere in the product title and is important there, you'll need to use the "Find replace REGEX" tamper plugin. In the field "REGEX to find", enter a regex that matches strings ending with a "=":

/\=$/

And replace with an empty string.

D7 or D8?

At the moment of writing, the Tamper plugin "Find replace REGEX" has not been ported to D8 yet (see https://www.drupal.org/project/tamper/issues/2975179 for the status). If you are using D8 and the Tamper plugin "Find replace" does not suffice, you'll to create a custom module and listen to the Feeds parse event in order to apply modifications on the product titles.

Example:

namespace Drupal\mymodule\EventSubscriber;

class ProductFeedSubscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[FeedsEvents::PARSE][] = ['afterParse', FeedsEvents::AFTER];
    return $events;
  }

  /**
   * Act on parser result.
   */
  public function afterParse(ParseEvent $event) {
    // Check if this is the feed we want to edit.
    $feed_type_id = $event->getFeed()->getType()->id();
    if ($feed_type_id != 'myfeedtype') {
      // Not the feed type we want to edit. Abort.
      return;
    }

    /** @var \Drupal\feeds\Result\ParserResultInterface */
    $parser_result = $event->getParserResult();

    /** @var \Drupal\feeds\Feeds\Item\ItemInterface */
    foreach ($parser_result as $item) {
      // Get product title.
      $title = $item->get('title');

      // Remove "=" sign from the end if there is any.
      $title = preg_replace('/\=$', '', $title);

      // Set title back on item.
      $item->set('title', $title);
    }
  }

}

Be sure to replace "myfeedtype" in the example above with the name of the feed type that you created for importing products.

An event listener always needs to be declared in your module's 'services.yml' file:

services:
  mymodule.product_feed:
    class: Drupal\mymodule\EventSubscriber\ProductFeedSubscriber
    tags:
      - {name: event_subscriber}

In the D7 version, you would need to implement the hook hook_feeds_after_parse() to alter the parsed result.

  • Thank you very much for a very detailed explanation! I am using D7. Based on your explanation I assume feeds tamper functions happen before importing, not after, Now I am curious, I am trying to set Title of commerce product to be a unique, but I am using x-path parser module and it has no option to set title value as unique. – Lub Aug 7 '18 at 13:46
  • So, I tried to set a product title as unique, but it did not worked by any way. Then I somewhere read that GUID must not have to be only a number, so I set GUID to be mapped to @Title, and then I proceeded with your suggestions. It works nicely so I mark this answer as correct. Even field validation module did not helped me to set title as unique in feeds import. – Lub Aug 8 '18 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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