You are here

public function ProductBundleStockProxy::createTransaction in Commerce Product Bundle 8

Create a stock transaction.

Parameters

\Drupal\commerce\PurchasableEntityInterface $entity: The purchasable entity.

int $location_id: The location ID.

string $zone: The zone.

float $quantity: Tbe quantity.

float $unit_cost: The unit cost.

string $currency_code: The currency code.

int $transaction_type_id: The transaction type ID.

array $metadata: Holds all the optional values those are:

  • related_tid: related transaction.
  • related_oid: related order.
  • related_uid: related user.
  • data: Serialized data array.

Return value

int Return the ID of the transaction.

Overrides StockUpdateInterface::createTransaction

File

modules/commerce_product_bundle_stock/src/ProductBundleStockProxy.php, line 38

Class

ProductBundleStockProxy
Provides a stock service for product bundles.

Namespace

Drupal\commerce_product_bundle_stock

Code

public function createTransaction(PurchasableEntityInterface $bundle, $location_id, $zone, $quantity, $unit_cost, $currency_code, $transaction_type_id, array $metadata) {

  /** @var \Drupal\commerce_product_bundle\Entity\BundleItemInterface $item */
  foreach ($bundle
    ->getBundleItems() as $item) {

    /** @var \Drupal\commerce_product\Entity\ProductVariationInterface $entity */
    $entity = $item
      ->getCurrentVariation();
    $service = $this->stockServiceManager
      ->getService($entity);
    $updater = $service
      ->getStockUpdater();
    $item_quantity = $quantity * $item
      ->getQuantity();
    $updater
      ->createTransaction($entity, $location_id, $zone, $item_quantity, $unit_cost, $currency_code, $transaction_type_id, $metadata);
  }
}