Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Step 1: Global Price Adjustment for RON (+10%)
- function adjust_prices_for_ron_init() {
- if ( class_exists( 'WooCommerce' ) && isset($GLOBALS['woocommerce_wpml']) ) {
- // Adjust product prices globally, excluding cart and checkout for FlyCart products
- add_filter( 'woocommerce_product_get_price', 'adjust_product_price_for_ron', 10, 2 );
- add_filter( 'woocommerce_product_get_sale_price', 'adjust_product_price_for_ron', 10, 2 );
- add_filter( 'woocommerce_product_get_regular_price', 'adjust_product_price_for_ron', 10, 2 );
- add_filter( 'woocommerce_product_variation_get_price', 'adjust_product_price_for_ron', 10, 2 );
- add_filter( 'woocommerce_product_variation_get_regular_price', 'adjust_product_price_for_ron', 10, 2 );
- add_filter( 'woocommerce_product_variation_get_sale_price', 'adjust_product_price_for_ron', 10, 2 );
- }
- }
- function adjust_product_price_for_ron( $price, $product ) {
- global $woocommerce_wpml;
- if ( isset( $woocommerce_wpml ) ) {
- $current_currency = $woocommerce_wpml->multi_currency->get_client_currency();
- // Ensure the price is adjusted only if it's not a FlyCart-discounted product in cart or checkout
- if ( $current_currency === 'RON' ) {
- // If we are in cart or checkout, avoid double adjustments for FlyCart products
- if ( is_cart() || is_checkout() ) {
- if ( ! is_flycart_discount_applied($product) && is_numeric($price) ) {
- $price = $price * 1.10; // Apply +10% only if FlyCart discount is not applied
- }
- } else {
- // For other pages, apply +10% to all products in RON
- if ( is_numeric($price) ) {
- $price = $price * 1.10;
- }
- }
- }
- }
- return $price;
- }
- add_action( 'init', 'adjust_prices_for_ron_init' );
- // Helper Function: Check if a FlyCart discount is applied
- function is_flycart_discount_applied( $product ) {
- if ( ! $product instanceof WC_Product ) {
- return false;
- }
- // Use FlyCart logic to determine if a discount is applied
- if ( function_exists( 'AWDR' ) ) {
- $discounted_price = AWDR()->pricing_rules->get_product_discount_price( $product, $product->get_price('edit') );
- // If the discounted price is less than the original price, a discount is applied
- if ( is_numeric( $discounted_price ) && $discounted_price < $product->get_price('edit') ) {
- return true;
- }
- }
- return false;
- }
- // Step 2: Adjust FlyCart Products in Cart and Checkout to Avoid Double Adjustment
- add_action( 'woocommerce_before_calculate_totals', 'adjust_flycart_products_in_cart', 20 );
- function adjust_flycart_products_in_cart( $cart ) {
- global $woocommerce_wpml;
- // Prevent running this action multiple times
- if ( did_action('woocommerce_before_calculate_totals') >= 2 ) {
- return;
- }
- if ( isset( $woocommerce_wpml ) ) {
- $current_currency = $woocommerce_wpml->multi_currency->get_client_currency();
- if ( $current_currency === 'RON' && is_object($cart) ) {
- foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
- $product = $cart_item['data'];
- if ( ! $product instanceof WC_Product ) {
- continue;
- }
- // Check if the product has a FlyCart discount applied
- if ( isset( $cart_item['wo_discount_applied'] ) || isset( $cart_item['discounts'] ) ) {
- $original_price = $product->get_meta('_original_price', true);
- if ( empty($original_price) ) {
- // Save the original price if not already stored
- $original_price = $product->get_price('edit');
- $product->update_meta_data('_original_price', $original_price);
- }
- // Get FlyCart discounted price and set it to the product
- if ( function_exists( 'AWDR' ) ) {
- $flycart_discounted_price = AWDR()->pricing_rules->get_product_discount_price( $product, $original_price );
- if ( is_numeric( $flycart_discounted_price ) && $flycart_discounted_price > 0 ) {
- $product->set_price( $flycart_discounted_price );
- }
- }
- }
- }
- }
- }
- }
- // Step 3: Subtotal Correction for Cart and Checkout Pages
- add_action('woocommerce_cart_totals_before_order_total', 'adjust_cart_subtotal_for_flycart_discounts', 10);
- add_action('woocommerce_review_order_before_order_total', 'adjust_cart_subtotal_for_flycart_discounts', 10);
- function adjust_cart_subtotal_for_flycart_discounts() {
- if ( WC()->cart && isset( WC()->cart ) ) {
- $cart = WC()->cart;
- global $woocommerce_wpml;
- if (isset($woocommerce_wpml)) {
- $current_currency = $woocommerce_wpml->multi_currency->get_client_currency();
- if ($current_currency === 'RON') {
- $total_discount = 0;
- foreach ($cart->get_cart() as $cart_item_key => $cart_item ) {
- $product = $cart_item['data'];
- // If FlyCart discount is applied, adjust subtotal accordingly
- if (isset($cart_item['wo_discount_applied']) || isset($cart_item['discounts'])) {
- $original_price = $product->get_meta('_original_price', true);
- $current_price = $product->get_price('edit');
- if ($original_price && is_numeric($original_price) && $current_price < $original_price) {
- $discount = ($original_price - $current_price) * $cart_item['quantity'];
- $total_discount += $discount;
- }
- }
- }
- if ($total_discount > 0) {
- WC()->cart->add_fee(__('Adjusted Discount', 'woocommerce'), -$total_discount, true);
- }
- }
- }
- }
- }
- // Step 4: Display Correct Price Format for On-Sale Products (Regular Price Strikethrough)
- add_filter( 'woocommerce_get_price_html', 'adjust_display_price_html_for_on_sale', 10, 2 );
- function adjust_display_price_html_for_on_sale( $price_html, $product ) {
- if ( $product->is_on_sale() ) {
- $regular_price = wc_price( $product->get_regular_price() );
- $sale_price = wc_price( $product->get_sale_price() );
- $price_html = '<del>' . $regular_price . '</del> <ins>' . $sale_price . '</ins>';
- }
- return $price_html;
- }
Advertisement
Add Comment
Please, Sign In to add comment