You are here

function uc_dropdown_attributes_product_create_dependency in Dropdown Attributes 7

Same name and namespace in other branches
  1. 8 uc_dropdown_attributes.module \uc_dropdown_attributes_product_create_dependency()
  2. 6 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.

2 calls to uc_dropdown_attributes_product_create_dependency()
UCDropdownAttributesTestCase::testProductAttributeDependency in tests/uc_dropdown_attributes.test
Test for dropdown attributes in products.
uc_dropdown_attributes_product_submit in ./dependent_dropdown.inc
Form submission handler for uc_dropdown_attributes_product().

File

./uc_dropdown_attributes.module, line 621
Show/hide attributes based on the values of other attributes.

Code

function uc_dropdown_attributes_product_create_dependency($nid, $aid, $parent_aid, $options, $required) {
  $attribute = uc_attribute_load($aid);
  $dep = db_insert('uc_dropdown_attributes')
    ->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.
  $sql = 'SELECT nid, aid, required FROM {uc_product_attributes}
    WHERE nid=:nid && aid=:aid';
  $result = db_query($sql, array(
    ':nid' => $nid,
    ':aid' => $aid,
  ));
  foreach ($result as $item) {
    if ($item->required == 1) {
      $dep = db_update('uc_product_attributes')
        ->fields(array(
        'required' => 0,
      ))
        ->condition('nid', $item->nid)
        ->condition('aid', $item->aid)
        ->execute();
    }
  }
}