You are here

public function BundleItemInterface::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.

1 method overrides BundleItemInterface::addVariation()
ProductBundleItem::addVariation in src/Entity/ProductBundleItem.php
Adds a variation.

File

src/Entity/BundleItemInterface.php, line 266

Class

BundleItemInterface
Provides an interface for defining product bundle item entities.

Namespace

Drupal\commerce_product_bundle\Entity

Code

public function addVariation(ProductVariationInterface $variation);