| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Description of Shipping |
| 5 |
* |
| 6 |
* @author Ali2Woo Team |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace AliNext_Lite;; |
| 10 |
|
| 11 |
class Shipping { |
| 12 |
|
| 13 |
public static function get_fake_method_id(){ |
| 14 |
return "a2wl-fake-shipping"; |
| 15 |
} |
| 16 |
|
| 17 |
public static function get_order_item_shipping_meta_key(){ |
| 18 |
return "_a2w_customer_chosen_shipping"; |
| 19 |
} |
| 20 |
|
| 21 |
//compatibility with Ali2Woo before 1.18.2 |
| 22 |
public static function get_order_item_legacy_shipping_meta_key(){ |
| 23 |
return "a2wl_shipping_code"; |
| 24 |
} |
| 25 |
|
| 26 |
public static function get_countries() { |
| 27 |
$countries = array_merge(WC()->countries->get_allowed_countries(), WC()->countries->get_shipping_countries()); |
| 28 |
return $countries; |
| 29 |
} |
| 30 |
|
| 31 |
public static function get_not_available_shipping_message($country){ |
| 32 |
|
| 33 |
$not_available_message = get_setting( 'aliship_not_available_message' ); |
| 34 |
$remove_cart_item = get_setting( 'aliship_not_available_remove' ); |
| 35 |
|
| 36 |
if ( $remove_cart_item ) { |
| 37 |
$default_shipping_message = str_replace( array( |
| 38 |
'{shipping_cost}', |
| 39 |
'{delivery_time}', |
| 40 |
'{country}' |
| 41 |
), '', $not_available_message ); |
| 42 |
} else { |
| 43 |
$not_available_cost = get_setting( 'aliship_not_available_cost' ); |
| 44 |
if ( ! $not_available_cost ) { |
| 45 |
$not_available_cost = esc_html__( 'free', 'ali2woo' ); |
| 46 |
} else { |
| 47 |
$not_available_cost = apply_filters('wcml_raw_price_amount', $not_available_cost); |
| 48 |
$not_available_cost = wc_price( $not_available_cost ); |
| 49 |
} |
| 50 |
$default_shipping_message = str_replace( array( |
| 51 |
'{shipping_cost}', |
| 52 |
'{delivery_time}' |
| 53 |
), array( |
| 54 |
$not_available_cost, |
| 55 |
self::process_delivery_time( get_setting( 'aliship_not_available_time_min' ) . '-' . get_setting( 'aliship_not_available_time_max' )) |
| 56 |
), $not_available_message ); |
| 57 |
|
| 58 |
$default_shipping_message = str_replace('{country}', $country, $default_shipping_message); |
| 59 |
} |
| 60 |
|
| 61 |
return $default_shipping_message; |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
public static function get_selection_types(){ |
| 66 |
return array('popup','select'); |
| 67 |
} |
| 68 |
|
| 69 |
public static function get_shipping_types(){ |
| 70 |
return array( |
| 71 |
'none' => _x('Do not calculate item shipping, only save customer`s shipping option', 'Setting title', 'ali2woo'), |
| 72 |
'new' => _x('Create a new shipping method and add it to currently available shipping options', 'Setting title', 'ali2woo'), |
| 73 |
'new_only' => _x('Create a new shipping method and make it the only available shipping option', 'Setting title', 'ali2woo'), |
| 74 |
'add' => _x('Calculate AliExpress shipping cost of all items in cart and add the cost to all currently available shipping options', 'Setting title', 'ali2woo'), |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
public static function get_selection_position_types(){ |
| 79 |
return array('before_cart' => _x('Before add-to-cart button', 'Setting title', 'ali2woo'), |
| 80 |
'after_cart' => _x('After add-to-cart button', 'Setting title', 'ali2woo')); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Get local method by original method company |
| 85 |
* retun false if this method is disabled in settings |
| 86 |
*/ |
| 87 |
public static function get_local_method_by_company($company){ |
| 88 |
return ShippingPostType::get_item($company); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Add method to the local methods and return its local data |
| 93 |
*/ |
| 94 |
public static function add_local_method($method){ |
| 95 |
ShippingPostType::add_item($method['company'], $method['serviceName']); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Add additional data to the method properties depending on the plugin settings |
| 100 |
*/ |
| 101 |
public static function get_normalized($method, $country, $type = "select", $product=false){ |
| 102 |
|
| 103 |
$countries = Shipping::get_countries(); |
| 104 |
|
| 105 |
$country_label = $countries[$country]; |
| 106 |
|
| 107 |
$local_values = self::get_local_method_by_company($method['company']); |
| 108 |
|
| 109 |
//this method is disabled in the shipping list |
| 110 |
if ( $local_values === false) return false; |
| 111 |
|
| 112 |
//if no such method there, add it |
| 113 |
if (!$local_values) { |
| 114 |
self::add_local_method($method); |
| 115 |
$local_values = self::get_local_method_by_company($method['company']); |
| 116 |
} |
| 117 |
|
| 118 |
$method['company'] = $local_values['title']; |
| 119 |
|
| 120 |
$ship_price = ShippingPriceFormula::apply_formula($method, $local_values); |
| 121 |
$current_currency = apply_filters('wcml_price_currency', NULL ); |
| 122 |
$method['price'] = $ship_price = apply_filters( 'wcml_raw_price_amount', $ship_price, $current_currency ); |
| 123 |
|
| 124 |
$method['formated_price'] = wp_strip_all_tags(wc_price($method['price'])); |
| 125 |
|
| 126 |
$method['formated_delivery_time'] = self::process_delivery_time($method['time']); |
| 127 |
|
| 128 |
$method_price_html = $method['price'] ? $method['formated_price'] : esc_html__('free', 'ali2woo'); |
| 129 |
|
| 130 |
//1) for visual info (in popup view) or select label |
| 131 |
|
| 132 |
$shipping_option_text = get_setting('aliship_shipping_option_text'); |
| 133 |
|
| 134 |
//do not replace {country}, it will be replaced in js |
| 135 |
$shipping_info = str_replace( array('{shipping_cost}', '{shipping_company}', '{delivery_time}', '{country}'), |
| 136 |
array( $method_price_html, $method['company'], $method['formated_delivery_time'], $country_label), $shipping_option_text |
| 137 |
); |
| 138 |
|
| 139 |
$method['label'] = $shipping_info; |
| 140 |
|
| 141 |
if ($type == "popup"){ |
| 142 |
//2) for modal table |
| 143 |
$html = '<div class="a2wl-div-table-row">' . |
| 144 |
'<div class="a2wl-div-table-col small-col">' . |
| 145 |
'<input type="radio" class="select_method" value= "' . $method['serviceName'] . '" name="a2wl_shipping_method_popup_field_{item_id}" id="a2wl_shipping_method_popup_field_{item_id}_' . $method['serviceName'] . '">' . |
| 146 |
'</div>' . |
| 147 |
'<div class="a2wl-div-table-col">' . $method['formated_delivery_time'] . '</div>' . |
| 148 |
'<div class="a2wl-div-table-col">' . $method_price_html . '</div>' . |
| 149 |
'<div class="a2wl-div-table-col">' . (isset($method['tracking']) && $method['tracking'] ? esc_html__('yes', 'ali2woo') : esc_html__('no', 'ali2woo')) . '</div>' . |
| 150 |
'<div class="a2wl-div-table-col">' . $method['company'] . '</div>' . |
| 151 |
'</div>'; |
| 152 |
|
| 153 |
$method['html_row'] = $html; |
| 154 |
} |
| 155 |
|
| 156 |
return $method; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
public static function process_delivery_time( $time ) { |
| 161 |
$time_arr = explode( '-', $time ); |
| 162 |
if ( count( $time_arr ) === 2 ) { |
| 163 |
$min = intval( $time_arr[0] ); |
| 164 |
if ( $min === intval( $time_arr[1] ) ) { |
| 165 |
/* translators: %s is replaced with "string" */ |
| 166 |
$return = sprintf( _n( '%s day', '%s days', $min, 'ali2woo' ), $min ); |
| 167 |
} else { |
| 168 |
/* translators: %s is replaced with "string" */ |
| 169 |
$return = sprintf( esc_html__( '%s days', 'ali2woo' ), $time ); |
| 170 |
} |
| 171 |
} else { |
| 172 |
/* translators: %s is replaced with "string" */ |
| 173 |
$return = sprintf( _n( '%s day', '%s days', $time, 'ali2woo' ), $time ); |
| 174 |
} |
| 175 |
|
| 176 |
return $return; |
| 177 |
} |
| 178 |
|
| 179 |
public static function table_of_placeholders( $args ) { |
| 180 |
if ( count( $args ) ) { |
| 181 |
?> |
| 182 |
<div class="table-responsive"> |
| 183 |
<table class="table table-bordered a2wl-table-of-placeholders"> |
| 184 |
<thead> |
| 185 |
<tr class="active"> |
| 186 |
<th scope="col" style="width: 50%"><?php esc_html_e( 'Placeholder', 'ali2woo' ) ?></th> |
| 187 |
<th scope="col" style="width: 50%"><?php esc_html_e( 'Purpose', 'ali2woo' ) ?></th> |
| 188 |
</tr> |
| 189 |
</thead> |
| 190 |
<tbody> |
| 191 |
<?php |
| 192 |
foreach ( $args as $key => $value ) { |
| 193 |
?> |
| 194 |
<tr> |
| 195 |
<td class="a2wl-placeholder-value-container"> |
| 196 |
<div class="form-inline" role="form"> |
| 197 |
<div class="form-group has-success has-feedback"> |
| 198 |
<input |
| 199 |
class="a2wl-placeholder-value form-control" type="text" |
| 200 |
readonly value="<?php echo esc_attr( "{{$key}}" ); ?>"> |
| 201 |
<span class="dashicons dashicons-admin-page form-control-feedback a2wl-placeholder-value-copy"></span> |
| 202 |
</div> |
| 203 |
</div> |
| 204 |
</td> |
| 205 |
<td><?php echo esc_html__( $value ); ?></td> |
| 206 |
</tr> |
| 207 |
<?php |
| 208 |
} |
| 209 |
?> |
| 210 |
</tbody> |
| 211 |
</table> |
| 212 |
</div> |
| 213 |
<?php |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
public static function get_formated_shipping_info_meta($order_id, $meta_value): string |
| 218 |
{ |
| 219 |
$shipping_info = json_decode($meta_value, true); |
| 220 |
$delivery_time = self::process_delivery_time($shipping_info['delivery_time']); |
| 221 |
$shipping_cost = floatval(($shipping_info['shipping_cost'])); |
| 222 |
|
| 223 |
if ($shipping_cost) { |
| 224 |
$WC_Order = wc_get_order($order_id); |
| 225 |
$shipping_cost = apply_filters('wcml_raw_price_amount', $shipping_cost); |
| 226 |
$shipping_cost = wc_price( $shipping_cost, ['currency' => $WC_Order->get_currency()]); |
| 227 |
$display_value = "[{$shipping_cost}] {$shipping_info['company']} ({$delivery_time})"; |
| 228 |
} else { |
| 229 |
$display_value = "[" . esc_html__( 'Free', 'ali2woo' ) . "] {$shipping_info['company']} ({$delivery_time})"; |
| 230 |
} |
| 231 |
|
| 232 |
return $display_value; |
| 233 |
} |
| 234 |
} |
| 235 |
|