获取可变产品的每个 WooCommerce 变体的特定产品属性名称和值 [英] Get specific product attribute name and value for each WooCommerce variation of a variable product

查看:74
本文介绍了获取可变产品的每个 WooCommerce 变体的特定产品属性名称和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WooCommerce 中,我想使用我自己的定制设计展示可变产品作为产品变体的轮播.我得到了可以在数组中显示产品正常价格的代码,但我坚持在其中获取属性名称和值.

基于"

解决方案

在下面的代码中,您将能够获得您特定的产品属性名称和术语名称值.您还将获得正确的价格显示(即使产品正在打折).

您必须在代码中定义所需的产品属性 slug(用您的替换颜色").

我还在最后添加了 html 缺少的结束标记.

add_action('woocommerce_before_single_product_summary', 'custom_table_after_single_product');函数 custom_table_after_single_product(){全球$产品;//仅适用于可变产品if(!$product->is_type('variable')) 返回;//在下面设置您的产品属性 slug$product_attribute_slug = '颜色';//<==== <==== <==== <==== <==== <==== <====$targeted_taxonomy = 'pa_' .$product_attribute_slug;$available_variations = $product->get_available_variations();如果(计数($available_variations)> 0){$output = ' 
<div class="容器"><div class="row"><div class="owl-carouselpacked_sections">';//循环变化foreach( $available_variations 作为 $variation_data ){//1. 变动价格(处理特价商品清洁展示)if( $variation_data['display_price'] === $variation_data['display_regular_price'] ) {$price_to_display = wc_price( $variation_data['display_price'] );} 别的 {$variation = wc_get_product($variation_data['variation_id']);//获取 WC_Product_Variation 对象$price_to_display = wc_get_price_to_display( $variation, array('price' => $variation->get_sale_price() ) );}//2.变体产品属性//遍历当前变体的产品属性foreach ( $variation_data['attributes'] as $variation_attribute => $term_slug ) {//获取分类slug$taxonomy = str_replace('attribute_', '', $variation_attribute);//获取此变体的正确产品属性术语值if( $taxonomy === $targeted_taxonomy ) {//获取属性值术语名称$term_name = get_term_by('slug', $term_slug, $taxonomy )->name;}}$output .='

<div class="inside"><div class="number">'.$term_name .'</div><div class="number_text">'.wc_attribute_label( $targeted_taxonomy ) .'</div><div class="price"><sup>$</sup><跨度>'.strip_tags($price_to_display) .'</span></div><div class="services"><ul><li><i class="fa fa-video-camera"></i>' .__("包括视频观看次数") .'</li><li><i class="fa fa-star"></i>' .__("高质量赞") .'</li><li><i class="fa fa-lock"></i>' .__("不需要密码") .'</li><li><i class="fa fa-clock-o"></i>' .__("订单在 60 秒后开始") .'</li><li><i class="fa fa-clock-o"></i>' .__("快速交付 10 分钟") .'</li><li><i class="fa fa-commenting"></i>' .__("24/7 支持") .'</li>

<div class="buy"><a href="#">'.__(立即购买") .'</a></div>

';}//输出回声 $output .'</div>

';}}

代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效.

In WooCommerce, I would like to display variable products with my own custom design as a carousel of product variations. I got the code by which I can display the regular price of the product in array but I stuck in getting attribute name and value in that.

Based on "WooCommerce variable products: Display some variations values in an HTML table" answer code, here is my actual code:

 add_action( 'woocommerce_before_single_product_summary', 'custom_table_after_single_product' );

function custom_table_after_single_product(){
    global $product;

   // Only for variable products
   if( ! $product->is_type('variable')) return; 

    $available_variations = $product->get_available_variations();
            // Get product attributes
    $attributes = $product->get_attributes();

    if( count($available_variations) > 0 ){

        $output = ' <section class="boxes">
        <div class="container">
            <div class="row">
            <div class="owl-carousel packed_sections">';

            // Get product attributes


        foreach( $available_variations as $variation ){
            // Get an instance of the WC_Product_Variation object
            $product_variation = wc_get_product($variation['variation_id']);


            $sale_price = $product_variation->get_sale_price();
            if( empty( $sale_price ) ) $sale_price = __( '<em>(empty)</em>', 'woocommerce' );


            $output .= '

                <div class="col-sm-12 package_box">
                    <div class="inside">
                        <div class="number">1000</div>

                        <div class="number_text">Followers</div>
                        <div class="price"><sup>$</sup> <span>'.$product_variation->get_regular_price().'</span></div>
                        <div class="services">
                            <ul>
                                <li><i class="fa fa-video-camera"></i> Include Video Views</li>
                                <li><i class="fa fa-star"></i> High quality likes</li>
                                <li><i class="fa fa-lock"></i> no password required</li>
                                <li><i class="fa fa-clock-o"></i> order start in 60 seconds</li>
                                <li><i class="fa fa-clock-o"></i> fast deliver 10 minutes</li>
                                <li><i class="fa fa-commenting"></i> 24/7 support</li>
                            </ul>
                        </div>
                        <div class="buy"><a href="#">buy now</a></div>
                    </div>
                </div>';


        }
        echo $output;
    }

}

This code gives the correct output, but I want attribute value and name with that also here is the output screenshot:

解决方案

In the following code, you will be able to get your specific product attribute name and term name value. You will also get the correct price display (even when product is on sale).

You will have to define in the code, your desired product attribute slug (replacing "color" by yours).

I have also added at the end the html missing closing tags.

add_action( 'woocommerce_before_single_product_summary', 'custom_table_after_single_product' );
function custom_table_after_single_product(){
    global $product;

    // Only for variable products
    if( ! $product->is_type('variable')) return;

    // HERE below SET your product attribute slug
    $product_attribute_slug = 'color'; // <====  <====  <====  <====  <====  <====  <====

    $targeted_taxonomy      = 'pa_' . $product_attribute_slug;

    $available_variations   = $product->get_available_variations();

    if( count($available_variations) > 0 ){

        $output = ' <section class="boxes">
        <div class="container">
            <div class="row">
            <div class="owl-carousel packed_sections">';

        // Loop through variations
        foreach( $available_variations as $variation_data ){

            // 1. The variation price (handle on sale products clean display)

            if( $variation_data['display_price'] === $variation_data['display_regular_price'] ) {
                $price_to_display = wc_price( $variation_data['display_price'] );
            } else {
                $variation        = wc_get_product($variation_data['variation_id']); // Get the WC_Product_Variation Object
                $price_to_display = wc_get_price_to_display( $variation, array('price' => $variation->get_sale_price() ) );
            }

            // 2. The variation product attribute

            // Loop though product attributes for the current variation
            foreach ( $variation_data['attributes'] as $variation_attribute => $term_slug ) {
                // Get the taxonomy slug
                $taxonomy = str_replace( 'attribute_', '', $variation_attribute );

                // Get the correct product attribute term value for this variation
                if( $taxonomy === $targeted_taxonomy ) {
                    // Get the attribute value term name
                    $term_name = get_term_by( 'slug', $term_slug, $taxonomy )->name;
                }
            }

            $output .= '<div class="col-sm-12 package_box">
                <div class="inside">
                    <div class="number">' . $term_name . '</div>
                    <div class="number_text">' . wc_attribute_label( $targeted_taxonomy ) . '</div>
                    <div class="price"><sup>$</sup> <span>' . strip_tags($price_to_display) . '</span></div>
                    <div class="services">
                        <ul>
                            <li><i class="fa fa-video-camera"></i> ' . __("Include Video Views") . '</li>
                            <li><i class="fa fa-star"></i> ' . __("High quality likes") . '</li>
                            <li><i class="fa fa-lock"></i> ' . __("no password required") . '</li>
                            <li><i class="fa fa-clock-o"></i> ' . __("order start in 60 seconds") . '</li>
                            <li><i class="fa fa-clock-o"></i> ' . __("fast deliver 10 minutes") . '</li>
                            <li><i class="fa fa-commenting"></i> ' . __("24/7 support") . '</li>
                        </ul>
                    </div>
                    <div class="buy"><a href="#">' . __("buy now") . '</a></div>
                </div>
            </div>';
        }

        // Output
        echo $output . '</div>
                </div>
            </div>
        </section>';
    }
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

这篇关于获取可变产品的每个 WooCommerce 变体的特定产品属性名称和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆