You are here

protected function TestController::createProductKit in Dropdown Attributes 8

Create a product kit.

Parameters

array $products: Array of product objects.

array $data: Optional data for the product kit.

Return value

object Product kit node object.

1 call to TestController::createProductKit()
TestController::productKit in uc_dropdown_test/src/Controller/TestController.php

File

uc_dropdown_test/src/Controller/TestController.php, line 696

Class

TestController
Additional test of Dropdown Attributes UI.

Namespace

Drupal\uc_dropdown_test\Controller

Code

protected function createProductKit(array $products, array $data = array()) {
  $weight_units = array(
    'lb',
    'kg',
    'oz',
    'g',
  );
  $length_units = array(
    'in',
    'ft',
    'cm',
    'mm',
  );
  $product_kit = $data + array(
    'type' => 'product_kit',
    'default_qty' => 1,
    'model' => $this
      ->randGen(8),
    'cost' => 0,
    'price' => 0,
    'weight' => array(
      0 => array(
        'value' => mt_rand(1, 9999),
        'units' => array_rand(array_flip($weight_units)),
      ),
    ),
    'dimensions' => array(
      0 => array(
        'length' => mt_rand(1, 9999),
        'width' => mt_rand(1, 9999),
        'height' => mt_rand(1, 9999),
        'units' => array_rand(array_flip($length_units)),
      ),
    ),
    'shippable' => 0,
    'mutable' => 0,
    'weight_units' => 'lb',
  );
  $product_kit += array(
    'body' => array(
      array(
        'value' => $this
          ->randGen(32),
        'format' => filter_default_format(),
      ),
    ),
    'title' => $this
      ->randGen(8),
    'uid' => \Drupal::currentUser()
      ->id(),
  );
  $node = Node::create($product_kit);
  $node
    ->save();
  $model = $node->model->value;
  $cost = $node->cost->value;
  $price = $node->price->value;
  $weight = $node->weight->value;
  $shippable = $node->shippable->value;
  foreach ($products as $product) {
    \Drupal::database()
      ->insert('uc_product_kits')
      ->fields(array(
      'vid' => $node
        ->getRevisionId(),
      'nid' => $node
        ->id(),
      'product_id' => $product
        ->id(),
      'mutable' => $node->mutable,
      'qty' => 1,
      'synchronized' => 1,
    ))
      ->execute();
    $model .= $product->model->value . ' / ';
    $cost += $product->cost->value;
    $price += $product->price->value;
    $weight += $product->weight->value * uc_weight_conversion($product->weight->units, $obj->weight_units);
    if ($product->shippable->value) {
      $shippable = TRUE;
    }
  }
  $model = rtrim($model, ' / ');
  $node->price
    ->setValue($price);
  \Drupal::database()
    ->merge('uc_products')
    ->key(array(
    'vid' => $node->vid->value,
  ))
    ->fields(array(
    'nid' => $node
      ->id(),
    'model' => $model,
    'cost' => $cost,
    'price' => $price,
    'weight' => $weight,
    'weight_units' => $node->weight_units,
    'default_qty' => $node->default_qty->value,
    'shippable' => $shippable,
  ))
    ->execute();
  $node = Node::load($node
    ->id());
  return $node;
}