| 1 |
<?php namespace TierPricingTable\Addons\Coupons; |
| 2 |
|
| 3 |
use TierPricingTable\Addons\AbstractAddon; |
| 4 |
use WC_Cart; |
| 5 |
|
| 6 |
class CouponsAddon extends AbstractAddon { |
| 7 |
|
| 8 |
public function getName(): string { |
| 9 |
return __( 'Coupons management', 'tier-pricing-table' ); |
| 10 |
} |
| 11 |
|
| 12 |
public function run() { |
| 13 |
add_action( 'woocommerce_coupon_options', array( $this, 'addTieredPricingOption' ), 10, 2 ); |
| 14 |
add_action( 'woocommerce_coupon_options_save', array( $this, 'saveTieredPricingOption' ), 10, 2 ); |
| 15 |
|
| 16 |
add_action( 'tiered_pricing_table/cart/need_price_recalculation', array( |
| 17 |
$this, |
| 18 |
'checkAppliedCoupons', |
| 19 |
), 999, 3 ); |
| 20 |
|
| 21 |
add_action( 'tiered_pricing_table/cart/need_price_recalculation/item', array( |
| 22 |
$this, |
| 23 |
'checkAppliedCoupons', |
| 24 |
), 999, 3 ); |
| 25 |
} |
| 26 |
|
| 27 |
public function checkAppliedCoupons( $recalculate, $item, $cart = null ) { |
| 28 |
|
| 29 |
$cart = $cart instanceof WC_Cart ? $cart : wc()->cart; |
| 30 |
|
| 31 |
if ( ! $cart ) { |
| 32 |
return $recalculate; |
| 33 |
} |
| 34 |
|
| 35 |
$coupons = $cart->get_coupons(); |
| 36 |
|
| 37 |
if ( empty( $coupons ) ) { |
| 38 |
return $recalculate; |
| 39 |
} |
| 40 |
|
| 41 |
foreach ( $coupons as $coupon ) { |
| 42 |
|
| 43 |
if ( ! $coupon instanceof \WC_Coupon ) { |
| 44 |
continue; |
| 45 |
} |
| 46 |
|
| 47 |
if ( ! $coupon->is_valid_for_product( $item['data'] ) ) { |
| 48 |
continue; |
| 49 |
} |
| 50 |
|
| 51 |
if ( get_post_meta( $coupon->get_id(), '_disable_tiered_pricing', true ) === 'yes' ) { |
| 52 |
return false; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
return $recalculate; |
| 57 |
} |
| 58 |
|
| 59 |
public function saveTieredPricingOption( $couponId ) { |
| 60 |
update_post_meta( $couponId, '_disable_tiered_pricing', |
| 61 |
isset( $_REQUEST['_disable_tiered_pricing'] ) ? 'yes' : 'no' ); |
| 62 |
} |
| 63 |
|
| 64 |
public function addTieredPricingOption( $couponId, $coupon ) { |
| 65 |
|
| 66 |
woocommerce_wp_checkbox( array( |
| 67 |
'id' => '_disable_tiered_pricing', |
| 68 |
'label' => __( 'Disable tiered pricing when the coupon is applied', 'woocommerce' ), |
| 69 |
'description' => __( 'Check this option to don\'t apply tiered pricing in the cart when users have applied this coupon.', |
| 70 |
'tier-pricing-table' ), |
| 71 |
'value' => get_post_meta( $couponId, '_disable_tiered_pricing', true ), |
| 72 |
) ); |
| 73 |
} |
| 74 |
|
| 75 |
public function getDescription(): string { |
| 76 |
return __( 'Disable tiered pricing for specific coupons.', 'tier-pricing-table' ); |
| 77 |
} |
| 78 |
|
| 79 |
public function getIcon(): string { |
| 80 |
return '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M21.41 11.58l-9-9C12.05 2.22 11.55 2 11 2H4c-1.1 0-2 .9-2 2v7c0 .55.22 1.05.59 1.42l9 9c.36.36.86.58 1.41.58.55 0 1.05-.22 1.41-.59l7-7c.37-.36.59-.86.59-1.41 0-.55-.23-1.06-.59-1.42zM5.5 7C4.67 7 4 6.33 4 5.5S4.67 4 5.5 4 7 4.67 7 5.5 6.33 7 5.5 7z"/></svg>'; |
| 81 |
} |
| 82 |
|
| 83 |
public function getSlug(): string { |
| 84 |
return 'coupons'; |
| 85 |
} |
| 86 |
} |
| 87 |
|