WooCommerce Product Add Ons, by WooCommerce - Apply discount for only base price not for addons
Apply discounts only to base price and not addons in WooCommerce Product Add Ons by WooCommerce.
global $awdr_addon_commpatible_products;
add_filter('advanced_woo_discount_rules_product_price_on_before_calculate_discount', function($product_price, $product, $quantity, $cart_item){
global $awdr_addon_commpatible_products;
if(!empty($cart_item)){
if(!empty($cart_item['addons'])){
if (method_exists($product, 'get_id') && function_exists('wc_get_product')) {
$product_id = $product->get_id();
if(isset($awdr_addon_commpatible_products[$product_id])){
$product = $awdr_addon_commpatible_products[$product_id];
} else {
$awdr_addon_commpatible_products[$product_id] = $product = wc_get_product($product_id);
}
if (method_exists($product, 'get_price')) {
$product_price = $product->get_price();
}
}
}
}
return $product_price;
}, 10, 4);
add_action( 'wp_head', function () { ?>
<script type="application/javascript">
(function ($) {
$(document).ready(function ($) {
setTimeout(function () {
var $form = $('form.cart').first();
$('#product-addons-total').each(function(){
var $targets = $(this);
$lock = $targets.attr('data-lock');
if($lock === undefined || $lock === null){
$lock = false;
}
if($lock == false){
var price = newText = $targets.attr('data-price');
var option = {
custom_price: price,
original_price: price
};
$targets.attr('data-lock', true);
$.AdvanceWooDiscountRules.getDynamicDiscountPriceFromCartForm($form, $targets, option);
}
});
}, 0);
$( ".single_variation_wrap" ).on( "show_variation", function ( event, variation, purchasable ) {
var $form = $('form.cart').first();
$('#product-addons-total').each(function(){
var $targets = $(this);
$lock = $targets.attr('data-lock');
if($lock === undefined || $lock === null){
$lock = false;
}
if($lock == false || $lock == 'false'){
var price = newText = variation.display_price //$targets.attr('data-price');
var option = {
custom_price: price,
original_price: price
};
$targets.attr('data-lock', true);
$.AdvanceWooDiscountRules.getDynamicDiscountPriceFromCartForm($form, $targets, option);
}
});
});
$(document.body).on( "advanced_woo_discount_rules_on_get_response_for_dynamic_discount", function ( e, response, target, options ) {
target.attr('data-lock', false);
if(response.success == true){
if(response.data !== undefined && response.data !== false){
target.attr('data-price', response.data.discounted_price);
target.attr('data-raw-price', response.data.discounted_price);
target[0].outerHTML = target[0].outerHTML;
var $form = jQuery('form.cart').first();
$form.find('input.qty').trigger('change');
}
}
});
});
})(jQuery);
</script>
<?php } );