Ürün İndirim Kodu
Herhangi bir eklenti kullanmadan WooCommerce ürün görsellerinde indirim miktarını nasıl görüntüleyeceğimizi görelim. İndirim bilgilerini görüntüleyerek daha fazla müşteri çekebilir ve satışlarınızı artırabilirsiniz
PHP kodu
// On-sale label on shop page
add_action( 'woocommerce_before_shop_loop_item', 'softexpert_show_sale_percentage_shop_page', 8 );
function softexpert_show_sale_percentage_shop_page() {
global $product;
if ( ! $product->is_on_sale() ) return;
if ( $product->is_type( 'simple' ) ) {
$max_percentage = ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100;
} elseif ( $product->is_type( 'variable' ) ) {
$max_percentage = 0;
foreach ( $product->get_children() as $child_id ) {
$variation = wc_get_product( $child_id );
$price = $variation->get_regular_price();
$sale = $variation->get_sale_price();
if ( $price != 0 && ! empty( $sale ) ) $percentage = ( $price - $sale ) / $price * 100;
if ( $percentage > $max_percentage ) {
$max_percentage = $percentage;
}
}
}
if ( $max_percentage > 0 ) echo "" . round($max_percentage) . "% indirim";
}
// On-sale label on single product page
add_action( 'woocommerce_before_single_product_summary', 'softexpert_show_sale_percentage_single_page', 8 );
function softexpert_show_sale_percentage_single_page() {
global $product;
if ( ! $product->is_on_sale() ) return;
if ( $product->is_type( 'simple' ) ) {
$max_percentage = ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100;
} elseif ( $product->is_type( 'variable' ) ) {
$max_percentage = 0;
foreach ( $product->get_children() as $child_id ) {
$variation = wc_get_product( $child_id );
$price = $variation->get_regular_price();
$sale = $variation->get_sale_price();
if ( $price != 0 && ! empty( $sale ) ) $percentage = ( $price - $sale ) / $price * 100;
if ( $percentage > $max_percentage ) {
$max_percentage = $percentage;
}
}
}
if ( $max_percentage > 0 ) echo "" . round($max_percentage) . "% indirim";
}
CSS kodu
.sale-price-off {
font-size: 13px;
color: #fff;
position: absolute;
z-index: 2;
background-color: #E11500;
clip-path: polygon(0% 0%, 100% 0, 98% 92%, 49% 100%, 0 92%);
min-height: 48px;
display: flex;
align-items: center;
text-align: center;
width: 55px;
line-height: 17px;
font-weight: 600;
border-top-left-radius: 0px;
}
.onsale{
display: none !important;
}