I'm trying to allow users to add the product to the cart using drupal commerce. I have a template file page__product.html.twig
I wanted to pragmatically add the cart button.
$ order_item_storage = Drupal :: service (& # 39; entity_type.manager & # 39;) -> getStorage (& # 39; commerce_order_item & # 39;);
$ order_item = $ order_item_storage-> createFromPurchasableEntity ( Drupal commerce_product Entity ProductVariation :: load ($ product-> getVariationIds ()[0]));
$ form_state_additions = [
'product' => $product,
'view_mode' => 'default',
'settings' => [
'combine' => TRUE,
],
];
$ vars['display_price'] = format_number ($ order_item-> getUnitPrice () -> getNumber (), 2). & # 39; & # 39; $ order_item-> getUnitPrice () -> getCurrencyCode ();
$ vars['form'] = Drupal :: service (& # 39; entity.form_builder & # 39;) -> getForm ($ order_item, & # 39; add_to_cart & # 39 ;, $ form_state_additions);
However, when I click on the button it reloads the page with this error
The website encountered an unexpected error. Please try again later.Exception: The given entity is not assigned to any store. in Drupal commerce_cart Form AddToCartForm- & gt; selectStore () (line 258 of modules / contrib / commerce / modules / cart / src / Form / AddToCartForm.php).
I'm using a custom import script
I tried to edit a product, edit its variations and select a store. However, in my opinion, the field that is stored in the editing page only allows me to select NODE e.g articles. I have a default store & # 39; Online & # 39 ;, the autocomplete, however, I only select nodes instead of stores. Any idea why?
$ product = Drupal commerce_product Entity Product :: create ([
'uid' => 1,
'type' => 'standard_commerce_product',
'title' => $p['name'],
& # 39; field_product_category & # 39; => array (& # 39; target_id & # 39; => $ cat_id),
& # 39; body & # 39; => array (& # 39; value & # 39; => $ p['description'], & # 39; format & # 39; => & # 39; full_html & # 39;),
& # 39; field_summ & # 39; => array (& # 39; value & # 39; => $ p['description'], & # 39; format & # 39; => & # 39; full_html & # 39;),
& # 39; field_additional_details & # 39; => array (& # 39; value & # 39; => $ p['specs'], & # 39; format & # 39; => & # 39; full_html & # 39;),
& # 39; field_product_image & # 39; => $ p_img,
& # 39; field_header_logo & # 39; => array (& # 39; target_id & # 39; => 2201),
& # 39; field_reviews & # 39; => array (& # 39; target_id & # 39; => $ agraph-> id (), & # 39; target_revision_id & # 39; => $ agraph-> getRevisionId ()),
]);
$ price = new Drupal commerce_price Price ((string) $ p['price'], & # 39; GBP & # 39;);
$ variation = Drupal commerce_product Entity ProductVariation :: create ([
'title' => $p['name'],
& # 39; type & # 39; => & # 39; default & # 39 ;, // The default variation type is & # 39; default & # 39 ;.
& # 39; sku & # 39; => $ p['sku'], // The sku variation.
& # 39; status & # 39; => 1, // The status of the product. 0 for the disabled, 1 for the disabled.
& # 39; price & # 39; => $ price,
"field_stores" => array (
"target_id" => 2
) // this line does not work. leave the field blank
]);