WCML_Multi_Currency_Shipping

The WooCommerce Multilingual WCML Multi Currency Shipping class.

Defined (1)

The class is defined in the following location(s).

/inc/currencies/class-wcml-multi-currency-shipping.php  
  1. class WCML_Multi_Currency_Shipping{ 
  2.  
  3. /** 
  4. * @var WCML_Multi_Currency 
  5. */ 
  6. private $multi_currency; 
  7.  
  8. public function __construct( &$multi_currency ) { 
  9. global $wpdb; 
  10.  
  11. $this->multi_currency =& $multi_currency; 
  12.  
  13. // shipping method cost settings 
  14. if( version_compare( WC()->version, '2.6.0', '>=' ) ) { 
  15. $rates = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE method_id IN('flat_rate', 'local_pickup', 'free_shipping')" ); 
  16. foreach ( $rates as $method ) { 
  17. $option_name = sprintf( 'woocommerce_%s_%d_settings', $method->method_id, $method->instance_id ); 
  18. add_filter( 'option_' . $option_name, array($this, 'convert_shipping_method_cost_settings') ); 
  19.  
  20. // Used for table rate shipping compatibility class 
  21. add_filter( 'wcml_shipping_price_amount', array( $this, 'shipping_price_filter' ) ); // WCML filters 
  22. add_filter( 'wcml_shipping_free_min_amount', array( $this, 'shipping_free_min_amount') ); // WCML filters 
  23.  
  24. add_filter( 'woocommerce_evaluate_shipping_cost_args', array( $this, 'woocommerce_evaluate_shipping_cost_args') ); 
  25.  
  26. add_filter( 'woocommerce_shipping_packages', array( $this, 'convert_shipping_taxes'), 10 ); 
  27.  
  28. add_filter( 'woocommerce_package_rates', array($this, 'convert_shipping_costs_in_package_rates'), 10, 2 ); 
  29.  
  30. // Before WooCommerce 2.6 
  31. if( version_compare( WC()->version, '2.6.0', '<' ) ) { 
  32. new WCML_Multi_Currency_Shipping_Legacy(); 
  33.  
  34.  
  35.  
  36. public function convert_shipping_costs_in_package_rates( $rates, $package ) { 
  37.  
  38. $client_currency = $this->multi_currency->get_client_currency(); 
  39. foreach( $rates as $rate_id => $rate ) { 
  40. if( isset( $rate->cost ) && $rate->cost ) { 
  41. $rate->cost = $this->multi_currency->prices->raw_price_filter( $rate->cost, $client_currency); 
  42.  
  43. return $rates; 
  44.  
  45. public function convert_shipping_method_cost_settings( $settings ) { 
  46.  
  47. $has_free_shipping_coupon = false; 
  48. if ( $coupons = WC()->cart->get_coupons() ) { 
  49. foreach ( $coupons as $code => $coupon ) { 
  50.  
  51. if ( 
  52. $coupon->is_valid() && 
  53. //backward compatibility for WC < 2.7 
  54. method_exists( $coupon, 'get_free_shipping' ) ? 
  55. $coupon->get_free_shipping() : 
  56. $coupon->enable_free_shipping() 
  57. ) { 
  58. $has_free_shipping_coupon = true; 
  59.  
  60. if( !empty( $settings['requires'] ) ) { 
  61.  
  62. if( 
  63. $settings['requires'] == 'min_amount' || 
  64. $settings['requires'] == 'either' || 
  65. $settings['requires'] == 'both' && $has_free_shipping_coupon 
  66. ) { 
  67. $settings['min_amount'] = apply_filters( 'wcml_shipping_free_min_amount', $settings['min_amount'] ); 
  68.  
  69. return $settings; 
  70.  
  71. /** 
  72. * @param $args 
  73. * @param $sum 
  74. * @param $method 
  75. * @return array 
  76. * When using [cost] in the shipping class costs, we need to use the not-converted cart total 
  77. * It will be converted as part of the total cost 
  78. */ 
  79. public function woocommerce_evaluate_shipping_cost_args( $args ) { 
  80.  
  81. $args['cost'] = $this->multi_currency->prices->unconvert_price_amount( $args['cost'] ); 
  82.  
  83. return $args; 
  84.  
  85. public function convert_shipping_taxes( $packages ) { 
  86.  
  87. foreach( $packages as $package_id => $package ) { 
  88. if( isset( $package['rates'] ) ) { 
  89. foreach( $package['rates'] as $rate_id => $rate ) { 
  90. $packages[$package_id]['rates'][$rate_id]->taxes = 
  91. WC_Tax::calc_shipping_tax( $packages[$package_id]['rates'][$rate_id]->cost, WC_Tax::get_shipping_tax_rates() ); 
  92.  
  93. return $packages; 
  94.  
  95. public function shipping_price_filter($price) { 
  96.  
  97. $price = $this->multi_currency->prices->raw_price_filter($price, $this->multi_currency->get_client_currency()); 
  98.  
  99. return $price; 
  100.  
  101.  
  102. public function shipping_free_min_amount($price) { 
  103.  
  104. $price = $this->multi_currency->prices->raw_price_filter( $price, $this->multi_currency->get_client_currency() ); 
  105.  
  106. return $price; 
  107.  
  108.  
  109.