当前位置: 首页>>代码示例>>PHP>>正文


PHP WC_Product_Variation::get_attributes方法代码示例

本文整理汇总了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;
 }
开发者ID:haltaction,项目名称:woocommerce,代码行数:29,代码来源:class-wc-api-products.php

示例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;
 }
开发者ID:woocommerce,项目名称:woocommerce,代码行数:23,代码来源:class-wc-api-products.php

示例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;
 }
开发者ID:shivapoudel,项目名称:woocommerce,代码行数:35,代码来源:class-wc-rest-products-controller.php

示例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>';
    }
开发者ID:shivapoudel,项目名称:woocommerce,代码行数:81,代码来源:class-wc-shortcodes.php

示例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;
 }
开发者ID:seriusokhatsky,项目名称:woocommerce,代码行数:32,代码来源:class-wc-rest-products-controller.php


注:本文中的WC_Product_Variation::get_attributes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。