本文整理汇总了PHP中WC_Product_Variation::get_attribute方法的典型用法代码示例。如果您正苦于以下问题:PHP WC_Product_Variation::get_attribute方法的具体用法?PHP WC_Product_Variation::get_attribute怎么用?PHP WC_Product_Variation::get_attribute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WC_Product_Variation
的用法示例。
在下文中一共展示了WC_Product_Variation::get_attribute方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: get_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' => 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;
}
示例2: get_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;
}