woocommerce-multilingual
/
inc
/
template-classes
/
multi-currency
/
class-wcml-custom-prices-ui.php
class-wcml-custom-prices-ui.php
4 weeks ago
class-wcml-exchange-rates-ui.php
4 weeks ago
class-wcml-multi-currency-ui.php
4 weeks ago
class-wcml-custom-prices-ui.php
190 lines
| 1 | <?php |
| 2 | |
| 3 | class WCML_Custom_Prices_UI extends WCML_Templates_Factory { |
| 4 | |
| 5 | private $woocommerce_wpml; |
| 6 | private $product_id; |
| 7 | private $custom_prices; |
| 8 | private $is_variation; |
| 9 | |
| 10 | private $custom_prices_fields; |
| 11 | |
| 12 | private $custom_prices_fields_labels; |
| 13 | |
| 14 | |
| 15 | public function __construct( $woocommerce_wpml, $product_id ) { |
| 16 | parent::__construct(); |
| 17 | |
| 18 | $this->woocommerce_wpml = $woocommerce_wpml; |
| 19 | $this->product_id = $product_id; |
| 20 | $this->is_variation = get_post_type( $product_id ) == 'product_variation'; |
| 21 | $this->custom_prices = get_post_custom( $product_id ); |
| 22 | $this->custom_prices_fields = apply_filters( 'wcml_custom_prices_fields', [ '_regular_price', '_sale_price' ], $product_id ); |
| 23 | $this->custom_prices_fields_labels = apply_filters( |
| 24 | 'wcml_custom_prices_fields_labels', |
| 25 | [ |
| 26 | '_regular_price' => __( 'Regular Price', 'woocommerce-multilingual' ), |
| 27 | '_sale_price' => __( 'Sale Price', 'woocommerce-multilingual' ), |
| 28 | ], |
| 29 | $product_id |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | public function get_model() { |
| 34 | $model = [ |
| 35 | 'product_id' => $this->product_id, |
| 36 | 'currencies' => $this->get_currencies_info(), |
| 37 | 'checked_calc_auto' => ! isset( $this->custom_prices['_wcml_custom_prices_status'] ) || ( isset( $this->custom_prices['_wcml_custom_prices_status'] ) && $this->custom_prices['_wcml_custom_prices_status'][0] == 0 ) ? 'checked="checked"' : '', |
| 38 | 'checked_calc_manually' => isset( $this->custom_prices['_wcml_custom_prices_status'] ) && $this->custom_prices['_wcml_custom_prices_status'][0] == 1 ? 'checked="checked"' : '', |
| 39 | 'wc_currencies' => get_woocommerce_currencies(), |
| 40 | 'is_variation' => $this->is_variation, |
| 41 | 'html_id' => $this->is_variation ? '[' . $this->product_id . ']' : '', |
| 42 | 'strings' => apply_filters( |
| 43 | 'wcml_custom_prices_strings', |
| 44 | [ |
| 45 | 'not_set' => sprintf( |
| 46 | /* translators: %1$s and %2$s are opening and closing HTML link tags */ |
| 47 | __( |
| 48 | 'Multi-currency is enabled, but no secondary currencies have been set. %1$sAdd secondary currency%2$s.', |
| 49 | 'woocommerce-multilingual' |
| 50 | ), |
| 51 | '<a href="' . \WCML\Utilities\AdminUrl::getMultiCurrencyTab() . '">', |
| 52 | '</a>' |
| 53 | ), |
| 54 | 'calc_auto' => __( 'Calculate prices in other currencies automatically', 'woocommerce-multilingual' ), |
| 55 | 'see_prices' => __( 'Click to see the prices in the other currencies as they are currently shown on the front end.', 'woocommerce-multilingual' ), |
| 56 | 'show' => __( 'Show', 'woocommerce-multilingual' ), |
| 57 | 'hide' => __( 'Hide', 'woocommerce-multilingual' ), |
| 58 | 'set_manually' => __( 'Set prices in other currencies manually', 'woocommerce-multilingual' ), |
| 59 | 'enter_prices' => __( 'Enter prices in other currencies', 'woocommerce-multilingual' ), |
| 60 | 'hide_prices' => __( 'Hide prices in other currencies', 'woocommerce-multilingual' ), |
| 61 | 'det_auto' => __( 'Determined automatically based on exchange rate', 'woocommerce-multilingual' ), |
| 62 | '_regular_price' => __( 'Regular Price', 'woocommerce-multilingual' ), |
| 63 | '_sale_price' => __( 'Sale Price', 'woocommerce-multilingual' ), |
| 64 | 'schedule' => __( 'Schedule', 'woocommerce-multilingual' ), |
| 65 | 'same_as_def' => __( 'Same as default currency', 'woocommerce-multilingual' ), |
| 66 | 'set_dates' => __( 'Set dates', 'woocommerce-multilingual' ), |
| 67 | 'collapse' => __( 'Collapse', 'woocommerce-multilingual' ), |
| 68 | 'from' => __( 'From…', 'woocommerce-multilingual' ), |
| 69 | 'to' => __( 'To…', 'woocommerce-multilingual' ), |
| 70 | 'enter_price' => __( 'Please enter in a value less than the regular price', 'woocommerce-multilingual' ), |
| 71 | ], |
| 72 | $this->product_id |
| 73 | ), |
| 74 | ]; |
| 75 | |
| 76 | return $model; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | public function get_currencies_info() { |
| 81 | |
| 82 | $currencies = $this->woocommerce_wpml->multi_currency->get_currencies(); |
| 83 | $wc_currencies = get_woocommerce_currencies(); |
| 84 | |
| 85 | foreach ( $currencies as $key => $currency ) { |
| 86 | |
| 87 | $currencies[ $key ]['currency_code'] = $key; |
| 88 | |
| 89 | foreach ( $this->custom_prices_fields as $price_field ) { |
| 90 | $currencies[ $key ]['readonly_price'][ $price_field ] = ''; |
| 91 | $currencies[ $key ]['custom_price'][ $price_field ] = ''; |
| 92 | } |
| 93 | |
| 94 | if ( $this->product_id ) { |
| 95 | |
| 96 | foreach ( $this->custom_prices_fields as $price_field ) { |
| 97 | $currencies[ $key ]['readonly_price'][ $price_field ] = get_post_meta( $this->product_id, $price_field, true ); |
| 98 | if ( $currencies[ $key ]['readonly_price'][ $price_field ] ) { |
| 99 | $currencies[ $key ]['readonly_price'][ $price_field ] = $currencies[ $key ]['readonly_price'][ $price_field ] * $currency['rate']; |
| 100 | $currencies[ $key ]['readonly_price'][ $price_field ] = wc_format_localized_price( $currencies[ $key ]['readonly_price'][ $price_field ] ); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | if ( isset( $this->custom_prices['_wcml_custom_prices_status'] ) ) { |
| 106 | |
| 107 | foreach ( $this->custom_prices_fields as $price_field ) { |
| 108 | if ( isset( $this->custom_prices[ $price_field . '_' . $key ][0] ) ) { |
| 109 | $currencies[ $key ]['custom_price'][ $price_field ] = wc_format_localized_price( $this->custom_prices[ $price_field . '_' . $key ][0] ); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | $currencies[ $key ]['currency_format'] = $wc_currencies[ $key ] . ' ( ' . get_woocommerce_currency_symbol( $key ) . ' )'; |
| 115 | $currencies[ $key ]['currency_symbol'] = get_woocommerce_currency_symbol( $key ); |
| 116 | |
| 117 | if ( $this->is_variation ) { |
| 118 | $currencies[ $key ]['custom_id'] = '[' . $key . '][' . $this->product_id . ']'; |
| 119 | } else { |
| 120 | $currencies[ $key ]['custom_id'] = '[' . $key . ']'; |
| 121 | |
| 122 | $wc_input = []; |
| 123 | |
| 124 | $wc_input['custom_attributes'] = []; |
| 125 | $wc_input['type_name'] = 'data_type'; |
| 126 | $wc_input['type_val'] = 'price'; |
| 127 | |
| 128 | foreach ( $this->custom_prices_fields as $price_field ) { |
| 129 | ob_start(); |
| 130 | woocommerce_wp_text_input( |
| 131 | [ |
| 132 | 'id' => '_custom' . $price_field . '[' . $key . ']', |
| 133 | 'value' => wc_format_localized_price( $currencies[ $key ]['custom_price'][ $price_field ] ), |
| 134 | 'class' => 'wc_input_price wcml_input_price short wcml' . $price_field, |
| 135 | 'label' => $this->custom_prices_fields_labels[ $price_field ] . ' (' . $currencies[ $key ]['currency_symbol'] . ')', |
| 136 | $wc_input['type_name'] => $wc_input['type_val'], |
| 137 | 'custom_attributes' => $wc_input['custom_attributes'], |
| 138 | ] |
| 139 | ); |
| 140 | $currencies[ $key ]['custom_html'][ $price_field ] = ob_get_contents(); |
| 141 | ob_end_clean(); |
| 142 | } |
| 143 | |
| 144 | $wc_input['custom_attributes'] = [ |
| 145 | 'readonly' => 'readonly', |
| 146 | 'rel' => $currency['rate'], |
| 147 | ]; |
| 148 | |
| 149 | foreach ( $this->custom_prices_fields as $price_field ) { |
| 150 | ob_start(); |
| 151 | woocommerce_wp_text_input( |
| 152 | [ |
| 153 | 'id' => '_readonly' . $price_field, |
| 154 | 'value' => wc_format_localized_price( $currencies[ $key ]['readonly_price'][ $price_field ] ), |
| 155 | 'class' => 'wc_input_price short', |
| 156 | 'label' => $this->custom_prices_fields_labels[ $price_field ] . ' (' . $currencies[ $key ]['currency_symbol'] . ')', |
| 157 | $wc_input['type_name'] => $wc_input['type_val'], |
| 158 | 'custom_attributes' => $wc_input['custom_attributes'], |
| 159 | ] |
| 160 | ); |
| 161 | $currencies[ $key ]['readonly_html'][ $price_field ] = ob_get_contents(); |
| 162 | ob_end_clean(); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | $currencies[ $key ]['schedule_auto_checked'] = ( ! isset( $this->custom_prices[ '_wcml_schedule_' . $key ] ) || ( isset( $this->custom_prices[ '_wcml_schedule_' . $key ] ) && $this->custom_prices[ '_wcml_schedule_' . $key ][0] == 0 ) ) ? 'checked="checked"' : ' '; |
| 167 | $currencies[ $key ]['schedule_man_checked'] = isset( $this->custom_prices[ '_wcml_schedule_' . $key ] ) && $this->custom_prices[ '_wcml_schedule_' . $key ][0] == 1 ? 'checked="checked"' : ' '; |
| 168 | |
| 169 | $currencies[ $key ]['sale_price_dates_from'] = ( isset( $this->custom_prices[ '_sale_price_dates_from_' . $key ] ) && $this->custom_prices[ '_sale_price_dates_from_' . $key ][0] != '' ) ? date_i18n( 'Y-m-d', $this->custom_prices[ '_sale_price_dates_from_' . $key ][0] ) : ''; |
| 170 | $currencies[ $key ]['sale_price_dates_to'] = ( isset( $this->custom_prices[ '_sale_price_dates_to_' . $key ] ) && $this->custom_prices[ '_sale_price_dates_to_' . $key ][0] != '' ) ? date_i18n( 'Y-m-d', $this->custom_prices[ '_sale_price_dates_to_' . $key ][0] ) : ''; |
| 171 | |
| 172 | } |
| 173 | |
| 174 | return $currencies; |
| 175 | |
| 176 | } |
| 177 | |
| 178 | |
| 179 | public function init_template_base_dir() { |
| 180 | $this->template_paths = [ |
| 181 | WCML_PLUGIN_PATH . '/templates/multi-currency/', |
| 182 | ]; |
| 183 | } |
| 184 | |
| 185 | public function get_template() { |
| 186 | return 'custom-prices.twig'; |
| 187 | } |
| 188 | |
| 189 | } |
| 190 |