AdminHooks.php
1 week ago
DefaultConversion.php
1 week ago
FlatRateShipping.php
5 years ago
FreeShipping.php
1 week ago
FrontEndHooks.php
1 week ago
LocalPickup.php
5 years ago
ShippingClasses.php
1 week ago
ShippingClassesMode.php
1 week ago
ShippingHooksFactory.php
1 week ago
ShippingMode.php
1 week ago
ShippingModeBase.php
1 week ago
ShippingModeProvider.php
1 week ago
UnsupportedShipping.php
1 week ago
VariableCost.php
1 week ago
FrontEndHooks.php
74 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\Multicurrency\Shipping; |
| 4 | |
| 5 | use IWPML_Action; |
| 6 | use WCML\StandAlone\IStandAloneAction; |
| 7 | |
| 8 | class FrontEndHooks implements IWPML_Action, IStandAloneAction { |
| 9 | |
| 10 | private $multiCurrency; |
| 11 | |
| 12 | public function __construct( $multiCurrency ) { |
| 13 | $this->multiCurrency = $multiCurrency; |
| 14 | } |
| 15 | |
| 16 | public function add_hooks() { |
| 17 | ShippingModeProvider::getAll()->each( function( ShippingMode $shippingMode ) { |
| 18 | add_filter( |
| 19 | 'woocommerce_shipping_' . $shippingMode->getMethodId() . '_instance_option', |
| 20 | $this->getShippingCost( $shippingMode ), |
| 21 | 10, |
| 22 | 3 |
| 23 | ); |
| 24 | } |
| 25 | ); |
| 26 | } |
| 27 | |
| 28 | public function getShippingCost( ShippingMode $shippingMode ) { |
| 29 | return function( $rate, $key, $wcShippingMethod ) use ( $shippingMode ) { |
| 30 | if ( $shippingMode->isManualPricingEnabled( $wcShippingMethod ) ) { |
| 31 | if ( 'cost' === $key ) { |
| 32 | $rate = $shippingMode->getShippingCostValue( $wcShippingMethod, $this->getClientCurrency() ); |
| 33 | do_action( 'wcml_shipping_cost_in_mc', $wcShippingMethod ); |
| 34 | } elseif ( $shippingMode instanceof ShippingClassesMode ) { |
| 35 | if ( $this->isShippingClass( $key ) ) { |
| 36 | $rate = $shippingMode->getShippingClassCostValue( $wcShippingMethod, $this->getClientCurrency(), $key ); |
| 37 | } elseif ( $this->isNoShippingClass( $key ) ) { |
| 38 | $rate = $shippingMode->getNoShippingClassCostValue( $wcShippingMethod, $this->getClientCurrency() ); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | return $rate; |
| 43 | }; |
| 44 | } |
| 45 | |
| 46 | private function isShippingClass( $key ) { |
| 47 | return 'class_cost_' === substr( $key, 0, 11 ); |
| 48 | } |
| 49 | |
| 50 | private function isNoShippingClass( $key ) { |
| 51 | return 'no_class_cost' === substr( $key, 0, 13 ); |
| 52 | } |
| 53 | |
| 54 | private function getClientCurrency() { |
| 55 | return $this->adjustCurrencyOnWidgetChange( $this->multiCurrency->get_client_currency() ); |
| 56 | } |
| 57 | |
| 58 | private function adjustCurrencyOnWidgetChange( $currencyCode ) { |
| 59 | $postData = wpml_collect( $_POST ); |
| 60 | $currencyCodeInRequest = $postData->get( 'currency' ); |
| 61 | if ( 'wcml_switch_currency' === $postData->get( 'action' ) |
| 62 | && $currencyCodeInRequest |
| 63 | && $this->validateCurrencyCode( $currencyCodeInRequest ) ) { |
| 64 | return $currencyCodeInRequest; |
| 65 | } |
| 66 | return $currencyCode; |
| 67 | } |
| 68 | |
| 69 | private function validateCurrencyCode( $currencyCode ) { |
| 70 | return in_array( $currencyCode, $this->multiCurrency->get_currency_codes() ); |
| 71 | } |
| 72 | |
| 73 | } |
| 74 |