รับและแสดงค่าสำหรับคุณลักษณะของผลิตภัณฑ์ใน WooCommerce

ใน WooCommerce ฉันมีรหัสนี้เพื่อแสดงคุณสมบัติผลิตภัณฑ์ทากในหน้าเก็บถาวรเช่นหน้า ร้านค้า :

if (!function_exists('shop_attributes_in_loop')) {
    function shop_attributes_in_loop(){
        global $product;
        $attributes = $product->get_attributes();
        if(!empty($attributes)){
            $attribute_single = array_keys($attributes);
            $myArray = array();
            echo '<div class="product_attributes">';
            foreach ($attribute_single as $attribute => $value) {
                $myArray[] = ucfirst($value);
            }
            echo implode(', ', $myArray).'</div>';
        }
    }
}
add_action('woocommerce_after_shop_loop_item', 'shop_attributes_in_loop');

พวกเขาแสดงเฉพาะชื่อฟิลด์แอตทริบิวต์เป็น pa_size, pa_color

ฉันจะรับและแสดงค่าสำหรับคุณลักษณะของผลิตภัณฑ์นั้นได้อย่างไร (เช่น 2kg, 3kg หรือ blue, green) ?

ขอบคุณ.


person Fninja    schedule 30.12.2016    source แหล่งที่มา
comment
เป็นเรื่องยากมากที่จะรู้ว่าคุณกำลังถามอะไรตามที่เขียนอยู่ในปัจจุบัน   -  person Demitrian    schedule 30.12.2016
comment
รหัสนี้แสดงชื่อของฟิลด์แอตทริบิวต์ ฉันต้องการให้รหัสแสดงค่าแอตทริบิวต์ (ไม่ใช่ชื่อของฟิลด์) ชัดเจนตอนนี้หรือไม่   -  person Fninja    schedule 30.12.2016


คำตอบ (2)


ง่ายด้วยรหัสนี้ (ตัวอย่างที่ 1):

function nt_product_attributes() {
global $product;
    if ( $product->has_attributes() ) {

        $attributes = ( object ) array (
        'color'              => $product->get_attribute( 'pa_color' ),
        'size'            => $product->get_attribute( 'pa_size' ),
        );
    return $attributes;
    }
}

การใช้งาน:

$attributes = nt_product_attributes();
echo $attributes->color;
echo $attributes->size;

หรือ (ตัวอย่างที่ 2)

function nt_product_attributes() {
global $product;
    if ( $product->has_attributes() ) {

        $attributes = array (
        'color'              => $product->get_attribute('pa_color'),
        'size'            => $product->get_attribute('pa_size'),
        );
    return $attributes;
    }
}

การใช้งาน:

$attributes = nt_product_attributes();
echo $attributes['color'];
echo $attributes['size'];
person novetrendy    schedule 08.07.2017

หากต้องการรับค่าแอตทริบิวต์ คุณต้องใช้ get_terms() โดยที่ภายในเป็นอาร์กิวเมนต์ slug คุณลักษณะผลิตภัณฑ์ WC ของคุณเป็น pa_size, pa_color... ดังนั้นในการแสดงค่าที่สอดคล้องกันสำหรับแต่ละคุณลักษณะ คุณจะต้องวนซ้ำ foreach ที่สอง

ดังนั้นโค้ดของคุณอาจเป็นดังนี้:

if (!function_exists('shop_attributes_in_loop')) {
    function shop_attributes_in_loop(){
        global $product;

        //Getting product attributes
        $product_attributes = $product->get_attributes();

        if(!empty($product_attributes)){

            //Getting product attributes slugs
            $product_attribute_slugs = array_keys($product_attributes);
            $count_slug = 0;

            echo '<div class="product_attributes">';

            foreach ($product_attribute_slugs as $product_attribute_slug){
                $count_slug++;

                // Removing "pa_" from attribute slug and adding a cap to first letter
                $attribute_name =  ucfirst( str_replace('pa_', '', $product_attribute_slug) );
                echo $attribute_name . ' (';

##  ===>  ===>  // Getting the product attribute values
                $attribute_values = get_terms($product_attribute_slug);
                $count_value = 0;
                foreach($attribute_values as $attribute_value){
                    $count_value++;
                    $attribute_name_value = $attribute_value->name; // name value
                    $attribute_slug_value = $attribute_value->slug; // slug value
                    $attribute_slug_value = $attribute_value->term_id; // ID value

                    // Displaying HERE the "names" values for an attribute
                    echo $attribute_name_value;
                    if($count_value != count($attribute_values)) echo ', ';
                }
                if($count_slug != count($product_attribute_slugs)) echo '), ';
                else echo ').';
            }
            echo '</div>';
        }
    }
} 
add_action('woocommerce_after_shop_loop_item', 'shop_attributes_in_loop');

และจะแสดงตัวอย่างบางอย่างเช่นนี้ (สำหรับค่าชื่อ pa_color และ pa_size):

Color (Black, Blue, Green), Size (30, 32, 34, 36, 38, 40, 42, 44).

โค้ดไปอยู่ในไฟล์ function.php ของธีมลูกที่ใช้งานอยู่ของคุณ (ธีมที่ใช้งานอยู่หรือในไฟล์ปลั๊กอินใดๆ)

โค้ดนี้ได้รับการทดสอบและใช้งานได้

person LoicTheAztec    schedule 01.01.2017