Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I create a product entity instance like below. However, it results in : "Recoverable fatal error: Argument 1 passed to Drupal\commerce_order\OrderItemStorage::createFromPurchasableEntity()
must implement interface Drupal\commerce\PurchasableEntityInterface, null given". I'm missing how to implement PurchasableEntityInterface.
How to complete this correctly? Thanks.
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce\PurchasableEntityInterface;
$my_product = Product::create([
'type' => 'food',
'langcode' => 'en',
'created' => REQUEST_TIME,
'changed' => REQUEST_TIME,
'uid' => 1,
'title' => 'My product',
'body' => [
'summary' => '',
'value' => '<p>Lorem Ipsum</p>',
'format' => 'full_html',
],
]);
$my_product->save();
Comments
Comment #2
bojanz CreditAttribution: bojanz at Centarro commentedThe support request is a duplicate of #2811529: How to create commerce products from code, someone will reply there.
Which other method were you calling when you got the error? Was it just this save?
You generally need a variation along with the product, but the system should not crash.
Reclassifying as a bug report.
Comment #3
jpdaut CreditAttribution: jpdaut commentedbojanz the code above was all I was using. However I now think there's no bug : my create() call was missing the variations argument, which is required, as I figured out after reading mbreden's very helpful post that came in today.
If I do a complete example like he suggests, it works fine. So to me I think there's no bug here, this issue can be closed. Thank you.
Comment #4
mglamanPer #3 this is a non-issue afterall.