Dapatkan dan tampilkan nilai atribut produk di WooCommerce

Di WooCommerce, saya memiliki kode ini untuk menampilkan slug atribut produk pada halaman arsip seperti halaman toko :

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');

Mereka hanya menampilkan nama bidang atribut sebagai pa_size, pa_color

Bagaimana cara mendapatkan dan menampilkan nilai untuk atribut produk tersebut (seperti 2kg, 3kg atau blue, green) ?

Terima kasih.


person Fninja    schedule 30.12.2016    source sumber
comment
Sangat sulit untuk mengetahui apa yang Anda tanyakan seperti yang tertulis saat ini.   -  person Demitrian    schedule 30.12.2016
comment
kode ini menunjukkan nama bidang atribut. saya ingin kode menunjukkan nilai atribut (bukan nama bidang) jelas sekarang?   -  person Fninja    schedule 30.12.2016


Jawaban (2)


Sederhana saja dengan kode ini (Contoh 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;
    }
}

Penggunaan:

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

ATAU (Contoh 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;
    }
}

Penggunaan:

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

Untuk mendapatkan nilai atribut, Anda perlu menggunakan get_terms() , dengan argumen di dalam sebagai slug atribut produk WC Anda sebagai pa_size, pa_color… Jadi untuk menampilkan nilai yang sesuai untuk setiap atribut, Anda memerlukan loop foreach kedua.

Jadi kode Anda bisa seperti ini:

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');

dan misalnya akan menampilkan sesuatu seperti ini (untuk nilai nama pa_color dan pa_size):

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

Kode masuk dalam file function.php dari tema anak aktif Anda (tema aktif atau dalam file plugin apa pun).

Kode ini telah diuji dan berfungsi.

person LoicTheAztec    schedule 01.01.2017