WC_GZD_DHL_Parcel_Shops

The WooCommerce Germanized WC GZD DHL Parcel Shops class.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-gzd-dhl-parcel-shops.php  
  1. class WC_GZD_DHL_Parcel_Shops { 
  2.  
  3. /** 
  4. * Single instance of WC_GZD_DHL_Parcel_Shops Class 
  5. * @var object 
  6. */ 
  7. protected static $_instance = null; 
  8.  
  9. public static function instance( $plugin = null ) { 
  10. if ( is_null( self::$_instance ) ) { 
  11. self::$_instance = new self( $plugin ); 
  12.  
  13. return self::$_instance; 
  14.  
  15. public function __construct() { 
  16.  
  17. $this->address_hooks(); 
  18.  
  19. if ( $this->is_enabled() ) { 
  20.  
  21. // Register fields before WC_GZD_Checkout is loaded 
  22. add_filter( 'woocommerce_gzd_custom_checkout_fields', array( $this, 'init_fields' ), 10, 1 ); 
  23. add_filter( 'woocommerce_gzd_custom_checkout_admin_fields', array( $this, 'init_admin_fields' ), 10, 1 ); 
  24.  
  25. add_action( 'woocommerce_gzd_registered_scripts', array( $this, 'load_scripts' ), 10, 3 ); 
  26. add_action( 'woocommerce_gzd_localized_scripts', array( $this, 'localize_scripts' ), 10, 1 ); 
  27.  
  28. add_action( 'woocommerce_checkout_process', array( $this, 'manipulate_checkout_fields' ), 10 ); 
  29. add_action( 'woocommerce_after_checkout_validation', array( $this, 'validate_fields' ), 10, 1 ); 
  30.  
  31. // My Account Edit Address 
  32. add_filter( 'woocommerce_shipping_fields', array( $this, 'manipulate_address_fields' ), 20, 1 ); 
  33. add_filter( 'woocommerce_process_myaccount_field_shipping_parcelshop', array( $this, 'validate_address_fields' ), 10, 1 ); 
  34.  
  35. // Customer fields 
  36. add_filter( 'woocommerce_customer_meta_fields', array( $this, 'init_profile_fields' ), 10, 1 ); 
  37.  
  38. add_action( 'woocommerce_before_checkout_form', array( $this, 'maybe_hide_fields_before_rendering' ), 10, 1 ); 
  39.  
  40. if ( $this->is_finder_enabled() ) { 
  41. // Add Markup 
  42. add_filter( 'woocommerce_form_field_checkbox', array( $this, 'add_button_markup' ), 10, 4 ); 
  43. add_action( 'wp_footer', array( $this, 'add_overlay_markup' ), 50 ); 
  44.  
  45. public function maybe_hide_fields_before_rendering( $checkout ) { 
  46.  
  47. $hide_fields = false; 
  48. $chosen_shipping_methods = wc_gzd_get_chosen_shipping_rates(); 
  49.  
  50. foreach ( $chosen_shipping_methods as $rate ) { 
  51. if ( in_array( $rate->id, $this->get_disabled_shipping_methods() ) ) { 
  52. $hide_fields = true; 
  53.  
  54. if ( apply_filters( 'woocommerce_gzd_dhl_parcel_shops_hide_fields', $hide_fields, $this ) ) { 
  55. add_filter( 'woocommerce_checkout_fields', array( $this, 'remove_fields' ), 10, 1 ); 
  56.  
  57. public function get_disabled_shipping_methods() { 
  58.  
  59. if ( get_option( 'woocommerce_gzd_display_checkout_shipping_rate_select' ) !== 'yes' ) 
  60. return array(); 
  61.  
  62. return get_option( 'woocommerce_gzd_dhl_parcel_shop_disabled_shipping_methods', array() ); 
  63.  
  64. public function remove_fields( $fields ) { 
  65.  
  66. if ( isset( $fields[ 'shipping' ][ 'shipping_parcelshop' ] ) ) { 
  67. unset( $fields[ 'shipping' ][ 'shipping_parcelshop' ] ); 
  68.  
  69. return $fields; 
  70.  
  71. public function address_hooks() { 
  72. add_filter( 'woocommerce_order_formatted_shipping_address', array( $this, 'set_formatted_shipping_address' ), 20, 2 ); 
  73. add_filter( 'woocommerce_order_formatted_billing_address', array( $this, 'set_formatted_billing_address' ), 20, 2 ); 
  74. add_filter( 'woocommerce_formatted_address_replacements', array( $this, 'set_formatted_address' ), 20, 2 ); 
  75. add_filter( 'woocommerce_localisation_address_formats', array( $this, 'set_address_format' ), 20 ); 
  76. add_filter( 'woocommerce_my_account_my_address_formatted_address', array( $this, 'set_user_address' ), 10, 3 ); 
  77.  
  78. public function set_address_format( $formats ) { 
  79.  
  80. foreach( $this->get_supported_countries() as $country ) { 
  81.  
  82. if ( ! isset( $formats[ $country ] ) ) 
  83. continue; 
  84.  
  85. $format = $formats[ $country ]; 
  86. $format = str_replace( "{name}", "{name}\n{parcelshop_post_number}", $format ); 
  87. $formats[ $country ] = $format; 
  88.  
  89. return $formats; 
  90.  
  91. public function set_formatted_shipping_address( $fields = array(), $order ) { 
  92. $fields[ 'parcelshop_post_number' ] = ''; 
  93.  
  94. if ( wc_gzd_get_crud_data( $order, 'shipping_parcelshop' ) ) { 
  95. $fields[ 'parcelshop_post_number' ] = wc_gzd_get_crud_data( $order, 'shipping_parcelshop_post_number' ); 
  96.  
  97. return $fields; 
  98.  
  99. public function set_formatted_billing_address( $fields = array(), $order ) { 
  100. $fields[ 'parcelshop_post_number' ] = ''; 
  101.  
  102. return $fields; 
  103.  
  104. public function set_formatted_address( $placeholder, $args ) { 
  105. if ( isset( $args[ 'parcelshop_post_number' ] ) ) { 
  106. $placeholder[ '{parcelshop_post_number}' ] = $args[ 'parcelshop_post_number' ]; 
  107. $placeholder[ '{parcelshop_post_number_upper}' ] = strtoupper( $args[ 'parcelshop_post_number' ] ); 
  108. } else { 
  109. $placeholder[ '{parcelshop_post_number}' ] = ''; 
  110. $placeholder[ '{parcelshop_post_number_upper}' ] = ''; 
  111. return $placeholder; 
  112.  
  113. public function set_user_address( $address, $customer_id, $name ) { 
  114. if ( 'shipping' === $name ) { 
  115. if ( get_user_meta( $customer_id, $name . '_parcelshop', true ) ) { 
  116. $address[ 'parcelshop_post_number' ] = get_user_meta( $customer_id, $name . '_parcelshop_post_number', true ); 
  117. return $address; 
  118.  
  119. public function init_fields( $fields ) { 
  120.  
  121. $fields[ 'parcelshop' ] = array( 
  122. 'type' => 'checkbox',  
  123. 'required' => false,  
  124. 'label' => __( 'Send to DHL Parcel Shop?', 'woocommerce-germanized' ),  
  125. 'before' => 'address_1',  
  126. 'group' => array( 'shipping' ),  
  127. 'class' => array( 'form-row-wide', 'first-check' ),  
  128. 'hidden' => $this->maybe_hide_fields(),  
  129. ); 
  130.  
  131. $fields[ 'parcelshop_post_number' ] = array( 
  132. 'type' => 'text',  
  133. 'required' => true,  
  134. 'label' => __( 'Postnumber', 'woocommerce-germanized' ),  
  135. 'before' => 'address_1',  
  136. 'group' => array( 'shipping' ),  
  137. 'class' => array( 'form-row-wide' ),  
  138. 'hidden' => $this->maybe_hide_fields(),  
  139. ); 
  140.  
  141. return $fields; 
  142.  
  143. public function maybe_hide_fields() { 
  144. return apply_filters( 'woocommerce_gzd_dhl_parcel_shops_hide_fields', false, $this ); 
  145.  
  146. public function init_profile_fields( $fields ) { 
  147.  
  148. $fields[ 'shipping' ][ 'fields' ][ 'shipping_parcelshop' ] = array( 
  149. 'label' => __( 'DHL Parcel Shop?', 'woocommerce-germanized' ),  
  150. 'type' => 'select',  
  151. 'options' => array( 0 => __( 'No', 'woocommerce-germanized' ), 1 => __( 'Yes', 'woocommerce-germanized' ) ),  
  152. 'description' => __( 'Select whether delivery to parcel shop should be enabled.', 'woocommerce-germanized' ),  
  153. ); 
  154.  
  155. $fields[ 'shipping' ][ 'fields' ][ 'shipping_parcelshop_post_number' ] = array( 
  156. 'label' => __( 'Postnumber', 'woocommerce-germanized' ),  
  157. 'type' => 'text',  
  158. 'description' => __( 'In case delivery to parcel shop is enabled please fill in the corresponding DHL post number.', 'woocommerce-germanized' ),  
  159. ); 
  160.  
  161. return $fields; 
  162.  
  163.  
  164. public function init_admin_fields( $fields ) { 
  165.  
  166. $fields[ 'parcelshop_post_number' ] = array( 
  167. 'type' => 'text',  
  168. 'label' => __( 'Postnumber', 'woocommerce-germanized' ),  
  169. 'before' => 'address_1',  
  170. 'address_type' => 'shipping',  
  171. 'show' => false,  
  172. ); 
  173.  
  174. return $fields; 
  175.  
  176. public function is_enabled() { 
  177. return ( get_option( 'woocommerce_gzd_dhl_parcel_shops' ) === 'yes' ); 
  178.  
  179. public function is_finder_enabled() { 
  180. return ( get_option( 'woocommerce_gzd_dhl_parcel_shop_finder' ) === 'yes' ); 
  181.  
  182. public function manipulate_address_fields( $fields ) { 
  183. global $wp; 
  184.  
  185. if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) { 
  186. return $fields; 
  187.  
  188. if ( empty( $_POST['action'] ) || 'edit_address' !== $_POST['action'] || empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-edit_address' ) ) { 
  189. return $fields; 
  190.  
  191. $user_id = get_current_user_id(); 
  192.  
  193. if ( $user_id <= 0 ) { 
  194. return $fields; 
  195.  
  196. if ( ! isset( $_POST[ 'shipping_parcelshop' ] ) && isset( $fields[ 'shipping_parcelshop_post_number' ] ) ) { 
  197. $fields[ 'shipping_parcelshop_post_number' ][ 'required' ] = false; 
  198. } else { 
  199. $fields[ 'shipping_address_1' ][ 'label' ] = __( 'Parcel Shop', 'woocommerce-germanized' ); 
  200.  
  201. return $fields; 
  202.  
  203. public function validate_address_fields( $value ) { 
  204. if ( $value && ! empty( $value ) ) { 
  205. $data = array( 
  206. 'shipping_parcelshop_post_number' => '',  
  207. 'shipping_parcelshop' => '',  
  208. 'shipping_country' => '',  
  209. ); 
  210.  
  211. foreach( $data as $key => $val ) { 
  212. if ( isset( $_POST[ $key ] ) ) { 
  213. $data[ $key ] = sanitize_text_field( $_POST[ $key ] ); 
  214.  
  215. $this->validate_fields( $data ); 
  216.  
  217. return $value; 
  218.  
  219. public function manipulate_checkout_fields() { 
  220. if ( ! WC()->checkout()->get_value( 'shipping_parcelshop' ) ) { 
  221. $fields = WC()->checkout()->checkout_fields; 
  222. $fields[ 'shipping' ][ 'shipping_parcelshop_post_number' ][ 'required' ] = false; 
  223. WC()->checkout()->checkout_fields = $fields; 
  224. } else { 
  225. $fields = WC()->checkout()->checkout_fields; 
  226. $fields[ 'shipping' ][ 'shipping_address_1' ][ 'label' ] = __( 'Parcel Shop', 'wooocommerce-germanized' ); 
  227. WC()->checkout()->checkout_fields = $fields; 
  228.  
  229. public function validate_fields( $data ) { 
  230.  
  231. $required = array( 
  232. 'shipping_parcelshop_post_number',  
  233. 'shipping_parcelshop' 
  234. ); 
  235.  
  236. foreach( $required as $req ) { 
  237. if ( ! isset( $data[ $req ] ) || empty( $data[ $req ] ) ) 
  238. return; 
  239.  
  240. $is_valid_postnumber = (bool) preg_match( '/^([0-9]*)$/', $data[ 'shipping_parcelshop_post_number' ] ); 
  241.  
  242. if ( ! $is_valid_postnumber ) { 
  243. wc_add_notice( __( 'Your PostNumber should contain numbers only', 'woocommerce-germanized' ), 'error' ); 
  244.  
  245. $is_valid_shipping_country = in_array( $data[ 'shipping_country' ], $this->get_supported_countries() ); 
  246.  
  247. if ( ! $is_valid_shipping_country ) { 
  248. wc_add_notice( sprintf( __( 'Parcel Shop Delivery is only supported in: %s.', 'woocommerce-germanized' ), implode( ', ', $this->get_supported_countries( true ) ) ), 'error' ); 
  249.  
  250. public function load_scripts( $suffix, $frontend_script_path, $assets_path ) { 
  251.  
  252. if ( is_checkout() || is_wc_endpoint_url( 'edit-address' ) ) { 
  253.  
  254. $deps = array( 'jquery' ); 
  255.  
  256. if ( is_checkout() ) 
  257. array_push( $deps, 'wc-checkout' ); 
  258.  
  259. wp_enqueue_script( 'wc-gzd-checkout-dhl-parcel-shops', $frontend_script_path . 'checkout-dhl-parcel-shops' . $suffix . '.js', $deps, WC_GERMANIZED_VERSION, true ); 
  260.  
  261. if ( $this->is_finder_enabled() ) { 
  262. wp_register_style( 'wc-gzd-checkout-dhl-parcel-shops-finder', $assets_path . 'css/woocommerce-gzd-dhl-parcel-shop-finder' . $suffix . '.css', '', WC_GERMANIZED_VERSION, 'all' ); 
  263. wp_enqueue_style( 'wc-gzd-checkout-dhl-parcel-shops-finder' ); 
  264.  
  265. public function localize_scripts( $assets_path ) { 
  266.  
  267. $lang = substr( get_bloginfo( "language" ), 0, 2 ); 
  268.  
  269. if ( wp_script_is( 'wc-gzd-checkout-dhl-parcel-shops' ) ) { 
  270. wp_localize_script( 'wc-gzd-checkout-dhl-parcel-shops', 'wc_gzd_dhl_parcel_shops_params', apply_filters( 'wc_gzd_dhl_parcel_shops_params', array( 
  271. 'address_field_title' => __( 'Parcel Shop', 'woocommerce-germanized' ),  
  272. 'address_field_placeholder' => __( 'Parcel Shop', 'woocommerce-germanized' ),  
  273. 'supported_countries' => $this->get_supported_countries(),  
  274. 'enable_finder' => $this->is_finder_enabled(),  
  275. 'button_wrapper' => '#wc-gzd-parcel-shop-finder-button-wrapper',  
  276. 'iframe_wrapper' => '#wc-gzd-parcel-shop-finder-iframe-wrapper',  
  277. 'iframe_src' => '//parcelshopfinder.dhlparcel.com/partnerservice.html?setLng=' . $lang,  
  278. 'shipping_country_error' => sprintf( __( 'Parcel Shop Delivery is only supported in: %s.', 'woocommerce-germanized' ), implode( ', ', $this->get_supported_countries( true ) ) ),  
  279. ) ) ); 
  280.  
  281. public function add_overlay_markup() { 
  282. if ( is_checkout() || is_wc_endpoint_url( 'edit-address' ) ) { 
  283. wc_get_template( 'checkout/dhl-parcel-shop-finder.php' ); 
  284.  
  285. public function add_button_markup( $field, $key, $args, $value ) { 
  286.  
  287. if ( 'shipping_parcelshop' === $key ) { 
  288. if ( substr( $field, -4 ) === "</p>" ) { 
  289. $field = substr( $field, 0, -4 ); 
  290. $field .= apply_filters( 'woocommerce_gzd_dhl_parcel_finder_button_html', '<span id="wc-gzd-parcel-shop-finder-button-wrapper"><a class="wc-gzd-parcel-finder-open-button" href="#">' . __( 'Parcel Shop Finder', 'woocommerce-germanized' ) . '</a></span></p><div class="clear"></div>' ); 
  291.  
  292. return $field; 
  293.  
  294. public function get_supported_countries( $as_names = false ) { 
  295.  
  296. $codes = apply_filters( 'woocommerce_gzd_dhl_parcel_shops_countries', (array) get_option( 'woocommerce_gzd_dhl_parcel_shop_supported_countries', array( 'DE', 'AT' ) ) ); 
  297.  
  298. if ( $as_names ) { 
  299. $names = WC()->countries->get_countries(); 
  300. return array_intersect_key( $names, array_flip( $codes ) ); 
  301. } else { 
  302. return $codes;