We first prefer to use WordPress with WooCommerce for e-commerce related website because of using WordPress and WooCommerce we can easily achieve what we needed.
In eCommerce sometime we needed to use some custom fields like GiftWrap, Or And other details with the ordered item. that we can do easily using adding custom field into products. Some time we needed Coupons for custom added fields so for that we can do using simple steps as below.
For using below steps you must need technical skills because sometime if code place on wrong file or not set proper attributes then not working or your website will stop working.
Step 1:
First you need to create a custom field for products custom field restriction in coupon. that will allow admin to change that option value with deferent coupons.
//Add option in coupon to apply code only in Gift Wrap products
function add_coupon_gw_only() {
woocommerce_wp_checkbox( array( 'id' => 'gw_only', 'label' => __( 'Gift Wrap Only', 'woocommerce' ), 'description' => sprintf( __( 'Apply coupon only on Gift Wrap products', 'woocommerce' ) ) ) );
}
add_action( 'woocommerce_coupon_options_usage_restriction', 'add_coupon_gw_only', 10, 0 );
//Save meta field value when coupon save
function save_add_coupon_gw_only( $post_id ) {
$include_stats = isset( $_POST['gw_only'] ) ? 'yes' : 'no';
update_post_meta( $post_id, 'gw_only', $include_stats );
}
add_action( 'woocommerce_coupon_options_save', 'save_add_coupon_gw_only');
So above code will add one checkbox option under the coupon > usage restriction tab as below
Step 2:
In step 2 we need to check above custom field value before applying coupon code. there is many hooks but all we have need to check item key based validation because our custom filed store with item key when we add items into cart.
add_filter( 'woocommerce_coupon_get_discount_amount', 'woocommerce_coupon_get_discount_amount', 20, 5 );
function woocommerce_coupon_get_discount_amount( $discount, $discounting_amount, $cart_item, $single, $coupon ) {
$gw_only = get_post_meta( $coupon->get_id(), 'gw_only', true );
if ( $gw_only=="yes" && ! is_null( $cart_item )) {
if(isset($cart_item['cover_wrap']) && $cart_item['cover_wrap']==true)
return $discount;
global $woocommerce;
$woocommerce->cart->remove_coupons();
wc_add_notice("You applied coupon is only valid for Gift wrapping items.", "notice");
return 0;
}
}
So above code is checking items custom field and you can check with other your custom files using above way also there is many hooks available but those depend on your need.
More webhook is below for Coupon related validations.
woocommerce_coupon_is_valid_for_cart
//Check if a coupon is valid.
woocommerce_coupon_is_valid_for_cart
//Check if a coupon is valid for a product
woocommerce_coupon_is_valid_for_product
//Get discount amount for a cart item
woocommerce_coupon_get_discount_amount
//Map one of the WC_Coupon message codes to a message string.
woocommerce_coupon_message
//Map one of the WC_Coupon error codes to a message string
woocommerce_coupon_error
//Load coupon data
woocommerce_get_shop_coupon_data
More info review coupon class of WooCommerce https://github.com/woocommerce/woocommerce/blob/master/includes/class-wc-coupon.php
And If you need to do your own just contact me or drop your comment below. Thank you
Jose says
Hi, how can I put a drop down field foreach coupon applied in the cart?
Or, before submit a coupon code, make a question to the customer to know where had he get that code…it is possible? Thanks!