user3467855 user3467855 - 1 month ago 13
PHP Question

Getting applied coupons in cart in woocommerce_init hook

I am trying to get the coupons used in order under woocommerce_init. Tried to view the array $GLBALS and found smth related but don't know how to access them.

function remove_email_for_testcop_coupon($order_id) {
global $woocommerce;
$coupons = $woocommerce->cart->applied_coupons;
var_dump($GLOBALS['GLOBALS']['wp_filter']['init']); //=> here there are found but don't know how to access it normaly => need to reach applied_coupons, just that is generating a dinamic nonce.
remove_action( 'init', array( 'WC_Emails', 'init_transactional_emails' ) );
if (in_array('testcop', $coupons)) {
remove_action( 'init', array( 'WC_Emails', 'init_transactional_emails' ) );
}
}
//add_action('wp', 'remove_email_for_testcop_coupon');
add_action( 'woocommerce_init', 'remove_email_for_testcop_coupon');

Answer

Managed to find out how:

function remove_email_for_testcop_coupon($order_id) {
    global $wp;
    $coupons = array();
    $cpage = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    if (strpos($cpage, '/course/') === false) {
        $obj = new WC_Session_Handler();
        $data =  accessProtected($obj, '_data');
        $coupons = unserialize($data['applied_coupons']);
        if (!empty($coupons)) {
            if (in_array('masterkey', $coupons) || in_array('testcop', $coupons)) {
                remove_action( 'init', array( 'WC_Emails', 'init_transactional_emails' ) );
            }
        }
    }
}
add_action( 'woocommerce_init', 'remove_email_for_testcop_coupon');
Comments