/** * Finds and returns list of attributes associated with selected product by it's ID. * * @param $productId int Product ID. * * @return array List of attributes attached to selected product. */ public function getAttributes($productId) { $wpdb = $this->wp->getWPDB(); $query = $wpdb->prepare("\n\t\tSELECT a.id, a.is_local, a.slug, a.label, a.type, pa.value,\n\t\t\tao.id AS option_id, ao.value AS option_value, ao.label as option_label,\n\t\t\tpam.id AS meta_id, pam.meta_key, pam.meta_value\n\t\tFROM {$wpdb->prefix}jigoshop_attribute a\n\t\t\tLEFT JOIN {$wpdb->prefix}jigoshop_attribute_option ao ON a.id = ao.attribute_id\n\t\t\tLEFT JOIN {$wpdb->prefix}jigoshop_product_attribute pa ON pa.attribute_id = a.id\n\t\t\tLEFT JOIN {$wpdb->prefix}jigoshop_product_attribute_meta pam ON pa.attribute_id = pam.attribute_id AND pa.product_id = pam.product_id\n\t\t\tWHERE pa.product_id = %d\n\t\t", array($productId)); $results = $wpdb->get_results($query, ARRAY_A); $attributes = array(); for ($i = 0, $endI = count($results); $i < $endI;) { $attribute = $this->createAttribute($results[$i]['type'], Attribute::PRODUCT_ATTRIBUTE_EXISTS); $attribute->setId((int) $results[$i]['id']); $attribute->setSlug($results[$i]['slug']); $attribute->setLabel($results[$i]['label']); $attribute->setLocal((bool) $results[$i]['is_local']); $attribute->setValue($results[$i]['value']); $fields = array(); while ($i < $endI && $results[$i]['id'] == $attribute->getId()) { $option = new Attribute\Option(); if ($results[$i]['option_id'] !== null) { $option->setId($results[$i]['option_id']); $option->setLabel($results[$i]['option_label']); $option->setValue($results[$i]['option_value']); $attribute->addOption($option); } while ($i < $endI && $results[$i]['id'] == $attribute->getId() && $results[$i]['option_id'] == $option->getId()) { if ($results[$i]['meta_id'] !== null && !isset($fields[$results[$i]['meta_key']])) { $field = new Attribute\Field(); $field->setId($results[$i]['meta_id']); $field->setKey($results[$i]['meta_key']); $field->setValue($results[$i]['meta_value']); $field->setAttribute($attribute); $fields[$results[$i]['meta_key']] = $field; } $i++; } } $attribute->restoreFields($fields); $attributes[$attribute->getId()] = $attribute; } return $attributes; }
/** * @return bool Whether attribute is used for variations. */ public function isVariable() { return (bool) $this->variable->getValue(); }