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
ShippingClasses.php
69 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\Multicurrency\Shipping; |
| 4 | |
| 5 | class ShippingClasses { |
| 6 | public static function addFields( array $field, \WCML_Multi_Currency $wcmlMultiCurrency ) { |
| 7 | $shippingClasses = WC()->shipping()->get_shipping_classes(); |
| 8 | if ( ! empty( $shippingClasses ) ) { |
| 9 | foreach ( $wcmlMultiCurrency->get_currency_codes() as $currencyCode ) { |
| 10 | if ( $wcmlMultiCurrency->get_default_currency() === $currencyCode ) { |
| 11 | continue; |
| 12 | } |
| 13 | foreach ( $shippingClasses as $shippingClass ) { |
| 14 | $classSourceLanguageCode = self::getSourceLanguageCode( $shippingClass ); |
| 15 | if ( $classSourceLanguageCode === null ) { |
| 16 | $field = self::addShippingClassField( $field, $shippingClass, $currencyCode ); |
| 17 | } else { |
| 18 | $field = self::askToSwitchLanguage( $field, $shippingClass, $classSourceLanguageCode ); |
| 19 | } |
| 20 | } |
| 21 | $field = self::addNoShippingClassField( $field, $currencyCode ); |
| 22 | } |
| 23 | } |
| 24 | return $field; |
| 25 | } |
| 26 | |
| 27 | protected static function getSourceLanguageCode( $shippingClass ) { |
| 28 | $classLanguageDetails = apply_filters( 'wpml_element_language_details', false, [ |
| 29 | 'element_id' => $shippingClass->term_id, |
| 30 | 'element_type' => $shippingClass->taxonomy, |
| 31 | ] ); |
| 32 | return $classLanguageDetails->source_language_code ?? null; |
| 33 | } |
| 34 | |
| 35 | protected static function askToSwitchLanguage( $field, $shippingClass, $classSourceLanguageCode ) { |
| 36 | $field[ 'wcml_ask_to_switch_language_' . $shippingClass->term_id ] = [ |
| 37 | 'title' => '', |
| 38 | /* translators: %1$s is a shipping class name and %2$s is a language code */ |
| 39 | 'description' => sprintf( __( 'Your shipping class %1$s has been created in %2$s language. Please switch your language if you want to provide shipping costs in different currencies for this class.', 'woocommerce-multilingual' ), |
| 40 | $shippingClass->name, |
| 41 | $classSourceLanguageCode), |
| 42 | 'type' => 'title' |
| 43 | ]; |
| 44 | return $field; |
| 45 | } |
| 46 | |
| 47 | protected static function addShippingClassField( $field, $shippingClass, $currencyCode ) { |
| 48 | $field[ 'class_cost_' . $shippingClass->term_id . '_' . $currencyCode ] = [ |
| 49 | /* translators: %1$s is a shipping class name and %2$s is a currency code */ |
| 50 | 'title' => sprintf( __( '"%1$s" shipping class cost in %2$s', 'woocommerce-multilingual' ), esc_html( $shippingClass->name ), esc_html( $currencyCode ) ), |
| 51 | 'type' => 'text', |
| 52 | 'placeholder' => __( 'N/A', 'woocommerce-multilingual' ), |
| 53 | 'class' => 'wcml-shipping-cost-currency' |
| 54 | ]; |
| 55 | return $field; |
| 56 | } |
| 57 | |
| 58 | protected static function addNoShippingClassField( $field, $currencyCode ) { |
| 59 | $field[ 'no_class_cost_' . $currencyCode ] = [ |
| 60 | /* translators: %s is a currency code */ |
| 61 | 'title' => sprintf( __( 'No shipping class cost in %s', 'woocommerce-multilingual' ), esc_html( $currencyCode ) ), |
| 62 | 'type' => 'text', |
| 63 | 'placeholder' => __( 'N/A', 'woocommerce-multilingual' ), |
| 64 | 'default' => '', |
| 65 | 'class' => 'wcml-shipping-cost-currency' |
| 66 | ]; |
| 67 | return $field; |
| 68 | } |
| 69 | } |