You are here

public function StockServiceManager::sellStock in Commerce Stock 8

Sell stock.

Parameters

\Drupal\commerce\PurchasableEntityInterface $entity: The purchasable entity (most likely a product variation entity).

int $location_id: The location ID.

string $zone: The zone.

float $quantity: The quantity.

float $unit_cost: The unit cost.

string $currency_code: The currency code.

int $order_id: The order ID.

int $user_id: The user ID.

string $message: The message.

Overrides StockTransactionsInterface::sellStock

File

src/StockServiceManager.php, line 154

Class

StockServiceManager
The stock service manager.

Namespace

Drupal\commerce_stock

Code

public function sellStock(PurchasableEntityInterface $entity, $location_id, $zone, $quantity, $unit_cost, $currency_code, $order_id, $user_id, $message = NULL) {
  $transaction_type_id = StockTransactionsInterface::STOCK_SALE;
  $metadata = [
    'related_oid' => $order_id,
    'related_uid' => $user_id,
  ];
  if (!is_null($message)) {
    $metadata['data']['message'] = $message;
  }

  // Make sure quantity is positive.
  $quantity = -1 * abs($quantity);
  $stock_updater = $this
    ->getService($entity)
    ->getStockUpdater();
  $stock_updater
    ->createTransaction($entity, $location_id, $zone, $quantity, $unit_cost, $currency_code, $transaction_type_id, $metadata);
}