You are here

function commerce_ss_tokens in Commerce Stock 7.2

Implements hook_tokens().

File

modules/commerce_ss/commerce_ss.module, line 74
Allow commerce products to have stock levels associated with their SKU.

Code

function commerce_ss_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  if ($type == 'commerce-product' && !empty($data['commerce-product'])) {
    $product = entity_metadata_wrapper('commerce_product', $data['commerce-product']);
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'commerce-stock-int':
          $replacements[$original] = (int) $product->commerce_stock
            ->value();
          break;
        case 'commerce-stock-user':
          $replacements[$original] = $product->commerce_stock
            ->value() - commerce_stock_check_cart_product_level($product->product_id
            ->value());
          break;
      }
    }
  }
  return $replacements;
}