درست کردن درصد تخفیف در المنتور برای محصولات وکامرس
شما میتونید با استفاده از این تکه کد و این آموزش درصد تخفیف محصولات سایتتو در همه جا از سایتت میتونی نشون بدی
کد درصد تخفیف
زبان: PHP
function wc_discount_percent_shortcode($atts) {
$atts = shortcode_atts(array(
'id' => null,
), $atts, 'wc_discount_percent');
if (!empty($atts['id'])) {
$product = wc_get_product($atts['id']);
} else {
global $product;
if (!is_object($product) || !method_exists($product, 'is_on_sale')) {
return '';
}
}
if (!is_object($product) || !method_exists($product, 'is_on_sale')) {
return '';
}
if ($product->is_on_sale()) {
if ($product->is_type('variable')) {
$percentages = array();
$variation_prices = $product->get_variation_prices();
foreach ($variation_prices['regular_price'] as $key => $regular_price) {
if ($regular_price > 0) {
$sale_price = $variation_prices['sale_price'][$key];
$percentage = round((($regular_price - $sale_price) / $regular_price) * 100);
$percentages[] = $percentage;
}
}
$percentage = !empty($percentages) ? max($percentages) : 0;
} else {
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
if ($regular_price > 0 && $sale_price > 0) {
$percentage = round((($regular_price - $sale_price) / $regular_price) * 100);
} else {
$percentage = 0;
}
}
if ($percentage > 0) {
return $percentage . '%';
}
}
return '';
}
add_shortcode('wc_discount_percent', 'wc_discount_percent_shortcode');