/** * Get the attributes for a product or product variation * * @since 2.1 * @param WC_Product|WC_Product_Variation $product * @return array */ private function get_attributes($product) { $attributes = array(); if ($product->is_type('variation')) { // variation attributes foreach ($product->get_variation_attributes() as $attribute_name => $attribute) { // taxonomy-based attributes are prefixed with `pa_`, otherwise simply `attribute_` $attributes[] = array('name' => wc_attribute_label(str_replace('attribute_', '', $attribute_name)), 'slug' => str_replace('attribute_', '', str_replace('pa_', '', $attribute_name)), 'option' => $attribute); } } else { foreach ($product->get_attributes() as $attribute) { // taxonomy-based attributes are comma-separated, others are pipe (|) separated if ($attribute['is_taxonomy']) { $options = explode(',', $product->get_attribute($attribute['name'])); } else { $options = explode('|', $product->get_attribute($attribute['name'])); } $attributes[] = array('name' => wc_attribute_label($attribute['name']), 'slug' => str_replace('pa_', '', $attribute['name']), 'position' => (int) $attribute['position'], 'visible' => (bool) $attribute['is_visible'], 'variation' => (bool) $attribute['is_variation'], 'options' => array_map('trim', $options)); } } return $attributes; }
/** * Get the attributes for a product or product variation. * * @param WC_Product|WC_Product_Variation $product Product instance. * @return array */ protected function get_attributes($product) { $attributes = array(); if ($product->is_type('variation')) { // Variation attributes. foreach ($product->get_variation_attributes() as $attribute_name => $attribute) { $name = str_replace('attribute_', '', $attribute_name); if (!$attribute) { continue; } // Taxonomy-based attributes are prefixed with `pa_`, otherwise simply `attribute_`. if (0 === strpos($attribute_name, 'attribute_pa_')) { $option_term = get_term_by('slug', $attribute, $name); $attributes[] = array('id' => wc_attribute_taxonomy_id_by_name($name), 'name' => $this->get_attribute_taxonomy_label($name), 'option' => $option_term && !is_wp_error($option_term) ? $option_term->name : $attribute); } else { $attributes[] = array('id' => 0, 'name' => $name, 'option' => $attribute); } } } else { foreach ($product->get_attributes() as $attribute) { if ($attribute['is_taxonomy']) { $attributes[] = array('id' => wc_attribute_taxonomy_id_by_name($attribute['name']), 'name' => $this->get_attribute_taxonomy_label($attribute['name']), 'position' => (int) $attribute['position'], 'visible' => (bool) $attribute['is_visible'], 'variation' => (bool) $attribute['is_variation'], 'options' => $this->get_attribute_options($product->get_id(), $attribute)); } else { $attributes[] = array('id' => 0, 'name' => $attribute['name'], 'position' => (int) $attribute['position'], 'visible' => (bool) $attribute['is_visible'], 'variation' => (bool) $attribute['is_variation'], 'options' => $this->get_attribute_options($product->get_id(), $attribute)); } } } return $attributes; }
/** * Get the attributes for a product or product variation * * @since 2.1 * @param WC_Product|WC_Product_Variation $product * @return array */ private function get_attributes($product) { $attributes = array(); if ($product->is_type('variation')) { // variation attributes foreach ($product->get_variation_attributes() as $attribute_name => $attribute) { // taxonomy-based attributes are prefixed with `pa_`, otherwise simply `attribute_` $attributes[] = array('name' => ucwords(str_replace('attribute_', '', str_replace('pa_', '', $attribute_name))), 'option' => $attribute); } } else { foreach ($product->get_attributes() as $attribute) { $attributes[] = array('name' => ucwords(str_replace('pa_', '', $attribute['name'])), 'position' => $attribute['position'], 'visible' => (bool) $attribute['is_visible'], 'variation' => (bool) $attribute['is_variation'], 'options' => $this->get_attribute_options($product->get_id(), $attribute)); } } return $attributes; }
/** * Get the attributes for a product or product variation. * * @param WC_Product|WC_Product_Variation $product * @return array */ protected function get_attributes($product) { $attributes = array(); if ($product->is_type('variation')) { // Variation attributes. foreach ($product->get_variation_attributes() as $attribute_name => $attribute) { $name = str_replace('attribute_', '', $attribute_name); // Taxonomy-based attributes are prefixed with `pa_`, otherwise simply `attribute_`. if (0 === strpos($attribute_name, 'attribute_pa_')) { $attributes[] = array('id' => wc_attribute_taxonomy_id_by_name($name), 'name' => $this->get_attribute_taxonomy_label($name), 'option' => $attribute); } else { $attributes[] = array('id' => 0, 'name' => str_replace('pa_', '', $name), 'option' => $attribute); } } } else { foreach ($product->get_attributes() as $attribute) { // Taxonomy-based attributes are comma-separated, others are pipe (|) separated. if ($attribute['is_taxonomy']) { $attributes[] = array('id' => $attribute['is_taxonomy'] ? wc_attribute_taxonomy_id_by_name($attribute['name']) : 0, 'name' => $this->get_attribute_taxonomy_label($attribute['name']), 'position' => (int) $attribute['position'], 'visible' => (bool) $attribute['is_visible'], 'variation' => (bool) $attribute['is_variation'], 'options' => array_map('trim', explode(',', $product->get_attribute($attribute['name'])))); } else { $attributes[] = array('id' => 0, 'name' => str_replace('pa_', '', $attribute['name']), 'position' => (int) $attribute['position'], 'visible' => (bool) $attribute['is_visible'], 'variation' => (bool) $attribute['is_variation'], 'options' => array_map('trim', explode('|', $product->get_attribute($attribute['name'])))); } } } return $attributes; }