Thanks to @Bhautik for helping to correct some functions to auto-generate WooCommerce coupons when applied with a URL. Post is here - How to auto generate a coupon in WooCommerce and apply it to the cart?
I'm now trying to add a free product to this same function. I've kinda got it working but with errors. If anyone could chip in with some advice or help would be greatly appreciated. Please see screenshots below. Thanks in advance.
https://i.stack.imgur.com/Muwdn.gif
https://i.stack.imgur.com/3F9GW.gif
https://i.stack.imgur.com/m9mXF.gif
Here's my code so far.
function coupon_exists( $coupon_code ) {
global $wpdb;
$sql = $wpdb->prepare( "SELECT post_name FROM $wpdb->posts WHERE post_type = 'shop_coupon' AND post_name = '%s'", $coupon_code );
$coupon_codes = $wpdb->get_results($sql);
if ( count( $coupon_codes ) > 0 ) {
return true;
} else {
return false;
}
}
// Generate coupon
function generate_coupon($coupon_generated) {
// Set some coupon data by default
$date_expires = date('Y-m-d H:i:s', strtotime('+24 hours'));
$discount_type = 'percent';
$coupon_amount = '100';
$coupon = new WC_Coupon();
$coupon->set_code($coupon_generated);
$coupon->set_discount_type($discount_type);
$coupon->set_amount($coupon_amount);
$coupon->set_date_expires($date_expires);
$coupon->set_individual_use(true);
$coupon->set_product_ids (array(74));
$coupon->set_usage_limit(1);
$coupon->set_limit_usage_to_x_items(1);
$coupon->set_usage_limit_per_user(1);
$coupon->set_exclude_sale_items(true);
$coupon->set_minimum_amount('100');
$coupon->set_maximum_amount('150');
$coupon->save();
return $coupon_generated;
}
// Add coupon and product to cart
function generate_coupon_add_product_to_cart ( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$coupon_code = ( isset( $_GET['coupon-code'] ) && $_GET['coupon-code'] != '' ) ? $_GET['coupon-code'] : '' ;
if( $coupon_code == '' ){
return;
}
$applied_coupons = $cart->get_applied_coupons();
if( empty( $applied_coupons ) || ! in_array( $coupon_code, $applied_coupons ) ){
if ( !coupon_exists( $coupon_code ) ) {
generate_coupon( $coupon_code );
}
}
// I'm having trouble with the code below this point
$free_product_id = 74;
$free_product_in_cart = false;
foreach( $cart->get_cart() as $cart_item_key => $cart_item ) {
$cart_count = $cart->get_cart_contents_count();
$free_product = $cart_item['data'];
if ( $free_product->get_id() == $free_product_id ) {
$free_product_key = $cart_item_key;
$regular_price = $cart_item['data']->get_regular_price();
$cart_item['data']->set_price(0);
$free_product_in_cart = true;
}
}
if ( !in_array($coupon_code, $applied_coupons) && !$free_product_in_cart ){
$cart->add_to_cart( $free_product_id, 1 );
$cart->add_discount( $coupon_code );
wc_add_notice( sprintf(__('<strong>FREE</strong> product valued at %s has been added to your cart.', 'woocommerce'),'<strong>' . get_woocommerce_currency_symbol() . $regular_price . '</strong>'), 'notice');
}
// Removing coupon
elseif( !in_array($coupon_code, $applied_coupons) && $free_product_in_cart ){
$cart->remove_coupon( $coupon_code );
wc_add_notice( __("Coupon removed"), 'notice' );
}
// Removing free product if it is the only one left in cart
elseif ( $free_product_in_cart && $cart_count <= 1 ){
$cart->remove_cart_item( $free_product_key );
wc_add_notice( __("Product removed"), 'notice' );
}
}
add_action('woocommerce_before_calculate_totals', 'generate_coupon_add_product_to_cart');```