本文整理汇总了PHP中WC_Product_Variation::get_attributes方法的典型用法代码示例。如果您正苦于以下问题:PHP WC_Product_Variation::get_attributes方法的具体用法?PHP WC_Product_Variation::get_attributes怎么用?PHP WC_Product_Variation::get_attributes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WC_Product_Variation
的用法示例。
在下文中一共展示了WC_Product_Variation::get_attributes方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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
*
* @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;
}
示例3: get_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;
}
示例4: product_page
/**
* Show a single product page.
*
* @param array $atts
* @return string
*/
public static function product_page($atts)
{
if (empty($atts)) {
return '';
}
if (!isset($atts['id']) && !isset($atts['sku'])) {
return '';
}
$args = array('posts_per_page' => 1, 'post_type' => 'product', 'post_status' => 'publish', 'ignore_sticky_posts' => 1, 'no_found_rows' => 1);
if (isset($atts['sku'])) {
$args['meta_query'][] = array('key' => '_sku', 'value' => sanitize_text_field($atts['sku']), 'compare' => '=');
$args['post_type'] = array('product', 'product_variation');
}
if (isset($atts['id'])) {
$args['p'] = absint($atts['id']);
}
$single_product = new WP_Query($args);
$preselected_id = '0';
// check if sku is a variation
if (isset($atts['sku']) && $single_product->have_posts() && 'product_variation' === $single_product->post->post_type) {
$variation = new WC_Product_Variation($single_product->post->ID);
$attributes = $variation->get_attributes();
// set preselected id to be used by JS to provide context
$preselected_id = $single_product->post->ID;
// get the parent product object
$args = array('posts_per_page' => 1, 'post_type' => 'product', 'post_status' => 'publish', 'ignore_sticky_posts' => 1, 'no_found_rows' => 1, 'p' => $single_product->post->post_parent);
$single_product = new WP_Query($args);
?>
<script type="text/javascript">
jQuery( document ).ready( function( $ ) {
var $variations_form = $( '[data-product-page-preselected-id="<?php
echo esc_attr($preselected_id);
?>
"]' ).find( 'form.variations_form' );
<?php
foreach ($attributes as $attr => $value) {
?>
$variations_form.find( 'select[name="<?php
echo esc_attr($attr);
?>
"]' ).val( '<?php
echo $value;
?>
' );
<?php
}
?>
});
</script>
<?php
}
ob_start();
while ($single_product->have_posts()) {
$single_product->the_post();
wp_enqueue_script('wc-single-product');
?>
<div class="single-product" data-product-page-preselected-id="<?php
echo esc_attr($preselected_id);
?>
">
<?php
wc_get_template_part('content', 'single-product');
?>
</div>
<?php
}
// end of the loop.
wp_reset_postdata();
return '<div class="woocommerce">' . ob_get_clean() . '</div>';
}
示例5: 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;
}