WooCommerceNameYourPrice.php
4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* Class WooCommerceNameYourPrice
*
* @package WCPay\MultiCurrency\Compatibility
*/
namespace WCPay\MultiCurrency\Compatibility;
use WCPay\MultiCurrency\MultiCurrency;
/**
* Class that controls Multi Currency Compatibility with WooCommerce Name Your Price Plugin.
*/
class WooCommerceNameYourPrice extends BaseCompatibility {
const NYP_CURRENCY = '_wcpay_multi_currency_nyp_currency';
/**
* Init the class.
*
* @return void
*/
protected function init() {
// Add needed actions and filters if Name Your Price is active.
if ( class_exists( 'WC_Name_Your_Price' ) ) {
// Convert meta prices.
add_filter( 'wc_nyp_raw_minimum_price', [ $this, 'get_nyp_prices' ] );
add_filter( 'wc_nyp_raw_maximum_price', [ $this, 'get_nyp_prices' ] );
add_filter( 'wc_nyp_raw_suggested_price', [ $this, 'get_nyp_prices' ] );
// Maybe translate cart prices.
add_action( 'woocommerce_add_cart_item_data', [ $this, 'add_initial_currency' ], 20, 3 );
add_filter( 'woocommerce_get_cart_item_from_session', [ $this, 'convert_cart_currency' ], 20, 2 );
add_filter( MultiCurrency::FILTER_PREFIX . 'should_convert_product_price', [ $this, 'should_convert_product_price' ], 50, 2 );
}
}
/**
* Converts the min/max/suggested prices of Name Your Price extension.
*
* @param mixed $price The price to be filtered.
* @return mixed The price as a string or float.
*/
public function get_nyp_prices( $price ) {
return ! $price ? $price : $this->multi_currency->get_price( $price, 'product' );
}
/**
* Store the inintial currency when item is added.
*
* @param array $cart_item Extra cart item data being passed to the cart item.
* @param int $product_id The id of the product being added to the cart.
* @param int $variation_id The id of the variation being added to the cart.
*
* @return array
*/
public function add_initial_currency( $cart_item, $product_id, $variation_id ) {
$nyp_id = $variation_id ? $variation_id : $product_id;
if ( \WC_Name_Your_Price_Helpers::is_nyp( $nyp_id ) && isset( $cart_item['nyp'] ) ) {
$currency = $this->multi_currency->get_selected_currency();
$cart_item['nyp_currency'] = $currency->get_code();
$cart_item['nyp_original'] = $cart_item['nyp'];
}
return $cart_item;
}
/**
* Switch the cart price when currency changes.
* Do not convert price if in the same currency as when added to the cart.
* This prevevents USD > EUR > USD style conversions and potential rounding problems.
*
* @param array $cart_item Cart item array.
* @param array $values Cart item values e.g. quantity and product_id.
*
* @return array
*/
public function convert_cart_currency( $cart_item, $values ) {
if ( isset( $cart_item['nyp_original'] ) && isset( $cart_item['nyp_currency'] ) ) {
// Store the original currency in $product meta.
$cart_item['data']->update_meta_data( self::NYP_CURRENCY, $cart_item['nyp_currency'] );
$selected_currency = $this->multi_currency->get_selected_currency();
// If the currency is currently the same as at time price entered, restore NYP to original value.
if ( $cart_item['nyp_currency'] === $selected_currency->get_code() ) {
$cart_item['nyp'] = $cart_item['nyp_original'];
} else {
$nyp_currency = $this->multi_currency->get_enabled_currencies()[ $cart_item['nyp_currency'] ] ?? null;
// Convert entered price back to default currency.
$converted_price = ( (float) $cart_item['nyp_original'] ) / $nyp_currency->get_rate();
if ( ! $selected_currency->get_is_default() ) {
$converted_price = $this->multi_currency->get_price( $converted_price, 'product' );
}
$cart_item['nyp'] = $converted_price;
}
$cart_item = WC_Name_Your_Price()->cart->set_cart_item( $cart_item );
}
return $cart_item;
}
/**
* Checks to see if the product's price should be converted.
*
* @param bool $return Whether to convert the product's price or not. Default is true.
* @param object $product Product object to test.
*
* @return bool True if it should be converted.
*/
public function should_convert_product_price( bool $return, $product ): bool {
// If it's already false, return it.
if ( ! $return ) {
return $return;
}
$currency = $this->multi_currency->get_selected_currency();
// Check for cart items to see if they are in the original currency.
if ( $currency->get_code() === $product->get_meta( self::NYP_CURRENCY ) ) {
$return = false;
}
// Check to see if the product is a NYP product.
if ( \WC_Name_Your_Price_Helpers::is_nyp( $product ) ) {
return false;
}
return $return;
}
}