Last active
April 15, 2020 16:15
-
-
Save iamchrissmith/566ed57e8e72f96a752e to your computer and use it in GitHub Desktop.
Two functions to change the way WooCommerce counts coupon usage so Product coupons are counted each time they are applied not once per cart.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function crc_woocommerce_display_coupons(){ | |
global $wpdb; | |
$coupon_id = $wpdb->get_results( | |
$wpdb->prepare( | |
" | |
SELECT ID FROM $wpdb->posts | |
WHERE post_type = %s | |
AND post_status = 'publish' | |
", | |
'shop_coupon' | |
) | |
); | |
if (!$coupon_id) | |
return; | |
$appliedCoupons = WC()->cart->get_applied_coupons(); | |
if ( isset($appliedCoupons[0]) ) : | |
$currentCoupon = $appliedCoupons[0]; | |
endif; | |
echo '<label for="coupon-dropdown">Please select your water provider (will be verified): </label>'; | |
echo '<select id="coupon-dropdown" class="coupon">'; | |
$options = '<option value="">---SELECT ONE---</option>'; | |
foreach ($coupon_id as $id) : | |
$totalvalid = 0; | |
$coupon = get_post($id->ID); | |
$couponmeta = get_post_meta( $id->ID ); | |
// $this->post_title = apply_filters( 'woocommerce_coupon_code', $coupon->post_title ); | |
$couponcode = apply_filters( 'woocommerce_coupon_code', $coupon->post_title ); | |
$couponname = ucwords(str_replace('-',' ',$couponcode)); | |
$coupon = new WC_Coupon( $couponcode ); | |
if ( isset($couponmeta['usage_count']) ) : | |
$couponsused = $couponmeta['usage_count'][0]; | |
else : | |
$couponsused = 0; | |
endif; | |
if ( isset($couponmeta['usage_limit']) ) : | |
$couponlimit = $couponmeta['usage_limit'][0]; | |
else : | |
$couponlimit = 0; | |
$couponsused = -1; | |
endif; | |
$couponsleft = $couponlimit - $couponsused; | |
if ( $couponsleft > 0 ) : | |
$countvalid = TRUE; | |
$couponvalid = FALSE; | |
foreach (WC()->cart->cart_contents as $product): | |
// print_r($product); | |
$product_id = $product['product_id']; | |
$product_qty = $product['quantity']; | |
// echo "Product ID: ".$product_id; | |
// echo "<br/>"; | |
// echo "Product QTY: ".$product_qty; | |
// echo "<br/>"; | |
// $current_product = new WC_Product(); | |
$this_product = get_product($product_id); | |
if (!$couponvalid && $coupon->is_valid_for_product($this_product) ) : | |
$couponvalid = TRUE; | |
endif; | |
if ( $coupon->is_valid_for_product($this_product) ) : | |
$totalvalid += $product_qty; | |
endif; | |
if ($totalvalid > $couponsleft) : | |
$countvalid = FALSE; | |
endif; | |
endforeach; | |
if ( $couponvalid && $countvalid ): | |
$options .= '<option '.selected($currentCoupon, $couponcode).' value="'.$couponcode.'">'.$couponname.'</option>'; | |
elseif ( !$countvalid ) : | |
WC()->cart->remove_coupon( $couponcode ); | |
WC()->cart->calculate_totals(); | |
endif; | |
endif; | |
endforeach; | |
if ($options != '<option value="">---SELECT ONE---</option>' ) { | |
echo $options; | |
} else { | |
echo '<option value="">---No Discounts Available---</option>'; | |
} | |
?> | |
</select> | |
<input type="hidden" name="coupon_code" class="input-text" id="coupon_code" value="" /> | |
<input type="submit" class="button" name="apply_coupon" value="<?php _e( 'Apply Discount', 'woocommerce' ); ?>" /> | |
<p style="clear:both;"><em>Note: If your water provider is not listed, there are no discounts available in your area. If no water providers are listed, then your product does not qualify for a discount. Please proceed to checkout.</em></p> | |
<?php | |
} | |
add_action('woocommerce_cart_coupon', 'crc_woocommerce_display_coupons',5); | |
function crc_correct_coupon_usage_inc() { | |
//print_r(WC()); | |
foreach (WC()->cart->coupon_applied_count as $code => $amount) { | |
foreach ( WC()->cart->coupons as $coupon ) { | |
if ( $coupon->discount_type == 'fixed_product' ) { | |
$increase_count_by = $amount-1; | |
// echo "Coupon Code: ".$code."<br/>"; | |
// echo "Coupon Usage amount: ".$amount; | |
$i = 1; | |
while ( $i < $amount ) { | |
if ( ! $code ) { | |
continue; | |
} | |
$coupon = new WC_Coupon( $code ); | |
$order = new WC_Order( $order_id ); | |
$used_by = $order->get_user_id(); | |
if ( ! $used_by ) { | |
$used_by = $order->billing_email; | |
} | |
$coupon->inc_usage_count( $used_by ); | |
$i++; | |
} | |
} | |
} | |
} | |
} | |
add_action( 'woocommerce_checkout_update_order_meta', 'crc_correct_coupon_usage_inc'); |
No problem. Thanks for responding. No, not looking for development work, just hoping a solution that was posted publicly still works.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry, this has not been maintained for several years. If you need WooCommerce development work Sofo Studios took over that client work.