You are here

protected function TestController::createProduct in Dropdown Attributes 8

Create a product.

Parameters

array $data: Optional data for the product.

Return value

object Product node object.

3 calls to TestController::createProduct()
TestController::product in uc_dropdown_test/src/Controller/TestController.php
TestController::productClass in uc_dropdown_test/src/Controller/TestController.php
TestController::productKit in uc_dropdown_test/src/Controller/TestController.php

File

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

Class

TestController
Additional test of Dropdown Attributes UI.

Namespace

Drupal\uc_dropdown_test\Controller

Code

protected function createProduct(array $data = array()) {
  $weight_units = array(
    'lb',
    'kg',
    'oz',
    'g',
  );
  $length_units = array(
    'in',
    'ft',
    'cm',
    'mm',
  );
  $product = $data + array(
    'type' => 'product',
    'model' => $this
      ->randGen(8),
    'cost' => mt_rand(1, 9999),
    'price' => mt_rand(1, 9999),
    '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)),
      ),
    ),
    'pkg_qty' => mt_rand(1, 99),
    'default_qty' => 1,
    'shippable' => 1,
  );
  $product['model'] = array(
    array(
      'value' => $product['model'],
    ),
  );
  $product['price'] = array(
    array(
      'value' => $product['price'],
    ),
  );
  $product += array(
    'body' => array(
      array(
        'value' => $this
          ->randGen(32),
        'format' => filter_default_format(),
      ),
    ),
    'title' => $this
      ->randGen(8),
    'type' => 'page',
    'uid' => \Drupal::currentUser()
      ->id(),
  );
  $node = entity_create('node', $product);
  $node
    ->save();
  return $node;
}