Mình làm theo: https:// levantoan.com/thay-doi-cach-hien-thi-gia-cho-cac.../ Nhưng nó hiển thị giá của biến thể, tuy nhiên chỉ hiện giá biến thể, không hiển thị tên biến thể bác bác cho mình hỏi làm sao để hiển thị ạ Code: Code: function find_valid_variations() { global $product; $variations = $product->get_available_variations(); $attributes = $product->get_attributes(); $new_variants = array(); // Loop through all variations foreach( $variations as $variation ) { // Peruse the attributes. // 1. If both are explicitly set, this is a valid variation // 2. If one is not set, that means any, and we must 'create' the rest. $valid = true; // so far foreach( $attributes as $slug => $args ) { if( array_key_exists("attribute_$slug", $variation['attributes']) && !empty($variation['attributes']["attribute_$slug"]) ) { // Exists } else { // Not exists, create $valid = false; // it contains 'anys' // loop through all options for the 'ANY' attribute, and add each foreach( explode( '|', $attributes[$slug]['value']) as $attribute ) { $attribute = trim( $attribute ); $new_variant = $variation; $new_variant['attributes']["attribute_$slug"] = $attribute; $new_variants[] = $new_variant; } } } // This contains ALL set attributes, and is itself a 'valid' variation. if( $valid ) $new_variants[] = $variation; } return $new_variants; } function list_price_variable(){ global $product, $post; $variations = find_valid_variations(); // Check if the special 'price_grid' meta is set, if it is, load the default template: if ( get_post_meta($post->ID, 'price_grid', true) ) { // Enqueue variation scripts wp_enqueue_script( 'wc-add-to-cart-variation' ); // Load the template wc_get_template( 'single-product/add-to-cart/variable.php', array( 'available_variations' => $product->get_available_variations(), 'attributes' => $product->get_variation_attributes(), 'selected_attributes' => $product->get_variation_default_attributes() ) ); return; } // Cool, lets do our own template! ?> <table class="variations variations-grid" cellspacing="0"> <tbody> <?php foreach ($variations as $key => $value) { if( !$value['variation_is_visible'] ) continue; ?> <tr> <td> <?php foreach($value['attributes'] as $key => $val ) { $val = str_replace(array('-','_'), ' ', $val); $category_slug = str_replace('attribute_', '', $key); $category = get_term_by('slug', ucwords($val), $category_slug); $categoryName = $category->name.' '; printf( '<span class="attr attr-%s">%s</span>', $key, $categoryName); } ?> </td> <td> <?php echo $value['price_html'];?> </td> </tr> <?php } ?> </tbody> </table> <?php } function wc_wc20_variation_price_format( $price, $product ) { $price = list_price_variable(); return $price; } //add_filter( 'woocommerce_variable_sale_price_html', 'wc_wc20_variation_price_format', 10, 2 ); add_filter( 'woocommerce_variable_price_html', 'wc_wc20_variation_price_format', 10, 2 ); --- Double Post Merged, Nov 27, 2020, Original Post Date: Nov 27, 2020 --- Nó chỉ hiển thị giá chứ không hiển thị mấy chứ như nhỏ vừa lớn, các bác chỉ dùm mình với (
Thớt print đoạn bên dưới => loop thử xem! PHP: <?phpprint_r(wc_get_product($post->ID)->get_available_variations());?>