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
VariableCost.php
113 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\Multicurrency\Shipping; |
| 4 | |
| 5 | trait VariableCost { |
| 6 | use DefaultConversion; |
| 7 | |
| 8 | public function getFieldTitle( $currencyCode ): ?string { |
| 9 | /* translators: %s is a currency code */ |
| 10 | return sprintf( esc_html_x( 'Cost in %s', |
| 11 | 'The label for the field with shipping cost in additional currency. The currency symbol will be added in place of %s specifier.', |
| 12 | 'woocommerce-multilingual' ), $currencyCode ); |
| 13 | } |
| 14 | |
| 15 | public function getFieldDescription( $currencyCode ): ?string { |
| 16 | /* translators: %s is a currency code */ |
| 17 | return sprintf( esc_html_x( 'The shipping cost if customer choose %s as a purchase currency.', |
| 18 | 'The description for the field with shipping cost in additional currency. The currency symbol will be added in place of %s specifier.', |
| 19 | 'woocommerce-multilingual' ), $currencyCode ); |
| 20 | } |
| 21 | |
| 22 | private function getCostKey( $currencyCode ) { |
| 23 | return sprintf( 'cost_%s', $currencyCode ); |
| 24 | } |
| 25 | |
| 26 | private function getShippingClassCostKey( $shippingClassKey, $currency ) { |
| 27 | return $this->replaceShippingClassId( $shippingClassKey ) . '_' . $currency; |
| 28 | } |
| 29 | |
| 30 | private function getNoShippingClassCostKey( $currency ): string { |
| 31 | return 'no_class_cost_' . $currency; |
| 32 | } |
| 33 | |
| 34 | public function getSettingsFormKey( $currencyCode ) { |
| 35 | return $this->getCostKey( $currencyCode ); |
| 36 | } |
| 37 | |
| 38 | public function getMinimalOrderAmountValue( $amount, $shipping, $currency ) { |
| 39 | return $amount; |
| 40 | } |
| 41 | |
| 42 | public function getShippingCostValue( $rate, $currency ) { |
| 43 | $costName = $this->getCostKey( $currency ); |
| 44 | return $this->getCostValueForName( $rate, $currency, $costName, 'cost' ); |
| 45 | } |
| 46 | |
| 47 | public function getShippingClassCostValue( $rate, $currency, $shippingClassKey ) { |
| 48 | $costName = $this->getShippingClassCostKey( $shippingClassKey, $currency ); |
| 49 | return $this->getCostValueForName( $rate, $currency, $costName, $shippingClassKey ); |
| 50 | } |
| 51 | |
| 52 | public function getNoShippingClassCostValue( $rate, $currency ) { |
| 53 | $costName = $this->getNoShippingClassCostKey( $currency ); |
| 54 | return $this->getCostValueForName( $rate, $currency, $costName, 'no_class_cost' ); |
| 55 | } |
| 56 | |
| 57 | private function getCostValueForName( $rate, $currency, $costName, $rateField ) { |
| 58 | if ( ! isset( $rate->$rateField ) ) { |
| 59 | @$rate->$rateField = 0; |
| 60 | } |
| 61 | if ( ! empty( $rate->instance_id ) ) { |
| 62 | if ( $this->isManualPricingEnabled( $rate ) ) { |
| 63 | $rateSettings = $this->getWpOption( $this->getMethodId(), $rate->instance_id ); |
| 64 | if ( ! empty( $rateSettings[ $costName ] ) ) { |
| 65 | $rate->$rateField = $rateSettings[ $costName ]; |
| 66 | } else { |
| 67 | $rate->$rateField = $this->getValueFromDefaultCurrency( $rate->$rateField, $rateSettings, $costName, $currency ); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | return $rate->$rateField; |
| 72 | } |
| 73 | |
| 74 | public function isManualPricingEnabled( $instance ) { |
| 75 | return self::isEnabled( $this->getWpOption( $this->getMethodId(), $instance->instance_id ) ); |
| 76 | } |
| 77 | |
| 78 | private function getWpOption( $methodId, $instanceId ) { |
| 79 | return get_option( \WCML_Multi_Currency_Shipping::getShippingOptionName( $methodId, $instanceId ) ); |
| 80 | } |
| 81 | |
| 82 | private function getShippingClassTermId( $key ) { |
| 83 | if ( preg_match( '/^class_cost_(\d*)(_[A-Z]*)*$/', $key, $matches ) && isset( $matches[1] ) ) { |
| 84 | return $matches[1]; |
| 85 | } |
| 86 | return false; |
| 87 | } |
| 88 | private function replaceShippingClassId( $shippingClassKey ) { |
| 89 | $termId = $this->getShippingClassTermId( $shippingClassKey ); |
| 90 | if ( $termId ) { |
| 91 | $termTrid = apply_filters( 'wpml_element_trid', null, $termId, 'tax_product_shipping_class' ); |
| 92 | $termTranslations = apply_filters( 'wpml_get_element_translations', null, $termTrid, 'tax_product_shipping_class' ); |
| 93 | if ( is_array( $termTranslations ) ) { |
| 94 | foreach ( $termTranslations as $translation ) { |
| 95 | if ( $translation->source_language_code === null ) { |
| 96 | $shippingClassKey = str_replace( $termId, $translation->element_id, $shippingClassKey ); |
| 97 | break; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | return $shippingClassKey; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | public function _testGetShippingClassTermId( $key ) { |
| 107 | if ( ! isset( $_SERVER['SCRIPT_NAME'] ) || stristr( $_SERVER['SCRIPT_NAME'], 'phpunit' ) === false ) { |
| 108 | die( "don't run this method directly outside phpunit env" ); |
| 109 | } |
| 110 | return $this->getShippingClassTermId( $key ); |
| 111 | } |
| 112 | } |
| 113 |