Skip to content

Instantly share code, notes, and snippets.

View AnanthFlycart's full-sized avatar

Anantharaj B AnanthFlycart

View GitHub Profile
@AnanthFlycart
AnanthFlycart / Checkout Upsell: Exclude main product from discounts (form Woo Discount Rules v2)
Created December 6, 2023 06:32
CUW: WDR compatibility - Exclude main product from discounts
add_filter('advanced_woo_discount_rules_calculate_discount_for_cart_item', function ($calculate_discount, $cart_item) {
if ($calculate_discount && function_exists('WC') && isset(WC()->cart) && method_exists(WC()->cart, 'get_cart')) {
foreach (WC()->cart->get_cart() as $item) {
if (!empty($item['cuw_product']['main_item_key']) && $cart_item['key'] == $item['cuw_product']['main_item_key']) {
return false;
}
}
}
return $calculate_discount;
}, 200, 2);
add_action('wp_ajax_cuw_get_cart_subtotal', 'cuw_get_cart_subtotal');
add_action('wp_ajax_nopriv_cuw_get_cart_subtotal', 'cuw_get_cart_subtotal');
if (!function_exists('cuw_get_cart_subtotal')) {
function cuw_get_cart_subtotal() {
$subtotal = 0;
if (function_exists('WC') && isset(WC()->cart) && function_exists('wc_price')) {
WC()->cart->calculate_totals();
$subtotal = WC()->cart->get_subtotal();
if (get_option('woocommerce_tax_display_shop') === 'incl') {
add_filter('rightpress_product_price_changes_price_to_set', function($price, $cart_item_key, $price_changes, $cart_items) {
if (isset($cart_items[$cart_item_key]['cuw_offer']['price'])) {
return (float)$cart_items[$cart_item_key]['cuw_offer']['price'];
} elseif(isset($cart_items[$cart_item_key]['cuw_product']['price'])) {
return (float)$cart_items[$cart_item_key]['cuw_product']['price'];
}
return $price;
}, 100, 4);
add_action('wp_footer', function () { ?>
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery(document).on("change", ".cuw-modal .cuw-product-row .variant-select", function () {
cuw_actions.update_product_image(jQuery(this).closest('.cuw-product-row'), jQuery(this).find("option:selected").val());
});
});
</script>
<?php
});
add_action('advanced_woo_discount_rules_loaded', function() {
if (class_exists('\WDRPro\App\Rules\FreeShipping')) {
remove_action('woocommerce_shipping_init', array('\WDRPro\App\Rules\FreeShipping', 'mayHaveFreeShipping'));
remove_filter('woocommerce_shipping_methods', array('\WDRPro\App\Rules\FreeShipping', 'registerFreeShippingMethod'));
add_filter('woocommerce_shipping_wdr_free_shipping_is_available', '__return_false', PHP_INT_MAX);
}
}, 100);
add_action('wp_footer', function() { ?>
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery(".cuw-fbt-products .cuw-product .cuw-product-checkbox").prop('checked', false);
});
</script>
<?php
});
add_filter('advanced_woo_discount_rules_is_conditions_passed', function ($status) {
if ($status && function_exists('WC') && isset(WC()->cart) && method_exists(WC()->cart, 'get_cart')) {
foreach (WC()->cart->get_cart() as $cart_item) {
if (isset($cart_item['cuw_offer']['discount']['type']) && $cart_item['cuw_offer']['discount']['type'] != 'no_discount') {
return false;
} elseif (isset($cart_item['cuw_product']['discount']['type']) && $cart_item['cuw_product']['discount']['type'] != 'no_discount') {
return false;
}
}
}
add_filter('cuw_noc_action_display_locations', function($locations) {
$locations['cuw_noc_shortcode'] = esc_html__("Use a shortcode [cuw_noc]", 'checkout-upsell-woocommerce');
return $locations;
});
add_shortcode('cuw_noc', function() {
ob_start();
if (!empty($_GET['key']) && function_exists('wc_get_order_id_by_order_key')) {
$order_id = wc_get_order_id_by_order_key(sanitize_key($_GET['key']));
$order = wc_get_order($order_id);
if (!empty($order) && method_exists('CUW\App\Modules\Campaigns\NOC', 'getActionsHtml')) {
add_filter('cuw_thankyou_upsell_products_display_locations', function($locations) {
$locations['cuw_thankyou_upsells_shortcode'] = esc_html__("Use a shortcode [cuw_thankyou_upsells]", 'checkout-upsell-woocommerce');
return $locations;
});
add_shortcode('cuw_thankyou_upsells', function() {
ob_start();
if (!empty($_GET['key']) && function_exists('wc_get_order_id_by_order_key')) {
$order_id = wc_get_order_id_by_order_key(sanitize_key($_GET['key']));
$order = wc_get_order($order_id);
if (!empty($order) && method_exists('CUW\App\Pro\Modules\Campaigns\ThankyouUpsells', 'getProductsHtml')) {
// Tested in PPOM for WooCommerce plugin version 32.0
if (!function_exists('advanced_woo_discount_rules_get_ppom_new_total_addon_price')) {
function advanced_woo_discount_rules_get_ppom_new_total_addon_price($cart_item) {
if (isset($cart_item['ppom']['fields']) && function_exists('ppom_get_field_prices') && function_exists('ppom_price_get_addon_total')) {
$product_id = isset( $cart_item['product_id'] ) ? $cart_item['product_id'] : '';
$variation_id = isset( $cart_item['variation_id'] ) ? $cart_item['variation_id'] : '';
$ppom_fields_post = $cart_item['ppom']['fields'];
$product_quantity = floatval( $cart_item['quantity'] );
$ppom_field_prices = ppom_get_field_prices( $ppom_fields_post, $product_id, $product_quantity, $variation_id );
return ppom_price_get_addon_total( $ppom_field_prices );