milenbogoev

Flycart WCML price function

Oct 31st, 2024
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.72 KB | None | 0 0
  1. // Step 1: Global Price Adjustment for RON (+10%)
  2. function adjust_prices_for_ron_init() {
  3. if ( class_exists( 'WooCommerce' ) && isset($GLOBALS['woocommerce_wpml']) ) {
  4. // Adjust product prices globally, excluding cart and checkout for FlyCart products
  5. add_filter( 'woocommerce_product_get_price', 'adjust_product_price_for_ron', 10, 2 );
  6. add_filter( 'woocommerce_product_get_sale_price', 'adjust_product_price_for_ron', 10, 2 );
  7. add_filter( 'woocommerce_product_get_regular_price', 'adjust_product_price_for_ron', 10, 2 );
  8. add_filter( 'woocommerce_product_variation_get_price', 'adjust_product_price_for_ron', 10, 2 );
  9. add_filter( 'woocommerce_product_variation_get_regular_price', 'adjust_product_price_for_ron', 10, 2 );
  10. add_filter( 'woocommerce_product_variation_get_sale_price', 'adjust_product_price_for_ron', 10, 2 );
  11. }
  12. }
  13.  
  14. function adjust_product_price_for_ron( $price, $product ) {
  15. global $woocommerce_wpml;
  16.  
  17. if ( isset( $woocommerce_wpml ) ) {
  18. $current_currency = $woocommerce_wpml->multi_currency->get_client_currency();
  19.  
  20. // Ensure the price is adjusted only if it's not a FlyCart-discounted product in cart or checkout
  21. if ( $current_currency === 'RON' ) {
  22. // If we are in cart or checkout, avoid double adjustments for FlyCart products
  23. if ( is_cart() || is_checkout() ) {
  24. if ( ! is_flycart_discount_applied($product) && is_numeric($price) ) {
  25. $price = $price * 1.10; // Apply +10% only if FlyCart discount is not applied
  26. }
  27. } else {
  28. // For other pages, apply +10% to all products in RON
  29. if ( is_numeric($price) ) {
  30. $price = $price * 1.10;
  31. }
  32. }
  33. }
  34. }
  35.  
  36. return $price;
  37. }
  38.  
  39. add_action( 'init', 'adjust_prices_for_ron_init' );
  40.  
  41. // Helper Function: Check if a FlyCart discount is applied
  42. function is_flycart_discount_applied( $product ) {
  43. if ( ! $product instanceof WC_Product ) {
  44. return false;
  45. }
  46.  
  47. // Use FlyCart logic to determine if a discount is applied
  48. if ( function_exists( 'AWDR' ) ) {
  49. $discounted_price = AWDR()->pricing_rules->get_product_discount_price( $product, $product->get_price('edit') );
  50.  
  51. // If the discounted price is less than the original price, a discount is applied
  52. if ( is_numeric( $discounted_price ) && $discounted_price < $product->get_price('edit') ) {
  53. return true;
  54. }
  55. }
  56.  
  57. return false;
  58. }
  59.  
  60. // Step 2: Adjust FlyCart Products in Cart and Checkout to Avoid Double Adjustment
  61. add_action( 'woocommerce_before_calculate_totals', 'adjust_flycart_products_in_cart', 20 );
  62.  
  63. function adjust_flycart_products_in_cart( $cart ) {
  64. global $woocommerce_wpml;
  65.  
  66. // Prevent running this action multiple times
  67. if ( did_action('woocommerce_before_calculate_totals') >= 2 ) {
  68. return;
  69. }
  70.  
  71. if ( isset( $woocommerce_wpml ) ) {
  72. $current_currency = $woocommerce_wpml->multi_currency->get_client_currency();
  73.  
  74. if ( $current_currency === 'RON' && is_object($cart) ) {
  75. foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
  76. $product = $cart_item['data'];
  77.  
  78. if ( ! $product instanceof WC_Product ) {
  79. continue;
  80. }
  81.  
  82. // Check if the product has a FlyCart discount applied
  83. if ( isset( $cart_item['wo_discount_applied'] ) || isset( $cart_item['discounts'] ) ) {
  84. $original_price = $product->get_meta('_original_price', true);
  85.  
  86. if ( empty($original_price) ) {
  87. // Save the original price if not already stored
  88. $original_price = $product->get_price('edit');
  89. $product->update_meta_data('_original_price', $original_price);
  90. }
  91.  
  92. // Get FlyCart discounted price and set it to the product
  93. if ( function_exists( 'AWDR' ) ) {
  94. $flycart_discounted_price = AWDR()->pricing_rules->get_product_discount_price( $product, $original_price );
  95.  
  96. if ( is_numeric( $flycart_discounted_price ) && $flycart_discounted_price > 0 ) {
  97. $product->set_price( $flycart_discounted_price );
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105.  
  106. // Step 3: Subtotal Correction for Cart and Checkout Pages
  107. add_action('woocommerce_cart_totals_before_order_total', 'adjust_cart_subtotal_for_flycart_discounts', 10);
  108. add_action('woocommerce_review_order_before_order_total', 'adjust_cart_subtotal_for_flycart_discounts', 10);
  109.  
  110. function adjust_cart_subtotal_for_flycart_discounts() {
  111. if ( WC()->cart && isset( WC()->cart ) ) {
  112. $cart = WC()->cart;
  113. global $woocommerce_wpml;
  114.  
  115. if (isset($woocommerce_wpml)) {
  116. $current_currency = $woocommerce_wpml->multi_currency->get_client_currency();
  117.  
  118. if ($current_currency === 'RON') {
  119. $total_discount = 0;
  120.  
  121. foreach ($cart->get_cart() as $cart_item_key => $cart_item ) {
  122. $product = $cart_item['data'];
  123.  
  124. // If FlyCart discount is applied, adjust subtotal accordingly
  125. if (isset($cart_item['wo_discount_applied']) || isset($cart_item['discounts'])) {
  126. $original_price = $product->get_meta('_original_price', true);
  127. $current_price = $product->get_price('edit');
  128.  
  129. if ($original_price && is_numeric($original_price) && $current_price < $original_price) {
  130. $discount = ($original_price - $current_price) * $cart_item['quantity'];
  131. $total_discount += $discount;
  132. }
  133. }
  134. }
  135.  
  136. if ($total_discount > 0) {
  137. WC()->cart->add_fee(__('Adjusted Discount', 'woocommerce'), -$total_discount, true);
  138. }
  139. }
  140. }
  141. }
  142. }
  143.  
  144. // Step 4: Display Correct Price Format for On-Sale Products (Regular Price Strikethrough)
  145. add_filter( 'woocommerce_get_price_html', 'adjust_display_price_html_for_on_sale', 10, 2 );
  146.  
  147. function adjust_display_price_html_for_on_sale( $price_html, $product ) {
  148. if ( $product->is_on_sale() ) {
  149. $regular_price = wc_price( $product->get_regular_price() );
  150. $sale_price = wc_price( $product->get_sale_price() );
  151. $price_html = '<del>' . $regular_price . '</del> <ins>' . $sale_price . '</ins>';
  152. }
  153. return $price_html;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment