Advertisement
Guest User

Untitled

a guest
Oct 26th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.19 KB | None | 0 0
  1. class Coupon_Discount_Calculator {
  2.     private $context = null;
  3.  
  4.     public function __construct() {
  5.         add_filter( "adp_context_created", array( $this, 'save_context' ) );
  6.         add_filter( 'wdp_amount_saved', array( $this, 'add_woocommerce_coupon_discounts_to_saved_amount' ), 10, 2 );
  7.     }
  8.  
  9.     public function save_context( $context ) {
  10.         if ( !isset( $this->context ) ) {
  11.             $this->context = $context;
  12.         }
  13.         return $context;
  14.     }
  15.  
  16.     public function add_woocommerce_coupon_discounts_to_saved_amount( $amount_saved, $cart_items ) {
  17.         $includeTax = 'incl' === $this->context->get_tax_display_cart_mode();
  18.  
  19.         foreach ( WC()->cart->get_coupons() as $wcCoupon ) {
  20.             $code = $wcCoupon->get_code();
  21.             $adpData = $wcCoupon->get_meta( 'adp', true, 'edit' );
  22.  
  23.             if ( !isset( $adpData['parts'] ) ) {
  24.                 /** @var $coupon CouponCart */
  25.                 $woocommerce_coupon_discount = WC()->cart->get_coupon_discount_amount( $code, !$includeTax );
  26.                 $amount_saved += $woocommerce_coupon_discount;
  27.             }
  28.         }
  29.  
  30.         return $amount_saved;
  31.     }
  32. }
  33.  
  34. new Coupon_Discount_Calculator();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement