"coupon code in link woocommerce" Code Answer's
You're definitely familiar with the best coding language Whatever that developers use to develop their projects and they get all their queries like "coupon code in link woocommerce" answered properly. Developers are finding an appropriate answer about coupon code in link woocommerce related to the Whatever coding language. By visiting this online portal developers get answers concerning Whatever codes question like coupon code in link woocommerce. Enter your desired code related query in the search bar and get every piece of information about Whatever code related question on coupon code in link woocommerce.
woocommerce apply coupon with add to cart
function wc_ninja_apply_coupon( $coupon_code ) {
if ( 'test_code' === $coupon_code ) {
$product_id = 99;
WC()->cart->add_to_cart( $product_id );
}
}
add_action( 'woocommerce_applied_coupon', 'wc_ninja_apply_coupon' );
Source: wordpress.org
coupon code in link woocommerce
// Set coupon code as custom data in cart session
add_action('wp_loaded', 'add_coupon_code_to_cart_session');
function add_coupon_code_to_cart_session() {
// Exit if no code in URL or if the coupon code is already set cart session
if( empty( $_GET["code"] ) || WC()->session->get( 'custom_discount' ) ) return;
if( ! WC()->session->get( 'custom_discount' ) ) {
$coupon_code = esc_attr($_GET["code"]);
WC()->session->set( 'custom_discount', $coupon_code );
// If there is an existing non empty cart active session we apply the coupon
if( ! WC()->cart->is_empty() ){
WC()->cart->add_discount( $coupon_code );
}
}
}
// Add coupon code when a product is added to cart once
add_action('woocommerce_add_to_cart', 'add_coupon_code_to_cart', 10, 6 );
function add_coupon_code_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ){
$coupon_code = WC()->session->get( 'custom_discount' );
$applied_coupons = WC()->session->get('applied_coupons');
if( empty($coupon_code) || in_array( $coupon_code, $applied_coupons ) ) return;
WC()->cart->add_discount( $coupon_code );
}
// Remove coupon code when user empty his cart
add_action('woocommerce_cart_item_removed', 'check_coupon_code_cart_items_removed', 10, 6 );
function check_coupon_code_cart_items_removed( $cart_item_key, $cart ){
$coupon_code = WC()->session->get( 'custom_discount' );
if( $cart->has_discount( $coupon_code ) && $cart->is_empty() );
$cart->remove_coupon( $coupon_code );
}
Source: stackoverflow.com
All those coders who are working on the Whatever based application and are stuck on coupon code in link woocommerce can get a collection of related answers to their query. Programmers need to enter their query on coupon code in link woocommerce related to Whatever code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about coupon code in link woocommerce for the programmers working on Whatever code while coding their module. Coders are also allowed to rectify already present answers of coupon code in link woocommerce while working on the Whatever language code. Developers can add up suggestions if they deem fit any other answer relating to "coupon code in link woocommerce". Visit this developer's friendly online web community, CodeProZone, and get your queries like coupon code in link woocommerce resolved professionally and stay updated to the latest Whatever updates.