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.6 4.7.7 4.7.8 4.7.9 4.8.0 4.9.0 4.9.1 5.0.1 5.0.2 5.1.1 5.1.2 5.1.3 5.2.0 5.2.1 5.3.2 5.3.3.1 5.3.4 5.3.5 5.3.6 5.3.7
woocommerce-multilingual / classes / Multicurrency / Shipping / VariableCost.php
woocommerce-multilingual / classes / Multicurrency / Shipping Last commit date
AdminHooks.php 1 month ago DefaultConversion.php 1 month ago FlatRateShipping.php 5 years ago FreeShipping.php 1 month ago FrontEndHooks.php 1 month ago LocalPickup.php 5 years ago ShippingClasses.php 1 month ago ShippingClassesMode.php 1 month ago ShippingHooksFactory.php 1 month ago ShippingMode.php 1 month ago ShippingModeBase.php 1 month ago ShippingModeProvider.php 1 month ago UnsupportedShipping.php 1 month ago VariableCost.php 1 month 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