Với WooCommerce, chúng ta có thể dễ dàng thêm sản phẩm có nhiều biến thể thay đổi giá như màu sắc, kích cỡ, dung tích hay trọng lượng, kiểu dáng,.. Tuy nhiên, giá sản phẩm có nhiều biến thể được trình bày trong WooCommerce lại không rõ ràng, hiển thị một khoảng giá.

gia san pham bien the 3

Đây là cách xử lý vấn đề với sản phẩm hiển thị khoảng giá.

Thêm đoạn code đổi giá sản phẩm có biến thể Woocommerce vào file function.php

function wc_wc20_variation_price_format( $price, $product ) {
 //Main Price
 $prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
 $price = $prices[0] !== $prices[1] ? sprintf( __( 'Giá từ: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
  
 // Sale Price
 $prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
 sort( $prices );
 $saleprice = $prices[0] !== $prices[1] ? wc_price( $prices[0] ) : wc_price( $prices[0] );
  
 if ( $price !== $saleprice ) {
 $price = '<del>' . $saleprice . '</del> <ins>' . $price . '</ins>';
 }
 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 );

Kết quả sau khi chỉnh

gia san pham bien the 4

Liệt kê toàn bộ giá theo thuộc tính của từng sản phẩm

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.'&nbsp;';
 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 );

Lưu lại và xem thành quả.

gia san pham bien the 10

Chúc các bạn thực hiện thành công!

Chat hỗ trợ
Chat ngay