Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xlplugins/f4b6bbc0013f455831e075fbb507c56a to your computer and use it in GitHub Desktop.
Save xlplugins/f4b6bbc0013f455831e075fbb507c56a to your computer and use it in GitHub Desktop.
sustain applied coupon during with aero-add-to-checkout parameter
class WFACP_AERO_ADD_TO_CHECKOUT_Coupon {
private $wfacp_woocommerce_applied_coupon;
public function __construct() {
add_action( 'wfacp_add_to_cart_init', [ $this, 'capture_applied_coupons' ] );
}
public function capture_applied_coupons() {
if ( ! isset( $_GET['aero-add-to-checkout'] ) ) {
return;
}
$this->wfacp_woocommerce_applied_coupon = WC()->session->get( 'wfacp_woocommerce_applied_coupon_' . WFACP_Common::get_id(), [] );
if ( empty( $this->wfacp_woocommerce_applied_coupon ) ) {
return;
}
$this->wfacp_woocommerce_applied_coupon = WC()->cart->get_applied_coupons();;
add_action( 'wfacp_after_checkout_page_found', [ $this, 'apply_coupons' ] );
}
public function apply_coupons() {
try {
$wfacp_woocommerce_applied_coupon = $this->wfacp_woocommerce_applied_coupon;
foreach ( $wfacp_woocommerce_applied_coupon as $coupon_id ) {
$coupon_id = trim( $coupon_id );
WC()->cart->add_discount( $coupon_id );
}
} catch ( Exception $e ) {
}
}
}
new WFACP_AERO_ADD_TO_CHECKOUT_Coupon();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment