| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Rest_API\Barcode\Item_Info file. |
| 4 |
* |
| 5 |
* @package PostNLWooCommerce\Rest_API\Legacy\Barcode |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace PostNLWooCommerce\Rest_API\Legacy\Barcode; |
| 9 |
|
| 10 |
use PostNLWooCommerce\Helper\Mapping; |
| 11 |
use PostNLWooCommerce\Rest_API\Base_Info; |
| 12 |
use PostNLWooCommerce\Shipping_Method\Settings; |
| 13 |
use PostNLWooCommerce\Utils; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Class Item_Info |
| 21 |
* |
| 22 |
* @package PostNLWooCommerce\Rest_API\Legacy\Barcode |
| 23 |
*/ |
| 24 |
class Item_Info extends Base_Info { |
| 25 |
/** |
| 26 |
* API args. |
| 27 |
* |
| 28 |
* @var api_args |
| 29 |
*/ |
| 30 |
protected $api_args; |
| 31 |
|
| 32 |
/** |
| 33 |
* Settings class instance. |
| 34 |
* |
| 35 |
* @var PostNLWooCommerce\Shipping_Method\Settings |
| 36 |
*/ |
| 37 |
protected $settings; |
| 38 |
|
| 39 |
/** |
| 40 |
* Query args of the item info. |
| 41 |
* |
| 42 |
* @var array |
| 43 |
*/ |
| 44 |
public $query_args; |
| 45 |
|
| 46 |
/** |
| 47 |
* Parses the arguments and sets the instance's properties. |
| 48 |
* |
| 49 |
* @throws \Exception If some data in $args did not pass validation. |
| 50 |
*/ |
| 51 |
protected function parse_args() { |
| 52 |
$barcode_args = array_merge( $this->api_args['settings'], $this->api_args['shipping_address'] ); |
| 53 |
$this->query_args = Utils::parse_args( $barcode_args, $this->get_query_info_schema() ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Method to convert the post data to API args. |
| 58 |
* |
| 59 |
* @param Array $post_data Data from post variable in checkout page. |
| 60 |
* |
| 61 |
* @throws \Exception If order id does not exist. |
| 62 |
*/ |
| 63 |
public function convert_data_to_args( $post_data ) { |
| 64 |
if ( ! is_a( $post_data['order'], 'WC_Order' ) ) { |
| 65 |
throw new \Exception( |
| 66 |
__( 'Order ID does not exist!', 'postnl-for-woocommerce' ) |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
$order = $post_data['order']; |
| 71 |
$shipping_country = $order->get_shipping_country(); |
| 72 |
$shipping_state = $order->get_shipping_state(); |
| 73 |
|
| 74 |
if ( ! empty( $post_data['customer_code'] ) ) { |
| 75 |
$this->api_args['custom']['customer_code'] = $post_data['customer_code']; |
| 76 |
} |
| 77 |
|
| 78 |
$this->api_args['shipping_address'] = array( |
| 79 |
'first_name' => $order->get_shipping_first_name(), |
| 80 |
'last_name' => $order->get_shipping_last_name(), |
| 81 |
'company' => $order->get_shipping_company(), |
| 82 |
'address_1' => $order->get_shipping_address_1(), |
| 83 |
'address_2' => $order->get_shipping_address_2(), |
| 84 |
'city' => $order->get_shipping_city(), |
| 85 |
'state' => $shipping_state, |
| 86 |
'country' => Utils::is_canary_island( $shipping_state, $shipping_country ) ? 'IC' : $shipping_country, |
| 87 |
'postcode' => $order->get_shipping_postcode(), |
| 88 |
); |
| 89 |
|
| 90 |
// This will be used to determined if we must use a specific barcode types. |
| 91 |
$this->api_args['backend_data'] = $post_data['saved_data']['backend'] ?? array(); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Set extra API args. |
| 96 |
*/ |
| 97 |
public function set_extra_data_to_api_args() { |
| 98 |
$this->set_rest_of_world_args(); |
| 99 |
$this->set_custom_customer_code(); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Change or set the args value for rest of the world. |
| 104 |
*/ |
| 105 |
public function set_rest_of_world_args() { |
| 106 |
if ( ! $this->is_rest_of_world() ) { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
$this->api_args['settings']['customer_code'] = $this->settings->get_globalpack_customer_code(); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Change or set the args value for custom customer code. |
| 115 |
*/ |
| 116 |
public function set_custom_customer_code() { |
| 117 |
if ( empty( $this->api_args['custom']['customer_code'] ) ) { |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
$this->api_args['settings']['customer_code'] = $this->api_args['custom']['customer_code']; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Retrieves the args scheme to use with for parsing store address info. |
| 126 |
* |
| 127 |
* @return array. |
| 128 |
*/ |
| 129 |
protected function get_query_info_schema() { |
| 130 |
// Closures in PHP 5.3 do not inherit class context |
| 131 |
// So we need to copy $this into a lexical variable and pass it to closures manually. |
| 132 |
$self = $this; |
| 133 |
|
| 134 |
return array( |
| 135 |
'customer_code' => array( |
| 136 |
'error' => __( 'Customer Code is empty!', 'postnl-for-woocommerce' ), |
| 137 |
), |
| 138 |
'customer_num' => array( |
| 139 |
'error' => __( 'Customer Number is empty!', 'postnl-for-woocommerce' ), |
| 140 |
), |
| 141 |
'globalpack_barcode_type' => array( |
| 142 |
'rename' => 'barcode_type', |
| 143 |
'default' => '3S', |
| 144 |
'sanitize' => function ( $value ) use ( $self ) { |
| 145 |
if ( ! $self->is_rest_of_world() ) { |
| 146 |
return $self->check_product_barcode_type( '3S' ); |
| 147 |
} |
| 148 |
|
| 149 |
// Use barcode type for specific products. |
| 150 |
$value = $self->check_product_barcode_type( $value ); |
| 151 |
|
| 152 |
return $self->string_length_sanitization( $value, 4 ); |
| 153 |
}, |
| 154 |
), |
| 155 |
'serie' => array( |
| 156 |
'default' => '000000000-999999999', |
| 157 |
'sanitize' => function ( $serie ) use ( $self ) { |
| 158 |
|
| 159 |
$barcode_type = $self->check_product_barcode_type( $self->api_args['settings'] ); |
| 160 |
if ( in_array( $barcode_type, array( 'UE', 'LA' ) ) ) { |
| 161 |
return '00000000-99999999'; |
| 162 |
} |
| 163 |
|
| 164 |
if ( $self->is_europe() ) { |
| 165 |
return '0000000-9999999'; |
| 166 |
} |
| 167 |
|
| 168 |
if ( $self->is_rest_of_world() ) { |
| 169 |
return '0000-9999'; |
| 170 |
} |
| 171 |
|
| 172 |
return $self->string_length_sanitization( $serie, 19 ); |
| 173 |
}, |
| 174 |
), |
| 175 |
'globalpack_customer_code' => array( |
| 176 |
'sanitize' => function ( $value ) use ( $self ) { |
| 177 |
return $self->string_length_sanitization( $value, 4 ); |
| 178 |
}, |
| 179 |
), |
| 180 |
); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Check if the current order is for Rest of the world. |
| 185 |
* |
| 186 |
* @return Boolean. |
| 187 |
*/ |
| 188 |
public function is_rest_of_world() { |
| 189 |
$to_country = $this->api_args['shipping_address']['country']; |
| 190 |
$to_state = $this->api_args['shipping_address']['state']; |
| 191 |
$destination = Utils::get_shipping_zone( $to_country, $to_state ); |
| 192 |
|
| 193 |
return ( 'ROW' === $destination ); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Check if the current order is for Rest of the world. |
| 198 |
* |
| 199 |
* @return Boolean. |
| 200 |
*/ |
| 201 |
public function is_europe() { |
| 202 |
$to_country = $this->api_args['shipping_address']['country']; |
| 203 |
$to_state = $this->api_args['shipping_address']['state']; |
| 204 |
$destination = Utils::get_shipping_zone( $to_country, $to_state ); |
| 205 |
|
| 206 |
return ( 'EU' === $destination ); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Use specific barcode types for some products. |
| 211 |
* |
| 212 |
* @param string $barcode_type Selected GlobalPack barcode type. |
| 213 |
* |
| 214 |
* @return string. |
| 215 |
*/ |
| 216 |
public function check_product_barcode_type( $barcode_type ) { |
| 217 |
$barcode_types = Mapping::products_custom_barcode_types(); |
| 218 |
$selected_options = array(); |
| 219 |
$backend_data = Utils::get_selected_label_features( $this->api_args['backend_data'] ); |
| 220 |
|
| 221 |
foreach ( $backend_data as $option => $value ) { |
| 222 |
if ( 'yes' === $value ) { |
| 223 |
$selected_options[] = $option; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
if ( ! empty( $selected_options ) ) { |
| 228 |
foreach ( $barcode_types as $type => $options_combinations ) { |
| 229 |
foreach ( $options_combinations as $combination ) { |
| 230 |
sort( $combination ); |
| 231 |
sort( $selected_options ); |
| 232 |
if ( $selected_options == $combination ) { |
| 233 |
return $type; |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
return $barcode_type; |
| 240 |
} |
| 241 |
} |
| 242 |
|