Thay chữ “Giảm giá” bằng chữ khác hoặc bằng % trong woocommerce

Thay chữ giảm bằng chữ khác trong woocommerce

Chào các bạn trong những ngày vừa qua có rất nhiều bạn mới làm web có hỏi mình trong woocommerce của wordpress cách thay chữ “Giảm giá” như hình bên dưới thành chữ khác, hoặc bằng bằng % giảm giá. Hôm nay mình viết bài này chia sẽ để mọi người cùng tham khảo nhé

Thay chữ “Giảm giá” bằng chữ khác trong woocommerce

Việc thay này khá đơn giản bạn Mở của bạn  functions.php tập tin nằm trong  wp-content / themes / tên theme / và thêm mã này ở phần cuối của nó

add_filter( ‘woocommerce_sale_flash’, ‘wc_custom_replace_sale_text’ );

function wc_custom_replace_sale_text( $html ) {

return str_replace( __( ‘Sale!’, ‘woocommerce’ ), __( ‘My sale text!’, ‘woocommerce’ ), $html );

}

Thay chữ “Giảm giá” bằng % giảm giá trong woocommerce

Bạn chỉ cần thêm đoạn code sau vào file functions.php của theme trang sử dụng là được nhé

/*
* Thay chữ Sale thành phần trăm (%) giảm giá
* Author: levantoan.com
*/
add_filter(‘woocommerce_sale_flash’, ‘devvn_woocommerce_sale_flash’, 10, 3);
function devvn_woocommerce_sale_flash($html, $post, $product){
return ‘<span class=”onsale”><span>’ . devvn_presentage_bubble($product) . ‘</span></span>’;
}

function devvn_presentage_bubble( $product ) {
$post_id = $product->get_id();

if ( $product->is_type( ‘simple’ ) || $product->is_type( ‘external’ ) ) {
$regular_price  = $product->get_regular_price();
$sale_price     = $product->get_sale_price();
$bubble_content = round( ( ( floatval( $regular_price ) – floatval( $sale_price ) ) / floatval( $regular_price ) ) * 100 );
} elseif ( $product->is_type( ‘variable’ ) ) {
if ( $bubble_content = devvn_percentage_get_cache( $post_id ) ) {
return devvn_percentage_format( $bubble_content );
}

$available_variations = $product->get_available_variations();
$maximumper           = 0;

for ( $i = 0; $i < count( $available_variations ); ++ $i ) {
$variation_id     = $available_variations[ $i ][‘variation_id’];
$variable_product = new WC_Product_Variation( $variation_id );
if ( ! $variable_product->is_on_sale() ) {
continue;
}
$regular_price = $variable_product->get_regular_price();
$sale_price    = $variable_product->get_sale_price();
$percentage    = round( ( ( floatval( $regular_price ) – floatval( $sale_price ) ) / floatval( $regular_price ) ) * 100 );
if ( $percentage > $maximumper ) {
$maximumper = $percentage;
}
}

$bubble_content = sprintf( __( ‘%s’, ‘woocommerce’ ), $maximumper );

devvn_percentage_set_cache( $post_id, $bubble_content );
} else {
$bubble_content = __( ‘Sale!’, ‘woocommerce’ );

return $bubble_content;
}

return devvn_percentage_format( $bubble_content );
}

function devvn_percentage_get_cache( $post_id ) {
return get_post_meta( $post_id, ‘_devvn_product_percentage’, true );
}

function devvn_percentage_set_cache( $post_id, $bubble_content ) {
update_post_meta( $post_id, ‘_devvn_product_percentage’, $bubble_content );
}

//Định dạng kết quả dạng -{value}%. Ví dụ -20%
function devvn_percentage_format( $value ) {
return str_replace( ‘{value}’, $value, ‘-{value}%’ );
}

// Xóa cache khi sản phẩm hoặc biến thể thay đổi
function devvn_percentage_clear( $object ) {
$post_id = ‘variation’ === $object->get_type()
? $object->get_parent_id()
: $object->get_id();

delete_post_meta( $post_id, ‘_devvn_product_percentage’ );
}
add_action( ‘woocommerce_before_product_object_save’, ‘devvn_percentage_clear’ );

Sau đó bạn thêm code CSS này nữa vào file css bạn có ngay % giảm giá cực đẹp

.badge-container.absolute.left.top.z-1

{

border-radius: 50%;
color: #ffffff;
font-size: 14px;
font-weight: 400;
height: 40px;
line-height: 40px;
position: absolute;
left: 10px;
text-align: center;
text-transform: uppercase;
width: 40px;
z-index: 2;
background-image: linear-gradient(
-90deg,#ec1f1f 0%,#ff9c00 100%);
margin-top: 48px;
}

Cách xỏa bỏ luôn chữ “giảm giá” trong woocommerce

Cách xóa bỏ bạn không cần tìm code nào cả bạn vào phần CSS và copy đoạn code dưới đây là được

.badge-container
{
display:none;
}

Các thao tác chỉnh sửa có vấn đề gì hãy liên hệ với chúng rôi để được tư vấn và hỗ trợ

Xem thêm: Tên miền là gì (domain)

Chúc bạn thành công !

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *