MulticurrencyHooks.php
98 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\Compatibility\WcNameYourPrice; |
| 4 | |
| 5 | use WC_Name_Your_Price_Compatibility; |
| 6 | |
| 7 | use function WPML\Container\make; |
| 8 | use function WCML\functions\getClientCurrency; |
| 9 | |
| 10 | class MulticurrencyHooks implements \IWPML_Action { |
| 11 | |
| 12 | public function add_hooks() { |
| 13 | if ( ! is_admin() ) { |
| 14 | if ( is_callable( [ 'WC_Name_Your_Price_Compatibility', 'is_nyp_gte' ] ) && WC_Name_Your_Price_Compatibility::is_nyp_gte( '3.0' ) ) { |
| 15 | add_filter( 'wc_nyp_raw_suggested_price', [ $this, 'product_price_filter' ] ); |
| 16 | add_filter( 'wc_nyp_raw_minimum_price', [ $this, 'product_price_filter' ] ); |
| 17 | add_filter( 'wc_nyp_raw_maximum_price', [ $this, 'product_price_filter' ] ); |
| 18 | } else { |
| 19 | add_filter( 'woocommerce_raw_suggested_price', [ $this, 'product_price_filter' ] ); |
| 20 | add_filter( 'woocommerce_raw_minimum_price', [ $this, 'product_price_filter' ] ); |
| 21 | add_filter( 'woocommerce_raw_maximum_price', [ $this, 'product_price_filter' ] ); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | add_filter( 'woocommerce_add_cart_item_data', [ $this, 'add_initial_currency' ] ); |
| 26 | add_filter( 'woocommerce_get_cart_item_from_session', [ $this, 'filter_woocommerce_get_cart_item_from_session' ], 20, 2 ); |
| 27 | |
| 28 | add_filter( 'wc_nyp_edit_in_cart_args', [ $this, 'edit_in_cart_args' ], 10 ); |
| 29 | add_filter( 'wc_nyp_get_initial_price', [ $this, 'get_initial_price' ], 10, 3 ); |
| 30 | } |
| 31 | |
| 32 | public function product_price_filter( $price, $currency = false ) { |
| 33 | return apply_filters( 'wcml_raw_price_amount', $price, $currency ); |
| 34 | } |
| 35 | |
| 36 | public function add_initial_currency( $cart_item_data ) { |
| 37 | |
| 38 | if ( isset( $cart_item_data['nyp'] ) ) { |
| 39 | $cart_item_data['nyp_currency'] = get_woocommerce_currency(); |
| 40 | $cart_item_data['nyp_original'] = $cart_item_data['nyp']; |
| 41 | } |
| 42 | |
| 43 | return $cart_item_data; |
| 44 | } |
| 45 | |
| 46 | public function filter_woocommerce_get_cart_item_from_session( $session_data, $values ) { |
| 47 | |
| 48 | if ( isset( $values['nyp_currency'] ) ) { |
| 49 | $session_data['nyp_currency'] = $values['nyp_currency']; |
| 50 | } |
| 51 | |
| 52 | if ( isset( $values['nyp_original'] ) ) { |
| 53 | $session_data['nyp_original'] = $values['nyp_original']; |
| 54 | } |
| 55 | |
| 56 | $current_currency = getClientCurrency(); |
| 57 | |
| 58 | if ( isset( $session_data['nyp_currency'] ) && $session_data['nyp_currency'] !== $current_currency ) { |
| 59 | |
| 60 | $product = $session_data['data']; |
| 61 | |
| 62 | $price_in_current_currency = $this->product_price_filter( $session_data['nyp'], $current_currency ); |
| 63 | |
| 64 | $product->set_price( $price_in_current_currency ); |
| 65 | $product->set_regular_price( $price_in_current_currency ); |
| 66 | $product->set_sale_price( $price_in_current_currency ); |
| 67 | |
| 68 | if ( $product->is_type( [ 'subscription', 'subscription_variation' ] ) ) { |
| 69 | $product->update_meta_data( '_subscription_price', $price_in_current_currency ); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | return $session_data; |
| 74 | } |
| 75 | |
| 76 | public function edit_in_cart_args( $args ) { |
| 77 | $args['nyp_currency'] = get_woocommerce_currency(); |
| 78 | return $args; |
| 79 | } |
| 80 | |
| 81 | public function get_initial_price( $initial_price, $product, $suffix ) { |
| 82 | |
| 83 | if ( isset( $_REQUEST[ 'nyp_raw' . $suffix ] ) && isset( $_REQUEST[ 'nyp_currency' ] ) ) { |
| 84 | $from_currency = wc_clean( $_REQUEST[ 'nyp_currency' ] ); |
| 85 | $current_currency = get_woocommerce_currency(); |
| 86 | if ( $from_currency !== $current_currency ) { |
| 87 | $raw_price = wc_clean( $_REQUEST[ 'nyp_raw' . $suffix ] ); |
| 88 | |
| 89 | $multi_currency = make( \WCML_Multi_Currency::class ); |
| 90 | $initial_price = $multi_currency->prices->convert_price_amount_by_currencies( $raw_price, $from_currency, $current_currency ); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return $initial_price; |
| 95 | } |
| 96 | |
| 97 | } |
| 98 |