Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save FrancoStino/cdcbf099421885987ac6545066f861a0 to your computer and use it in GitHub Desktop.
Save FrancoStino/cdcbf099421885987ac6545066f861a0 to your computer and use it in GitHub Desktop.
Discount as fee on cart at first order exclude product on sale and if is applied a coupon disable fee - Version Update
<?php
/**
* Discount as fee on cart at first order exclude product on sale and if is applied a coupon disable fee - Version Update
*/
add_action('woocommerce_cart_calculate_fees' , 'custom_discount', 10, 1);
function custom_discount( $cart ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) || ! is_user_logged_in() )
return;
// Getting "completed" customer orders
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => 'shop_order', // WC orders post type
'post_status' => 'wc-completed' // Only orders with status "completed"
) );
// Orders count
$customer_orders_count = count($customer_orders);
// Set variable
$new_subtotal = 0;
// Loop though each cart items and set prices in an array
foreach ( $cart->get_cart() as $cart_item ) {
// Get product
$product = wc_get_product( $cart_item['product_id'] );
// Product has no discount
if ( ! $product->is_on_sale() ) {
// line_subtotal
$line_subtotal = $cart_item['line_subtotal'];
// Add to new subtotal
$new_subtotal += $line_subtotal;
}
}
## Discount calculation ##
$discount = (-10 / 100) * $new_subtotal; // or WC()->cart->get_total_ex_tax()
## Applied discount (no products on sale) ##
if( !$product->is_on_sale() && empty($customer_orders) || $customer_orders_count == 0){
$cart->add_fee( 'Sconto del 10% sul primo ordine', $discount);
if( sizeof( $cart->get_applied_coupons() ) > 0 ){
//$cart->fees_api()->set_fees();
WC()->cart->remove_coupons();
wc_clear_notices();
wc_print_notice( '<strong>Il codice promozionale non è valido dal primo ordine</strong>', 'error' );
}
}
}
///////////////////
/*
* Add notice in cart and checkout
*/
add_action( 'woocommerce_before_checkout_form', 'notice_cart_checkout', 1 );
add_action( 'woocommerce_before_cart_contents', 'notice_cart_checkout', 1 );
function notice_cart_checkout() {
if ( is_admin() && ! defined( 'DOING_AJAX' ) || ! is_user_logged_in() )
return;
// Getting "completed" customer orders
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => 'shop_order', // WC orders post type
'post_status' => 'wc-completed' // Only orders with status "completed"
) );
// Orders count
$customer_orders_count = count($customer_orders);
if( empty($customer_orders) || $customer_orders_count == 0){
$notice = '<strong>Congratulazioni! Hai diritto ad uno sconto del 10% sul primo ordine.</strong> <span style="font-size:35px;">🎉🎊</span>';
wc_print_notice( $notice, 'success' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment