在 woocommerce_init 挂钩中获取购物车中已应用的优惠券
Getting applied coupons in cart in woocommerce_init hook
我正在尝试获取 woocommerce_init 下按顺序使用的优惠券。试图查看数组 $GLBALS 并发现一些相关但不知道如何访问它们。
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');
设法找出方法:
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');
我正在尝试获取 woocommerce_init 下按顺序使用的优惠券。试图查看数组 $GLBALS 并发现一些相关但不知道如何访问它们。
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');
设法找出方法:
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');