/**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state, NodeInterface $node = NULL)
 {
     $this->attributeTable = 'uc_product_attributes';
     $this->optionTable = 'uc_product_options';
     $this->idField = 'nid';
     $this->idValue = $node->id();
     $attributes = uc_product_get_attributes($node->id());
     return parent::buildForm($form, $form_state, $attributes);
 }
function uc_attribute_add_to_cart($nid, $qty, $data)
{
    $atts = uc_product_get_attributes($nid);
    if (!is_array($atts) || count($atts) == 0) {
        return;
    }
    if (!is_array($data) || !is_array($data['attributes'])) {
        $data['attributes'] = array();
    }
    $attsSubmitted = $data['attributes'];
    foreach ($atts as $key => $att) {
        if (!$att->required) {
            continue;
        }
        if (!isset($data['attributes'][$att->aid]) || empty($data['attributes'][$att->aid])) {
            return array(array('success' => FALSE, 'message' => t('You must specify an option for !attribute', array('!attribute' => $att->name))));
        }
    }
}
 /**
  * Creates Cart Links pointing to the given product(s).
  *
  * Links containing many combinations of attributes and options wil be
  * returned. Return value is an associative array containing two keys:
  *   -links: An array of the actual links we're building.
  *   -data: An array of metadata about the Cart Links so we won't have to try
  *   to re-construct this information by parsing the link at a later time.
  *
  * The 'links' and 'data' sub-arrays are both indexed by the keys used in
  * the $products array that is passed in as an argument, so these keys may
  * be used to lookup the link and metadata for a specific product.
  *
  * @param $products
  *   An array of products.
  *
  * @return
  *   Array containing Cart Links and link metadata.
  */
 protected function createValidCartLinks($products = array())
 {
     foreach ($products as $key => $product) {
         $nid = $product->id();
         $title = $product->label();
         $qty = mt_rand(1, 19);
         // $link_data will hold meta information about the Cart Links
         // so we won't have to try to re-construct this information by
         // parsing the link at a later time.
         $link_data[$key] = array('nid' => $nid, 'title' => $title, 'qty' => $qty, 'attributes' => array());
         // $cart_links will hold the actual links we're building.
         // $cart_links and $link_data share the same keys.
         $cart_links[$key] = '/cart/add/p' . $nid . '_q' . $qty;
         // Loop over attributes, append all attribute/option combos to links
         $attributes = uc_product_get_attributes($nid);
         foreach ($attributes as $attribute) {
             // If this is textfield, radio, or select option, then
             // only 1 option allowed.  If checkbox, multiple are allowed.
             switch ($attribute->display) {
                 case 0:
                     // textfield
                     $value = $this->randomMachineName(12);
                     // Textfield
                     $link_data[$key]['attributes'][$attribute->label][] = $value;
                     $cart_links[$key] .= '_a' . $attribute->aid . 'o' . $value;
                     break;
                 case 1:
                     // select
                 // select
                 case 2:
                     // radios
                     $option = $attribute->options[array_rand($attribute->options)];
                     $link_data[$key]['attributes'][$attribute->label][] = $option->name;
                     $cart_links[$key] .= '_a' . $attribute->aid . 'o' . $option->oid;
                     break;
                 case 3:
                     // checkboxes
                     foreach ($attribute->options as $option) {
                         $link_data[$key]['attributes'][$attribute->label][] = $option->name;
                         $cart_links[$key] .= '_a' . $attribute->aid . 'o' . $option->oid;
                     }
                     break;
             }
         }
     }
     return array('links' => $cart_links, 'data' => $link_data);
 }