PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/www/wp-content/plugins/ithemes-exchange/lib/shipping/class.shipping.php

https://github.com/ArzuA/gitwordpress
PHP | 711 lines | 412 code | 81 blank | 218 comment | 46 complexity | de655d0167c213152f7e82f85b654a21 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * This class get initiated when a shipping add-on is enabled
  4. * @package IT_Exchagne
  5. * @since 1.4.0
  6. */
  7. class IT_Exchange_Shipping {
  8. function IT_Exchange_Shipping() {
  9. // We need to include the abstract methods class regardless
  10. include_once( dirname( __FILE__ ) . '/class-method.php' );
  11. include_once( dirname( __FILE__ ) . '/class-shipping-feature.php' );
  12. add_action( 'it_exchange_enabled_addons_loaded', array( $this, 'maybe_init' ) );
  13. }
  14. function maybe_init() {
  15. $enabled_shipping_addons = (boolean) it_exchange_get_enabled_addons( array( 'category' => 'shipping' ) );
  16. if ( !$enabled_shipping_addons )
  17. return;
  18. // Init core shipping features
  19. include_once( dirname( __FILE__ ) . '/shipping-features/init.php' );
  20. add_action( 'template_redirect', array( $this, 'update_cart_shipping_method' ), 99);
  21. add_action( 'it_exchange_print_general_settings_tab_links', array( $this, 'print_shipping_tab_link' ) );
  22. add_filter( 'it_exchange_general_settings_tab_callback_shipping', array( $this, 'register_settings_tab_callback' ) );
  23. // Setup purchase requirement
  24. add_action( 'init', array( $this, 'init_shipping_address_purchase_requirements' ) );
  25. //$this->init_shipping_address_purchase_requirements();
  26. // Template part filters
  27. add_filter( 'it_exchange_get_content_checkout_totals_elements', array( $this, 'add_shipping_to_template_totals_loops' ) );
  28. add_filter( 'it_exchange_get_super-widget-checkout_after-cart-items_loops', array( $this, 'add_shipping_address_to_sw_template_totals_loops' ) );
  29. add_filter( 'it_exchange_get_super-widget-checkout_after-cart-items_loops', array( $this, 'add_shipping_method_to_sw_template_totals_loops' ) );
  30. add_filter( 'it_exchange_get_super-widget-checkout_after-cart-items_loops', array( $this, 'add_shipping_to_template_totals_loops' ) );
  31. // Ajax Request to update shipping address
  32. add_action( 'it_exchange_processing_super_widget_ajax_update-shipping', array( $this, 'process_ajax_request' ) );
  33. // Process the update address request
  34. add_action( 'template_redirect', array( $this, 'process_update_address_request' ) );
  35. // Clear the cart address when the cart is cleared
  36. add_action( 'it_exchange_empty_shopping_cart', array( $this, 'clear_cart_address' ) );
  37. // Updates the general settings states field in the admin
  38. add_action( 'it_exchange_admin_country_states_sync_for_shipping-general', array( $this, 'update_general_settings_state_field' ) );
  39. // Enqueue the JS for the checkout page
  40. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_checkout_page_scripts' ) );
  41. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_settings_js' ) );
  42. // Add var to checkout header
  43. add_action( 'wp_head', array( $this, 'add_js_to_checkout_header' ) );
  44. // Adjusts the cart total
  45. add_filter( 'it_exchange_get_cart_total', array( $this, 'modify_shipping_total' ) );
  46. // Remove Shipping information from cart data when cart is emptied or when item is added to cart
  47. add_action( 'it_exchange_empty_shopping_cart', array( $this, 'clear_cart_shipping_data' ) );
  48. add_action( 'it_exchange_add_cart_product', array( $this, 'clear_cart_shipping_data' ) );
  49. add_action( 'it_exchange_delete_cart_product', array( $this, 'clear_cart_shipping_data' ) );
  50. add_action( 'it_exchange_shipping_address_updated', array( $this, 'clear_cart_shipping_method' ) );
  51. }
  52. /**
  53. * Init Shipping Address Purchase Requirement
  54. *
  55. */
  56. function init_shipping_address_purchase_requirements() {
  57. if ( is_admin() )
  58. return;
  59. $this->register_shipping_address_purchase_requirement();
  60. $this->register_shipping_method_purchase_requirement();
  61. }
  62. /**
  63. * Registers the shipping address purchase requirement
  64. *
  65. * Use the it_exchange_register_purchase_requirement function to tell exchange
  66. * that your add-on requires certain conditionals to be set prior to purchase.
  67. * For more details see api/misc.php
  68. *
  69. * @since 1.4.0
  70. * @return void
  71. */
  72. function register_shipping_address_purchase_requirement() {
  73. // User must have a shipping address to purchase
  74. $properties = array(
  75. 'requirement-met' => 'it_exchange_get_customer_shipping_address', // This is a PHP callback
  76. 'sw-template-part' => 'shipping-address',
  77. 'checkout-template-part' => 'shipping-address',
  78. 'notification' => __( 'You must enter a shipping address before you can checkout', 'it-l10n-ithemes-exchange' ),
  79. 'priority' => 5.12
  80. );
  81. if ( it_exchange_get_available_shipping_methods_for_cart_products() )
  82. it_exchange_register_purchase_requirement( 'shipping-address', $properties );
  83. }
  84. /**
  85. * Registers the shipping method purchase requirement
  86. *
  87. * Use the it_exchange_register_purchase_requirement function to tell exchange
  88. * that your add-on requires certain conditionals to be set prior to purchase.
  89. * For more details see api/misc.php
  90. *
  91. * @since 1.4.0
  92. * @return void
  93. */
  94. function register_shipping_method_purchase_requirement() {
  95. // User must have a shipping address to purchase
  96. $properties = array(
  97. 'requirement-met' => 'it_exchange_get_cart_shipping_method', // This is a PHP callback
  98. 'sw-template-part' => 'shipping-method',
  99. 'checkout-template-part' => 'shipping-method',
  100. 'notification' => __( 'You must select a shipping method before you can checkout', 'it-l10n-ithemes-exchange' ),
  101. 'priority' => 5.13,
  102. );
  103. if ( it_exchange_get_available_shipping_methods_for_cart_products() )
  104. it_exchange_register_purchase_requirement( 'shipping-method', $properties );
  105. }
  106. /**
  107. * Prints the Shipping tab on the Exchange Settings admin page
  108. *
  109. * @since 1.4.0
  110. *
  111. * @param string $current_tab the current tab being requested
  112. * @return void
  113. */
  114. function print_shipping_tab_link( $current_tab ) {
  115. $active = 'shipping' == $current_tab ? 'nav-tab-active' : '';
  116. ?><a class="nav-tab <?php echo $active; ?>" href="<?php echo admin_url( 'admin.php?page=it-exchange-settings&tab=shipping' ); ?>"><?php _e( 'Shipping', 'it-l10n-ithemes-exchange' ); ?></a><?php
  117. }
  118. /**
  119. * Register the callback for the settings page
  120. *
  121. * I hate that this was setup like this. Seems like an uneeded function
  122. *
  123. * @since 1.4.0
  124. *
  125. * @return string the callback
  126. */
  127. function register_settings_tab_callback() {
  128. return array( $this, 'print_shipping_tab' );
  129. }
  130. /**
  131. * Prints the contents of the Shipping Tab
  132. *
  133. * First looks to see if a registered shipping provider's settins are being requested
  134. * If so, it inits those fields.
  135. * If not, it loads the general shipping settings
  136. *
  137. * @since 1.4.0
  138. *
  139. * @return void
  140. */
  141. function print_shipping_tab() {
  142. $settings = it_exchange_get_option( 'addon_shipping', true );
  143. ?>
  144. <div class="wrap">
  145. <?php
  146. ITUtility::screen_icon( 'it-exchange' );
  147. // Print Admin Settings Tabs
  148. $GLOBALS['IT_Exchange_Admin']->print_general_settings_tabs();
  149. // Print shipping provider tabs
  150. $this->print_provider_settings_tabs();
  151. // Print active shipping page
  152. $provider = ( ! empty( $_GET['provider'] ) && it_exchange_is_shipping_provider_registered( $_GET['provider'] ) ) ? it_exchange_get_registered_shipping_provider( $_GET['provider'] ) : 'shipping-general';
  153. $prefix = is_object( $provider ) ? $provider->slug : 'shipping-general';
  154. $action = add_query_arg( array( 'page' => 'it-exchange-settings', 'tab' => 'shipping' ), admin_url( 'admin.php' ) );
  155. $action = is_object( $provider ) ? add_query_arg( array( 'provider' => $provider->slug ), $action ) : $action;
  156. $fields = is_object( $provider ) ? $provider->provider_settings : $this->get_general_settings_fields();
  157. $country_states_js = is_object( $provider ) ? $provider->country_states_js : $this->get_general_settings_country_states_js();
  158. // Set admin setting form class options
  159. $options = array(
  160. 'prefix' => $prefix,
  161. 'form-options' => array(
  162. 'action' => $action,
  163. 'country-states-js' => $country_states_js,
  164. ),
  165. 'form-fields' => $fields,
  166. );
  167. it_exchange_print_admin_settings_form( $options );
  168. ?>
  169. </div>
  170. <?php
  171. }
  172. /**
  173. * Prints the tabs for all registered shipping providers
  174. *
  175. * @since 1.4.0
  176. *
  177. * @return html
  178. */
  179. function print_provider_settings_tabs() {
  180. // Return empty string if there aren't any registered shipping providers
  181. if ( ! $providers = it_exchange_get_registered_shipping_providers() )
  182. return '';
  183. // Set the currently requested shipping provider tab. Defaults to General
  184. $current = empty( $_GET['provider'] ) ? false : $_GET['provider'];
  185. $current = ( ! empty( $current ) && ! it_exchange_is_shipping_provider_registered( $current ) ) ? false : $current;
  186. // Print the HTML
  187. ?>
  188. <div class="it-exchange-secondary-tabs it-exchange-shipping-provider-tabs">
  189. <a class="shipping-provider-link <?php echo ( empty( $current ) ) ? 'it-exchange-current' : ''; ?>" href="<?php esc_attr_e( add_query_arg( array( 'page' => 'it-exchange-settings', 'tab' => 'shipping' ), admin_url( 'admin.php' ) ) ); ?>">
  190. <?php _e( 'General', 'it-l10n-ithemes-exchange' ); ?>
  191. </a>
  192. <?php
  193. foreach( $providers as $provider ) {
  194. $provider = it_exchange_get_registered_shipping_provider( $provider['slug'] );
  195. if ( empty( $provider->has_settings_page ) )
  196. continue;
  197. $url = add_query_arg( array( 'page' => 'it-exchange-settings', 'tab' => 'shipping', 'provider' => $provider->get_slug() ), admin_url( 'admin.php' ) );
  198. ?><a class="shipping-provider-link<?php echo ( $current == $provider->get_slug() ) ? ' it-exchange-current' : ''; ?>" href="<?php echo $url; ?>"><?php esc_html_e( $provider->get_label() ); ?></a><?php
  199. }
  200. ?>
  201. </div>
  202. <?php
  203. }
  204. /**
  205. * This returns the country-state-js options for general settings
  206. *
  207. * @since 1.4.0
  208. *
  209. * @return array
  210. */
  211. function get_general_settings_country_states_js() {
  212. $country_state_option = array(
  213. 'country-id' => 'product-ships-from-country',
  214. 'states-id' => '#product-ships-from-state',
  215. 'states-wrapper' => '#product-ships-from-state-wrapper',
  216. );
  217. return $country_state_option;
  218. }
  219. /**
  220. * This returns the settings fields array for general shipping settings
  221. *
  222. * @since 1.4.0
  223. *
  224. * @return array
  225. */
  226. function get_general_settings_fields() {
  227. $form_fields = array(
  228. array(
  229. 'type' => 'heading',
  230. 'label' => __( 'General Shipping Settings', 'it-l10n-ithemes-exchange' ),
  231. 'slug' => 'general-shipping-label',
  232. ),
  233. );
  234. $from_address = array();
  235. $shipping_methods = it_exchange_get_registered_shipping_methods();
  236. $features = array();
  237. foreach( (array) $shipping_methods as $method => $class ) {
  238. $method = it_exchange_get_registered_shipping_method( $method );
  239. if ( ! empty( $method->shipping_features ) && $method->enabled ) {
  240. foreach( $method->shipping_features as $feature ) {
  241. $features[$feature] = $feature;
  242. }
  243. }
  244. }
  245. if ( in_array( 'core-from-address', $features ) ) {
  246. $from_address = array(
  247. array(
  248. 'type' => 'text_box',
  249. 'label' => __( 'Products Ship From', 'it-l10n-ithemes-exchange' ),
  250. 'slug' => 'product-ships-from-address1',
  251. 'tooltip' => __( 'The default from address used when shipping your products.', 'it-l10n-ithemes-exchange' ),
  252. 'default' => '',
  253. 'options' => array(
  254. 'class' => 'large-text',
  255. 'placeholder' => __( 'Address 1', 'it-l10n-ithemes-exchange' ),
  256. ),
  257. ),
  258. array(
  259. 'type' => 'text_box',
  260. 'label' => '',
  261. 'slug' => 'product-ships-from-address2',
  262. 'default' => '',
  263. 'options' => array(
  264. 'class' => 'large-text',
  265. 'placeholder' => __( 'Address 2', 'it-l10n-ithemes-exchange' ),
  266. ),
  267. ),
  268. array(
  269. 'type' => 'text_box',
  270. 'label' => '',
  271. 'slug' => 'product-ships-from-city',
  272. 'default' => '',
  273. 'options' => array(
  274. 'class' => 'large-text',
  275. 'placeholder' => __( 'City', 'it-l10n-ithemes-exchange' ),
  276. ),
  277. ),
  278. array(
  279. 'type' => 'drop_down',
  280. 'label' => '',
  281. 'slug' => 'product-ships-from-country',
  282. 'default' => 'US',
  283. 'options' => it_exchange_get_data_set( 'countries' ),
  284. ),
  285. array(
  286. 'type' => 'drop_down',
  287. 'label' => '',
  288. 'slug' => 'product-ships-from-state',
  289. 'default' => 'NC',
  290. 'options' => it_exchange_get_data_set( 'states', array( 'country' => 'US' ) ),
  291. ),
  292. array(
  293. 'type' => 'text_box',
  294. 'label' => '',
  295. 'slug' => 'product-ships-from-zip',
  296. 'default' => '',
  297. 'options' => array(
  298. 'class' => 'normal-text',
  299. 'placeholder' => __( 'Zip', 'it-l10n-ithemes-exchange' ),
  300. ),
  301. ),
  302. array(
  303. 'type' => 'yes_no_drop_down',
  304. 'label' => __( 'Can individual products override the default Ships From Address?', 'it-l10n-ithemes-exchange' ),
  305. 'slug' => 'products-can-override-ships-from',
  306. 'tooltip' => __( 'Selecting "yes" will place these fields on the Add/Edit product screen.', 'it-l10n-ithemes-exchange' ),
  307. 'default' => '1',
  308. ),
  309. );
  310. }
  311. $form_fields = array_merge( $form_fields, $from_address );
  312. $form_fields = array_merge( $form_fields, array(
  313. array(
  314. 'type' => 'yes_no_drop_down',
  315. 'label' => __( 'Can individual products override the global Shipping Methods setting?', 'it-l10n-ithemes-exchange' ),
  316. 'slug' => 'products-can-override-available-shipping-methods',
  317. 'tooltip' => __( 'Selecting "yes" will allow you to set available Shipping Methods for a product from it\'s Add/Edit product screen.', 'it-l10n-ithemes-exchange' ),
  318. 'default' => '0',
  319. ),
  320. ));
  321. $measurements = array();
  322. if ( in_array( 'core-weight-dimensions', $features ) ) {
  323. $measurements = array(
  324. array(
  325. 'type' => 'drop_down',
  326. 'label' => __( 'Measurements Format', 'it-l10n-ithemes-exchange' ),
  327. 'slug' => 'measurements-format',
  328. 'tooltip' => __( 'Use standard for lbs and inches. Use metric for kg and cm.', 'it-l10n-ithemes-exchange' ),
  329. 'default' => 'standard',
  330. 'options' => array(
  331. 'standard' => __( 'Standard', 'it-l10n-ithemes-exchange' ),
  332. 'metric' => __( 'Metric', 'it-l10n-ithemes-exchange' ),
  333. ),
  334. ),
  335. );
  336. }
  337. $form_fields = array_merge( $form_fields, $measurements );
  338. return $form_fields;
  339. }
  340. /**
  341. * Add Shipping to the content-cart totals and content-checkout loop
  342. *
  343. * @since 1.4.0
  344. *
  345. * @param array $elements list of existing elements
  346. * @return array
  347. */
  348. function add_shipping_to_template_totals_loops( $elements ) {
  349. // Abort of total number of shipping methods available to cart is 0
  350. if ( count( it_exchange_get_available_shipping_methods_for_cart() ) < 1 )
  351. return $elements;
  352. // Locate the discounts key in elements array (if it exists)
  353. $index = array_search( 'totals-savings', $elements );
  354. if ( false === $index )
  355. $index = count( $elements) -1;
  356. array_splice( $elements, $index, 0, 'totals-shipping' );
  357. return $elements;
  358. }
  359. /**
  360. * Add Shipping to the super-widget-checkout totals loop
  361. *
  362. * @since 1.4.0
  363. *
  364. * @param array $loops list of existing elements
  365. * @return array
  366. */
  367. function add_shipping_address_to_sw_template_totals_loops( $loops ) {
  368. // Abort of total number of shipping methods available to cart is 0
  369. if ( count( it_exchange_get_available_shipping_methods_for_cart() ) < 1 )
  370. return $loops;
  371. $index = array_search( 'billing-address', $loops );
  372. if ( false === $index )
  373. $index = -1;
  374. // Shipping Address
  375. array_splice( $loops, $index, 0, 'shipping-address' );
  376. return $loops;
  377. }
  378. /**
  379. * Add Shipping Method to the super-widget-checkout totals loop
  380. *
  381. * @since 1.4.0
  382. *
  383. * @param array $loops list of existing elements
  384. * @return array
  385. */
  386. function add_shipping_method_to_sw_template_totals_loops( $loops ) {
  387. // Abort of total number of shipping methods available to cart is 0
  388. if ( count( it_exchange_get_available_shipping_methods_for_cart() ) < 1 )
  389. return $loops;
  390. // Locate the Billing Address or discounts key in elements array (if it exists) and insert before
  391. $index = array_search( 'billing-address', $loops );
  392. $index = ( false === $index ) ? array_search( 'shipping-address', $loops ) : $index;
  393. if ( false === $index )
  394. $index = -1;
  395. else
  396. $index++;
  397. // Shipping Address
  398. array_splice( $loops, $index, 0, 'shipping-method' );
  399. return $loops;
  400. }
  401. /**
  402. * Process Adding the shipping address to the SW via ajax
  403. *
  404. * Processes the POST request. If data is good, it updates the DB (where we store the data)
  405. * permanantly as well as the session where we store it for the template part.
  406. *
  407. * @since 1.0.0
  408. *
  409. * @return void
  410. */
  411. function process_ajax_request() {
  412. // Parse data
  413. $name = empty( $_POST['shippingName'] ) ? false : $_POST['shippingName'];
  414. $address1 = empty( $_POST['shippingAddress1'] ) ? false : $_POST['shippingAddress1'];
  415. $address2 = empty( $_POST['shippingAddress2'] ) ? false : $_POST['shippingAddress2'];
  416. $city = empty( $_POST['shippingCity'] ) ? false : $_POST['shippingCity'];
  417. $state = empty( $_POST['shippingState'] ) ? false : $_POST['shippingState'];
  418. $zip = empty( $_POST['shippingZip'] ) ? false : $_POST['shippingZip'];
  419. $country = empty( $_POST['shippingCountry'] ) ? false : $_POST['shippingCountry'];
  420. $customer = empty( $_POST['shippingCustomer'] ) ? false : $_POST['shippingCustomer'];
  421. $required_fields = apply_filter( 'it_exchange_required_shipping_address_fields', array( 'name', 'address1', 'city', 'state', 'zip', 'country', 'customer' ) );
  422. $states = it_exchange_get_data_set( 'states', array( 'country' => $country ) );
  423. if ( empty( $states ) && $key = array_search( 'state', $required_fields ) ) {
  424. unset( $required_fields[$key] );
  425. }
  426. foreach( $required_fields as $field ) {
  427. if ( !$$field ) {
  428. $invalid = true;
  429. break;
  430. }
  431. }
  432. // Update object with what we have
  433. $address = compact( 'name', 'address1', 'address2', 'city', 'state', 'zip', 'country', 'customer' );
  434. it_exchange_update_cart_data( 'shipping-address', $address );
  435. unset( $address['customer'] );
  436. // Register fail or success
  437. if ( $invalid ) {
  438. it_exchange_add_message( 'error', __( 'Please fill out all required fields' ) );
  439. die('0');
  440. } else {
  441. it_exchange_save_shipping_address( $address, $customer );
  442. die('1');
  443. }
  444. }
  445. /**
  446. * Process Adding the shipping address to the checkout page via POST request
  447. *
  448. * Processes the POST request. If data is good, it updates the DB (where we store the data)
  449. * permanantly as well as the session where we store it for the template part.
  450. *
  451. * @since 1.0.0
  452. *
  453. * @return void
  454. */
  455. function process_update_address_request() {
  456. // Abandon if not processing
  457. if ( ! it_exchange_is_page( 'checkout' ) || empty( $_POST['it-exchange-shipping-add-address-from-checkout'] ) )
  458. return;
  459. // Parse data
  460. $name = empty( $_POST['it-exchange-addon-shipping-name'] ) ? false : $_POST['it-exchange-addon-shipping-name'];
  461. $address1 = empty( $_POST['it-exchange-addon-shipping-address-1'] ) ? false : $_POST['it-exchange-addon-shipping-address-1'];
  462. $address2 = empty( $_POST['it-exchange-addon-shipping-address-2'] ) ? false : $_POST['it-exchange-addon-shipping-address-2'];
  463. $city = empty( $_POST['it-exchange-addon-shipping-city'] ) ? false : $_POST['it-exchange-addon-shipping-city'];
  464. $state = empty( $_POST['it-exchange-addon-shipping-state'] ) ? false : $_POST['it-exchange-addon-shipping-state'];
  465. $zip = empty( $_POST['it-exchange-addon-shipping-zip'] ) ? false : $_POST['it-exchange-addon-shipping-zip'];
  466. $country = empty( $_POST['it-exchange-addon-shipping-country'] ) ? false : $_POST['it-exchange-addon-shipping-country'];
  467. $required_fields = apply_filter( 'it_exchange_required_shipping_address_fields', array( 'name', 'address1', 'city', 'state', 'zip', 'country' ) );
  468. $states = it_exchange_get_data_set( 'states', array( 'country' => $country ) );
  469. if ( empty( $states ) && $key = array_search( 'state', $required_fields ) ) {
  470. unset( $required_fields[$key] );
  471. }
  472. foreach( $required_fields as $field ) {
  473. if ( !$$field ) {
  474. $invalid = true;
  475. break;
  476. }
  477. }
  478. // Update object with what we have
  479. $address = compact( 'name', 'address1', 'address2', 'city', 'state', 'zip', 'country' );
  480. it_exchange_update_cart_data( 'shipping-address', $address );
  481. // Register fail or success
  482. if ( $invalid ) {
  483. it_exchange_add_message( 'error', __( 'Please fill out all required fields' ) );
  484. } else {
  485. it_exchange_save_shipping_address( $address );
  486. it_exchange_add_message( 'notice', __( 'Shipping Address Updated' ) );
  487. }
  488. }
  489. /**
  490. * Clears the shipping address value when the cart is emptied
  491. *
  492. * @since 1.1.0
  493. *
  494. * @return void
  495. */
  496. function clear_cart_address() {
  497. it_exchange_remove_cart_data( 'shipping-address' );
  498. }
  499. /**
  500. * Adjusts the cart total
  501. *
  502. * @since 1.0.0
  503. *
  504. * @param $total the total passed to us by Exchange.
  505. * @return
  506. */
  507. function modify_shipping_total( $total ) {
  508. $shipping = it_exchange_get_cart_shipping_cost( false, false );
  509. return $total + $shipping;
  510. }
  511. /**
  512. * Enqueue Checkout Page Javascript
  513. *
  514. *
  515. * @since 1.2.0
  516. *
  517. * @return void
  518. */
  519. function enqueue_checkout_page_scripts() {
  520. if ( it_exchange_is_page( 'checkout' ) ) {
  521. // Register select to autocomplte
  522. $script = ITUtility::get_url_from_file( dirname( dirname( __FILE__ ) ) . '/assets/js/jquery.select-to-autocomplete.min.js' );
  523. wp_register_script( 'jquery-select-to-autocomplete', $script, array( 'jquery', 'jquery-ui-autocomplete' ) );
  524. // Load Shipping Address purchase requirement JS on checkout page.
  525. $script = ITUtility::get_url_from_file( dirname( dirname( __FILE__ ) ) . '/assets/js/shipping-purchase-requirement.js' );
  526. wp_enqueue_script( 'it-exchange-shipping-purchase-requirement', $script, array( 'jquery', 'jquery-ui-autocomplete', 'it-exchange-country-states-sync', 'jquery-select-to-autocomplete' ), false, true );
  527. $style = ITUtility::get_url_from_file( dirname( dirname( __FILE__ ) ) . '/assets/styles/autocomplete.css' );
  528. wp_register_style( 'it-exchange-autocomplete-style', $style );
  529. wp_enqueue_style( 'it-exchange-autocomplete-style' );
  530. }
  531. }
  532. /**
  533. * Enqueue JS for settings page
  534. *
  535. * @since 1.4.0
  536. */
  537. function enqueue_settings_js() {
  538. $current_screen = get_current_screen();
  539. if ( ! empty( $current_screen->base ) && 'exchange_page_it-exchange-settings' == $current_screen->base && ! empty( $_GET['tab'] ) && 'shipping' == $_GET['tab'] ) {
  540. $script = ITUtility::get_url_from_file( dirname( dirname( __FILE__ ) ) . '/admin/js/settings-shipping.js' );
  541. wp_enqueue_script( 'it-exchange-settings-shipping', $script, array( 'jquery' ) );
  542. }
  543. }
  544. /**
  545. * This function hooks into the AJAX call generated in general settings for country/states sync
  546. *
  547. * @since 1.4.0
  548. *
  549. * @return void
  550. */
  551. function update_general_settings_state_field() {
  552. $base_country = empty( $_POST['ite_base_country_ajax'] ) ? false : $_POST['ite_base_country_ajax'];
  553. $base_state = empty( $_POST['ite_base_state_ajax'] ) ? '' : $_POST['ite_base_state_ajax'];
  554. $states = it_exchange_get_data_set( 'states', array( 'country' => $base_country ) );
  555. if ( empty( $states ) ) {
  556. ?>
  557. <input type="text" id="product-ships-from-state" name="shipping-general-product-ships-from-state" maxlength="3" placeholder="<?php _e( 'State', 'it-l10n-ithemes-exchange' ); ?>" class="small-text" value="<?php esc_attr_e( $base_state ); ?>" />&nbsp;
  558. <?php $open_tag = '<a href="http://en.wikipedia.org/wiki/ISO_3166-2" target="_blank">'; ?>
  559. <span class="description"><?php printf( __( 'Please use the 2-3 character %sISO 3166-2 Country Subdivision Code%s', 'it-l10n-ithemes-exchange' ), $open_tag, '</a>' ); ?></span>
  560. <?php
  561. } else {
  562. ?>
  563. <select id="product-ships-from-state" name="shipping-general-product-ships-from-state">
  564. <?php
  565. foreach( (array) $states as $key => $value ) {
  566. ?><option value="<?php esc_attr_e( $key ); ?>" <?php selected( $key, $base_state ); ?>><?php esc_html_e( $value ); ?></option><?php
  567. }
  568. ?></select><?php
  569. }
  570. die();
  571. }
  572. // Update cart shipping mehtod
  573. function update_cart_shipping_method() {
  574. if ( ! empty( $_GET['ite-checkout-refresh'] ) ) {
  575. $cart_product_id = empty( $_POST['cart-product-id'] ) ? false : $_POST['cart-product-id'];
  576. $shipping_method = empty( $_POST['shipping-method'] ) ? '0': $_POST['shipping-method'];
  577. if ( ! empty( $cart_product_id ) ) {
  578. it_exchange_update_multiple_shipping_method_for_cart_product( $cart_product_id, $shipping_method );
  579. it_exchange_get_template_part( 'content-checkout' );
  580. } else {
  581. it_exchange_update_cart_data('shipping-method', $shipping_method );
  582. it_exchange_get_template_part( 'content-checkout' );
  583. }
  584. die();
  585. }
  586. // TEMP LOGIC
  587. if ( isset( $_POST['it-exchange-shipping-method'] ) ) {
  588. it_exchange_update_cart_data( 'shipping-method', $_POST['it-exchange-shipping-method'] );
  589. it_exchange_add_message( 'notice', __( 'Shipping method updated', 'it-l10n-ithemes-exchange' ) );
  590. }
  591. }
  592. /**
  593. * Adds some JS vars to the header or the checkout page
  594. *
  595. * @since 1.4.0
  596. *
  597. * @return void
  598. */
  599. function add_js_to_checkout_header() {
  600. if ( ! it_exchange_is_page( 'checkout' ) )
  601. return;
  602. ?>
  603. <script type="text/javascript">
  604. var ITExchangeCheckoutRefreshAjaxURL = '<?php echo esc_js( site_url() ); ?>/?ite-checkout-refresh=1';
  605. </script>
  606. <?php
  607. }
  608. /**
  609. * Removes all cart_data related to shipping
  610. *
  611. * @since 1.4.0
  612. *
  613. * @return void
  614. */
  615. function clear_cart_shipping_data() {
  616. it_exchange_remove_cart_data( 'shipping-address' );
  617. it_exchange_remove_cart_data( 'shipping-method' );
  618. }
  619. /**
  620. * Removes teh cart shipping method
  621. *
  622. * @since 1.4.0
  623. *
  624. * @return void
  625. */
  626. function clear_cart_shipping_method() {
  627. it_exchange_remove_cart_data( 'shipping-method' );
  628. }
  629. }
  630. $IT_Exchange_Shipping = new IT_Exchange_Shipping();