Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import Price for Commerce 5 does not work #1439

Open
sandrodunkel opened this issue May 8, 2024 · 6 comments
Open

Import Price for Commerce 5 does not work #1439

sandrodunkel opened this issue May 8, 2024 · 6 comments
Labels

Comments

@sandrodunkel
Copy link

Description

I'm trying to import a lot of products using a local CSV file. The mapping and import works for all selected properties except the variant price, which is set to 0. Maybe that has something to do with the change in the way Price and Promotional Price are being handled in Commerce 5.

When checking the Debug-mode provided by feed-me, the price is being mapped correctly. I also tried not mapping the price to a CSV column but rather using the Default Value option, but this didn't work either. Price is still 0.

Steps to reproduce

  1. Setup commerce and product type
  2. Setup feed using a local csv import
  3. Map corresponding fields, including Price under Product Variant Fields
  4. Run feed
  5. Notice that all the information is being imported correctly except the price, which is set to 0.

Additional info

  • Craft version: 5.1.2
  • PHP version: 8.2.15
  • Database driver & version: MySQL 8.0.33
  • Plugins & versions:
    • CKEditor (4.0.4)
    • Craft Commerce (5.0.3)
    • Feed Me (6.0.1)
    • Formie (3.0.0-beta.10)
    • Stripe for Craft Commerce (5.0.1)
@sandrodunkel
Copy link
Author

Same thing for Inventory/stock, this structure changed in Commerce 5. Also, the setting Promotable does not seem to work as well.

@MatoTominac
Copy link

Quick hotfix as we wait for the official fix.

\yii\base\Event::on(\craft\feedme\services\Process::class, \craft\feedme\services\Process::EVENT_STEP_BEFORE_ELEMENT_SAVE, function(\craft\feedme\events\FeedProcessEvent $event) {
    $feedData = $event->feedData;
    /** @var craft\commerce\elements\Product $element */
    $element = $event->element;
    $elementType = get_class($element);
    if ($elementType === 'craft\commerce\elements\Product') {
        $defaultVariant = $element->getDefaultVariant();
        if (!$defaultVariant) {
            return;
        }
        $price = $feedData['price'] ?? null;
        if ($price) {
            $defaultVariant->setBasePrice((float)$price);
        }
    }
});

@sandrodunkel
Copy link
Author

@MatoTominac Thanks for the hotfix, although it does not work for me. It works neither with csv imported values nor with the fallback value. Were you able to test your module?

@MatoTominac
Copy link

@sandrodunkel you probably need to update the line $price = $feedData['price'] ?? null;. Try var_dump($feedData); to find the column with price info. I'm importing from JSON so it's easy to find the pricing value in $feedData array, but since you're importing from CSV I would guess you need the column index with price value, so something like:

$price = $feedData[7] ?? null; // 7 is the index of pricing column in csv

@sandrodunkel
Copy link
Author

@MatoTominac Thanks for the hint! I got it working—I made an upper/lowercase error… :S

I adjusted the code to import the price for each variant and not just the default one:

if ($elementType === 'craft\commerce\elements\Product') {
  $variants = $element->getVariants();

  foreach ($variants as $variant) {
    $price = $feedData["Price"] ?? null;
    if ($price) {
      $variant->setBasePrice((float)$price);
    }
  }
}

I'm going to try to do something similar with the stock for now.

@lukeholder
Copy link
Member

We have a Commerce 5 compatibility PR #1448, to test it out change your craftcms/feed-me requirement in composer.json to:

"require": {
  "craftcms/feed-me": "dev-feature/commerce-5-support as 6.0.1"
  "...": "..."
}

Then run composer update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants