craftcms / feed-me

Craft CMS plugin for importing entry data from XML, RSS or ATOM feeds—routine task or on-demand.
Other
289 stars 139 forks source link

Issue with importing new Product and Variant in Craft Commerce 5 – "Call to a member function getFieldByHandle() on null" (Fields.php:216) #1525

Closed thibaultjadoul closed 1 month ago

thibaultjadoul commented 1 month ago

Description

I encountered an issue when importing a new Product and Variant in Craft Commerce. The error occurs during the parseField function when it tries to import values into custom fields.

The error message is as follows:

Error: Call to a member function getFieldByHandle() on null in /var/www/html/vendor/craftcms/feed-me/src/services/Fields.php:216
I have added some logs to investigate the issue and found that the error appears when attempting to import a custom field either at the Product or Variant level.

Logs for importing a custom field at the Product level:

2024-10-03 17:44:39 [queue.ERROR] [application] log element: {"elementQueryResult":null,"eagerLoadInfo":null,"id":39778,"tempId":null,"draftId":1718,"revisionId":null,"isProvisionalDraft":false,"uid":"31661a18-47c8-4779-b800-406253e61e2e","siteSettingsId":112544,"fieldLayoutId":25,"structureId":null,"enabled":true,"archived":false,"siteId":1,"title":null,"slug":"__temp_ngxpzwncqaynxsbgkovatirwwuabfviqjglg","uri":"sans-alcool\/__temp_ngxpzwncqaynxsbgkovatirwwuabfviqjglg","dateCreated":{"date":"2024-10-03 17:44:39.000000","timezone_type":3,"timezone":"Europe\/Luxembourg"},"dateUpdated":{"date":"2024-10-03 17:44:39.000000","timezone_type":3,"timezone":"Europe\/Luxembourg"},"dateLastMerged":null,"dateDeleted":null,"deletedWithOwner":null,"root":null,"lft":null,"rgt":null,"level":null,"searchScore":null,"trashed":false,"awaitingFieldValues":false,"propagating":false,"propagatingFrom":null,"propagateAll":false,"newSiteIds":[],"isNewForSite":false,"resaving":false,"duplicateOf":null,"firstSave":false,"mergingCanonicalChanges":false,"updatingFromDerivative":false,"previewing":false,"forceSave":false,"hardDelete":false,"defaultBasePrice":null,"postDate":{"date":"2024-10-03 17:44:00.000000","timezone_type":3,"timezone":"Europe\/Luxembourg"},"expiryDate":null,"typeId":4,"defaultVariantId":null,"defaultSku":null,"defaultHeight":null,"defaultLength":null,"defaultWidth":null,"defaultWeight":null,"taxCategory":null,"name":null,"storeId":1} {"memory":9708464} 
2024-10-03 17:44:39 [queue.ERROR] [application] log getFieldLayout: {"id":25,"type":"craft\\commerce\\elements\\Product","uid":"58f63570-01df-4478-a2ae-41bb39ed1bba","provider":null,"reservedFieldHandles":null} {"memory":9708904} 

Logs for importing a custom field at the Variant level:

2024-10-03 17:44:39 [queue.ERROR] [application] log element: {"elementQueryResult":null,"eagerLoadInfo":null,"id":null,"tempId":null,"draftId":null,"revisionId":null,"isProvisionalDraft":false,"uid":null,"siteSettingsId":null,"fieldLayoutId":null,"structureId":null,"enabled":true,"archived":false,"siteId":2,"title":null,"slug":null,"uri":null,"dateCreated":null,"dateUpdated":null,"dateLastMerged":null,"dateDeleted":null,"deletedWithOwner":null,"root":null,"lft":null,"rgt":null,"level":null,"searchScore":null,"trashed":false,"awaitingFieldValues":false,"propagating":false,"propagatingFrom":null,"propagateAll":false,"newSiteIds":[],"isNewForSite":false,"resaving":false,"duplicateOf":null,"firstSave":false,"mergingCanonicalChanges":false,"updatingFromDerivative":false,"previewing":false,"forceSave":false,"hardDelete":false,"width":null,"height":null,"length":null,"weight":0,"freeShipping":false,"promotable":true,"availableForPurchase":true,"minQty":null,"maxQty":null,"inventoryItemId":null,"inventoryTracked":true,"isDefault":false,"sortOrder":null,"deletedWithProduct":false,"fieldId":null,"saveOwnership":true,"updateSearchIndexForOwner":false} {"memory":9407552} 
2024-10-03 17:44:39 [queue.ERROR] [application] log getFieldLayout: null {"memory":9407832} 
2024-10-03 17:44:39 [queue.ERROR] [feed-me] {"message":"`Call to a member function getFieldByHandle() on null - Fields.php: 219`.","feed":"PRODUCTS - IMPORT - Non-alcoholic","key":"fwftoaqldwansaohxxqe"} {"memory":9413288} 
2024-10-03 17:44:39 [queue.ERROR] [Error] Error: Call to a member function getFieldByHandle() on null in /var/www/html/vendor/craftcms/feed-me/src/services/Fields.php:219

From the logs, it’s clear that $element->getFieldLayout() is returning null, and the $element object has no fieldLayoutId, id, or draftId.

Steps to Reproduce

Try to import a new Product and Variant with custom fields that do not yet exist in Craft Commerce using FeedMe. Monitor the logs and observe the error when the import process reaches the parseField function for custom fields.

Expected Behavior

The product and variant should be imported successfully, including their custom fields.

Actual Behavior

The import process fails when trying to assign values to custom fields, resulting in the error: Call to a member function getFieldByHandle() on null - Fields.php:216.

Versions

Craft CMS: 5.4.6 Craft Commerce: 5.1.3 FeedMe: 6.4.0

thibaultjadoul commented 1 month ago

I'm experiencing two major issues with Feed Me 6.4.0 when using Craft Commerce 5:

  1. Custom Fields on Variants: There is an issue importing data into custom fields for product variants, which wasn't present in version 6.3.0.
  2. Content Propagation Across Sites: The content isn't being properly propagated across all my sites. This worked fine in version 6.3.0.

Is it possible to release a dev version with only the stock import fix from 6.4.0? This would allow me to bypass the regressions in 6.4.0 while still benefiting from the stock import improvements.

Thanks for your help!

i-just commented 1 month ago

Hi, thanks for reporting! I raised a PR for this.

angrybrad commented 1 month ago

Feed Me 6.4.1 is out with the Commerce Variant import fix.