You are here

function uc_dropdown_attributes_product_create_dependency in Dropdown Attributes 8

Same name and namespace in other branches
  1. 6 uc_dropdown_attributes.module \uc_dropdown_attributes_product_create_dependency()
  2. 7 uc_dropdown_attributes.module \uc_dropdown_attributes_product_create_dependency()

Create an attribute dependency.

A public function that creates and stores an attribute dependency for a product.

Parameters

int $nid: Node ID.

int $aid: Attribute ID of the dependent (child) attribute.

int $parent_aid: Attribute ID of the parent attribute.

array $options: Array of the Option IDs that trigger the dependent attribute.

bool $required: TRUE if the dependent (child) attribute is required when it appears and FALSE if it is not required.

4 calls to uc_dropdown_attributes_product_create_dependency()
TestController::product in uc_dropdown_test/src/Controller/TestController.php
TestController::productKit in uc_dropdown_test/src/Controller/TestController.php
UCDropdownAttributesKitTest::testKitAttributeDependency in src/Tests/UCDropdownAttributesKitTest.php
Test for dropdown attributes in product kits.
UCDropdownAttributesProductTest::testProductAttributeDependency in src/Tests/UCDropdownAttributesProductTest.php
Test for dropdown attributes in products.

File

./uc_dropdown_attributes.module, line 591
A module for uc_dropdown_attributes.

Code

function uc_dropdown_attributes_product_create_dependency($nid, $aid, $parent_aid, $options, $required) {
  $attribute = uc_attribute_load($aid);
  $dep = \Drupal::database()
    ->insert('uc_dropdown_products')
    ->fields(array(
    'nid' => $nid,
    'aid' => $aid,
    'parent_aid' => $parent_aid,
    'parent_values' => serialize($options),
    'required' => $required,
  ))
    ->execute();

  // Need to check to make sure attribute is not required all the time.
  $result = \Drupal::database()
    ->select('uc_product_attributes', 'attributes')
    ->fields('attributes', array(
    'nid',
    'aid',
    'required',
  ))
    ->condition('attributes.nid', $nid)
    ->condition('attributes.aid', $aid)
    ->execute();
  foreach ($result as $item) {
    if ($item->required == 1) {
      $dep = \Drupal::database()
        ->update('uc_product_attributes')
        ->fields(array(
        'required' => 0,
      ))
        ->condition('nid', $item->nid)
        ->condition('aid', $item->aid)
        ->execute();
    }
  }
}