Skip to content

Instantly share code, notes, and snippets.

View AnanthFlycart's full-sized avatar

Anantharaj B AnanthFlycart

View GitHub Profile
// Show You Saved (Price) in Cart Totals and Order Review Row
if (!function_exists('woo_discount_rules_show_you_saved_cart_totals_row')) {
add_action('woocommerce_cart_totals_after_order_total', 'woo_discount_rules_show_you_saved_cart_totals_row', 9999);
add_action('woocommerce_review_order_after_order_total', 'woo_discount_rules_show_you_saved_cart_totals_row', 9999);
function woo_discount_rules_show_you_saved_cart_totals_row() {
$discount_total = 0;
// Discount total label in cart and checkout page
$display_text = __('You Saved', 'woo-discount-rules');
// Round discount value for both Price and Cart Rules (Ex: 9.99 round down to 9.95 and 9.94 round down to 9.90)
if (!function_exists('advanced_woo_discount_rules_round_discount_price')) {
function advanced_woo_discount_rules_round_discount_price($price) {
$rounded_price = wc_round_discount($price, 1);
$diff = wc_round_discount($price - $rounded_price, 2);
if ($diff == 0.05) {
return $rounded_price + 0.05;
} else if ($diff >= 0) {
return $rounded_price;
} else {
// Return filter passed true, only if all the attribute filters are passed
add_filter('advanced_woo_discount_rules_filter_passed', function ($filter_passed, $rule, $product, $sale_badge, $product_table, $conditionFailed) {
if (method_exists($rule, 'getFilter')) {
$filters = $rule->getFilter();
if ($filter_passed && !empty($filters) && class_exists('\Wdr\App\Helpers\Filter')) {
$filter_helper = new \Wdr\App\Helpers\Filter();
foreach ($filters as $filter) {
if ($filter->type == 'product_attributes') {
$filter_piece = (object) array($filter);
$attribute_filter_passed = $filter_helper->matchFilters($product, $filter_piece, $sale_badge, $product_table);
// PPOM Product Addon plugin compatibility (apply discount only on base price of product)
if (!function_exists('advanced_woo_discount_rules_get_ppom_total_addon_price')) {
function advanced_woo_discount_rules_get_ppom_total_addon_price($cart_item) {
if (isset($cart_item['data']) && isset($cart_item['ppom']['fields']) && function_exists('ppom_get_field_prices') && function_exists('ppom_price_get_addon_total')) {
$product_id = $cart_item['data']->get_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, $cart_item);
$total_addon_price = ppom_price_get_addon_total($ppom_field_prices);
// Merge all the Discount Bars
add_filter('advanced_woo_discount_rules_advance_table_based_on_rule', function($advanced_layout, $rule, $discount_calculator, $product, $product_price, $html_content) {
if (is_array($advanced_layout) && isset($advanced_layout[0]['badge_bg_color']) && !empty(isset($advanced_layout[0]['badge_bg_color'])) && method_exists($rule, 'getId')) {
$advanced_layout[0]['rule_id'] = $rule->getId();
}
return $advanced_layout;
}, 10, 6);
// Display Sale Badge using alternative Events (useful while sale flash event missing in theme)
if (!function_exists('advanced_woo_discount_rules_display_sale_badge_using_alternative_events')) {
function advanced_woo_discount_rules_display_sale_badge_using_alternative_events() {
global $post, $product;
if (!empty($post) && !empty($product) && $product->is_on_sale()) {
echo apply_filters('woocommerce_sale_flash', '<span class="onsale">' . esc_html__('Sale!', 'woocommerce') . '</span>', $post, $product);
}
}
add_action('woocommerce_before_shop_loop_item_title', function() {
advanced_woo_discount_rules_display_sale_badge_using_alternative_events();
// Return filter passed true, only if all the category and tags filters are passed
add_filter('advanced_woo_discount_rules_filter_passed', function ($filter_passed, $rule, $product, $sale_badge, $product_table, $conditionFailed) {
if (method_exists($rule, 'getFilter')) {
$filters = $rule->getFilter();
if ($filter_passed && !empty($filters) && class_exists('\Wdr\App\Helpers\Filter')) {
$filter_helper = new \Wdr\App\Helpers\Filter();
foreach ($filters as $filter) {
if (in_array($filter->type, array('product_category', 'product_tags'))) {
$filter_piece = (object) array($filter);
$custom_filter_passed = $filter_helper->matchFilters($product, $filter_piece, $sale_badge, $product_table);
// Customize You saved message html
add_filter('advanced_woo_discount_rules_line_item_subtotal_saved_text', function($subtotal_additional_text, $total_discount, $discount, $cart_item, $cart_item_key) {
$message = __("You saved ", 'woo-discount-rules') . $total_discount; // here you can customize you saved message
return '<br><div class="awdr-you-saved-text" style="color: green">' . $message . '</div>'; // here you can customize you saved message html block
}, 10, 5);
// To suppress third party plugin cart line item price strikeout html events
add_action('plugins_loaded', function() {
if(class_exists('\Wdr\App\Router')){
remove_all_filters('woocommerce_cart_item_price');
add_filter('woocommerce_cart_item_price', array(\Wdr\App\Router::$manage_discount, 'getCartPriceHtml'), 1000, 3);
}
});
// Calculate Subtotal based on Line Item Subtotal (helpful while using inclusive tax)
add_filter('advanced_woo_discount_rules_calculate_cart_subtotal_manually', '__return_true');