This use case involves copying an address from CiviCRM into a field on a commerce product.
- Create a product
Based on an event trigger
protected function createProduct(EntityInterface $group) { $user = User::load($this->currentUser->id()); $user_hash = $user->uuid(); $sku = $this->getMachineName($group->get('label') ->getString()) . '_' . $user_hash; $variation = ProductVariation::create([ 'type' => 'basic', 'sku' => $sku, 'price' => new Price('10.00', 'GBP'), ]); $variation->save(); $product = Product::create([ 'type' => 'basic', 'title' => t($group->get('label')->getString()), 'variations' => [$variation], ]); $this->setAddress($product); $product->save(); }
However, setAddress() is the challenge
protected function setProductAddress($civiCrmAddress, $contact, EntityInterface $product) { $addressList = $product->get('field_address');
Which provides an AddressFieldItemList. How do I create an AddressItem field?
Not like this: $address = AddressItem::createInstance($addressDefinition);
as I can’t find $addressDefinition (ComplexDataDefinitionInterface)
Just create an array $address = [‘country_code’ => ‘US]; ?