Skip to content

Instantly share code, notes, and snippets.

@artikus11
Created December 30, 2020 17:39
Show Gist options
  • Save artikus11/0e494809a59e5a85c72c529f9dd415c0 to your computer and use it in GitHub Desktop.
Save artikus11/0e494809a59e5a85c72c529f9dd415c0 to your computer and use it in GitHub Desktop.
/**
* Вывод скидки по купону в подытогах каждого товара
*
* @param $product_subtotal
* @param $product
* @param $quantity
* @param $cart
*
* @return string
*/
function art_add_discount_in_subtotal( $product_subtotal, $product, $quantity, $cart ) {
$coupons = WC()->cart->get_coupons();
$coupon_amount = 0;
foreach ( $coupons as $code => $coupon ) {
if ( 'percent' === $coupon->get_discount_type() ) {
$coupon_amount += (int) $coupon->get_amount();
}
}
if ( $coupon_amount ) {
$price = $product->get_price();
$row_price = $price * $quantity;
$price_coupon = $price - $price * (int) $coupon_amount / 100;
$row_price_coupon = $price_coupon * $quantity;
return wc_format_sale_price( $row_price, $row_price_coupon ) . $product->get_price_suffix();
}
return $product_subtotal;
}
add_action( 'woocommerce_cart_product_subtotal', 'art_add_discount_in_subtotal', 10, 4 );
/**
* Вывод скидки по купону у цены каждого товара
*
* @param $price
* @param $product
*
* @return string
*/
function art_add_discount_in_price( $price, $product ) {
$coupons = WC()->cart->get_coupons();
$coupon_amount = 0;
foreach ( $coupons as $code => $coupon ) {
if ( 'percent' === $coupon->get_discount_type() ) {
$coupon_amount += (int) $coupon->get_amount();
}
}
if ( $coupon_amount ) {
$price = $product->get_price();
$price_coupon = $price - $price * $coupon_amount / 100;
return wc_format_sale_price( $price, $price_coupon ) . $product->get_price_suffix();
}
return $price;
}
add_action( 'woocommerce_cart_product_price', 'art_add_discount_in_price', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment