public function ProductBundleItem::addVariation in Commerce Product Bundle 8
Adds a variation.
If the bundle item does not yet hold a product reference, nothing happens. So if you unsure you should propably check for existence of the product reference.
If the bundle item has a product reference, but doesn't restrict the variations, nothing will happen. You can check for this with hasVariations().
if($bundleItem->hasProduct() && $bundleItem->hasVariations()){
$bundleItem->addVariation($variation)
} else {
$bundleItem->setVariations([$variation]);
}
Parameters
\Drupal\commerce_product\Entity\ProductVariationInterface $variation: The variation.
Return value
$this
Throws
\InvalidArgumentException In case the variation don't belong to the referenced product.
Overrides BundleItemInterface::addVariation
File
- src/
Entity/ ProductBundleItem.php, line 317
Class
- ProductBundleItem
- Defines the product bundle item entity.
Namespace
Drupal\commerce_product_bundle\EntityCode
public function addVariation(ProductVariationInterface $variation) {
if ($this
->hasProduct() && $this
->hasVariations() && !$this
->hasVariation($variation)) {
$this
->assertSameProduct([
$variation,
]);
$this
->get('variations')
->appendItem($variation);
}
return $this;
}