WC_GZD_Checkout

The WooCommerce Germanized WC GZD Checkout class.

Defined (1)

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

/includes/class-wc-gzd-checkout.php  
  1. class WC_GZD_Checkout { 
  2.  
  3. public $custom_fields = array(); 
  4. public $custom_fields_admin = array(); 
  5.  
  6. protected static $_instance = null; 
  7.  
  8. public static function instance() { 
  9. if ( is_null( self::$_instance ) ) 
  10. self::$_instance = new self(); 
  11. return self::$_instance; 
  12.  
  13. /** 
  14. * Cloning is forbidden. 
  15. * @since 1.0 
  16. */ 
  17. public function __clone() { 
  18. _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce-germanized' ), '1.0' ); 
  19.  
  20. /** 
  21. * Unserializing instances of this class is forbidden. 
  22. * @since 1.0 
  23. */ 
  24. public function __wakeup() { 
  25. _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce-germanized' ), '1.0' ); 
  26.  
  27. public function __construct() { 
  28. add_action( 'init', array( $this, 'init_fields' ) ); 
  29. add_filter( 'woocommerce_billing_fields', array( $this, 'set_custom_fields' ), 0, 1 ); 
  30. add_filter( 'woocommerce_shipping_fields', array( $this, 'set_custom_fields_shipping' ), 0, 1 ); 
  31. // Add Fields to Order Edit Page 
  32. add_filter( 'woocommerce_admin_billing_fields', array( $this, 'set_custom_fields_admin' ), 0, 1 ); 
  33. add_filter( 'woocommerce_admin_shipping_fields', array( $this, 'set_custom_fields_admin' ), 0, 1 ); 
  34. // Save Fields on order 
  35. add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'save_fields' ) ); 
  36. // Add Title to billing address format 
  37. add_filter( 'woocommerce_order_formatted_billing_address', array( $this, 'set_formatted_billing_address' ), 0, 2 ); 
  38. add_filter( 'woocommerce_order_formatted_shipping_address', array( $this, 'set_formatted_shipping_address' ), 0, 2 ); 
  39. add_filter( 'woocommerce_formatted_address_replacements', array( $this, 'set_formatted_address' ), 0, 2 ); 
  40. // Add item desc to order 
  41. add_action( 'woocommerce_order_add_product', array( $this, 'set_order_meta' ), 0, 5 ); 
  42. add_filter( 'woocommerce_hidden_order_itemmeta', array( $this, 'set_order_meta_hidden' ), 0 ); 
  43. // Deactivate checkout shipping selection 
  44. add_action( 'woocommerce_review_order_before_shipping', array( $this, 'remove_shipping_rates' ), 0 ); 
  45. // Add better fee taxation 
  46. add_action( 'woocommerce_cart_calculate_fees', array( $this, 'do_fee_tax_calculation' ), PHP_INT_MAX, 1 ); 
  47. // Disallow user order cancellation 
  48. if ( get_option( 'woocommerce_gzd_checkout_stop_order_cancellation' ) == 'yes' ) { 
  49. add_filter( 'woocommerce_get_cancel_order_url', array( $this, 'cancel_order_url' ), PHP_INT_MAX, 1 ); 
  50. add_filter( 'woocommerce_get_cancel_order_url_raw', array( $this, 'cancel_order_url' ), PHP_INT_MAX, 1 ); 
  51. add_filter( 'user_has_cap', array( $this, 'disallow_user_order_cancellation' ), 15, 3 ); 
  52. // Free Shipping auto select 
  53. if ( get_option( 'woocommerce_gzd_display_checkout_free_shipping_select' ) == 'yes' ) 
  54. add_filter( 'woocommerce_package_rates', array( $this, 'free_shipping_auto_select' ) ); 
  55.  
  56. // Pay for order 
  57. add_action( 'wp', array( $this, 'force_pay_order_redirect' ), 15 ); 
  58.  
  59. public function force_pay_order_redirect() { 
  60.  
  61. global $wp; 
  62.  
  63. if ( is_wc_endpoint_url( 'order-pay' ) && isset( $_GET['force_pay_order'] ) ) { 
  64.  
  65. // Manipulate $_POST 
  66. $order_key = $_GET['key']; 
  67. $order_id = absint( $wp->query_vars[ 'order-pay' ] ); 
  68. $order = wc_get_order( $order_id ); 
  69.  
  70. if ( ! $order || $order->order_key != $order_key ) 
  71. return; 
  72.  
  73. // Check if gateway is available - otherwise don't force redirect - would lead to errors in pay_action 
  74. $gateways = WC()->payment_gateways->get_available_payment_gateways(); 
  75.  
  76. if ( ! isset( $gateways[ $order->payment_method ] ) ) 
  77. return; 
  78.  
  79. // Hide terms checkbox 
  80. add_filter( 'woocommerce_germanized_checkout_show_terms', array( $this, 'disable_terms_order_pay' ) ); 
  81.  
  82. // Set $_POST to disable double payment method selection -> redirect by WC_Form_Handler::pay_action() 
  83. $_POST['woocommerce_pay'] = 1; 
  84. $_POST['_wpnonce'] = wp_create_nonce( 'woocommerce-pay' ); 
  85. $_POST['terms'] = 1; 
  86. $_POST['payment_method'] = $order->payment_method; 
  87.  
  88.  
  89.  
  90. public function disable_terms_order_pay( $show ) { 
  91. return false; 
  92.  
  93. public function free_shipping_auto_select( $rates ) { 
  94.  
  95. if ( ! is_checkout() && ! is_cart() ) 
  96. return $rates; 
  97.  
  98. $keep = array(); 
  99. $hide = false; 
  100.  
  101. // Legacy Support 
  102. if ( isset( $rates[ 'free_shipping' ] ) ) { 
  103. $keep[] = 'free_shipping'; 
  104. $hide = true; 
  105.  
  106. // Check for cost-free shipping 
  107. foreach ( $rates as $key => $rate ) { 
  108.  
  109. // Do only hide if free_shipping exists 
  110. if ( strpos( $key, 'free_shipping' ) !== false ) { 
  111. $hide = true; 
  112.  
  113. if ( $rate->cost == 0 ) { 
  114. $keep[] = $key; 
  115.  
  116. // Unset all other rates 
  117. if ( ! empty( $keep ) && $hide ) { 
  118.  
  119. // Unset chosen shipping method to avoid key errors 
  120. unset( WC()->session->chosen_shipping_methods ); 
  121.  
  122. foreach ( $rates as $key => $rate ) { 
  123.  
  124. if ( ! in_array( $key, $keep ) ) 
  125. unset( $rates[ $key ] ); 
  126.  
  127. return $rates; 
  128.  
  129. public function add_payment_link( $order_id ) { 
  130.  
  131. if ( get_option( 'woocommerce_gzd_order_pay_now_button' ) === 'no' ) 
  132. return false; 
  133.  
  134. $order = wc_get_order( $order_id ); 
  135.  
  136. if ( ! $order->needs_payment() ) 
  137. return; 
  138.  
  139. wc_get_template( 'order/order-pay-now-button.php', array( 'url' => add_query_arg( array( 'force_pay_order' => true ), $order->get_checkout_payment_url() ), 'order_id' => $order_id ) ); 
  140.  
  141. public function disallow_user_order_cancellation( $allcaps, $caps, $args ) { 
  142. if ( isset( $caps[0] ) ) { 
  143. switch ( $caps[0] ) { 
  144. case 'cancel_order' : 
  145. $allcaps['cancel_order'] = false; 
  146. break; 
  147. return $allcaps; 
  148.  
  149. public function cancel_order_url( $url ) { 
  150.  
  151. // Default to home url 
  152. $return = get_permalink( wc_get_page_id( 'shop' ) ); 
  153.  
  154. // Extract order id and use order success page as return url 
  155. $search = preg_match( '/order_id=([0-9]+)/', $url, $matches ); 
  156.  
  157. if ( $search && isset( $matches[1] ) ) { 
  158. $order_id = absint( $matches[1] ); 
  159. $order = wc_get_order( $order_id ); 
  160. $return = $order->get_checkout_order_received_url(); 
  161.  
  162. return $return; 
  163.  
  164. public function init_fields() { 
  165. if ( get_option( 'woocommerce_gzd_checkout_address_field' ) == 'yes' ) { 
  166.  
  167. $this->custom_fields[ 'title' ] = array( 
  168. 'type' => 'select',  
  169. 'required' => 1,  
  170. 'label' => __( 'Title', 'woocommerce-germanized' ),  
  171. 'options' => apply_filters( 'woocommerce_gzd_title_options', array( 1 => __( 'Mr.', 'woocommerce-germanized' ), 2 => __( 'Ms.', 'woocommerce-germanized' ) ) ),  
  172. 'before' => 'first_name',  
  173. 'group' => array( 'billing', 'shipping' ),  
  174. ); 
  175.  
  176. $this->custom_fields_admin[ 'title' ] = array( 
  177. 'before' => 'first_name',  
  178. 'type' => 'select',  
  179. 'options' => array( 1 => __( 'Mr.', 'woocommerce-germanized' ), 2 => __( 'Ms.', 'woocommerce-germanized' ) ),  
  180. 'show' => false,  
  181. 'label' => __( 'Title', 'woocommerce-germanized' ),  
  182. ); 
  183.  
  184.  
  185. if ( get_option( 'woocommerce_gzd_checkout_phone_required' ) == 'no' ) { 
  186.  
  187. $this->custom_fields[ 'phone' ] = array( 
  188. 'before' => '',  
  189. 'override' => true,  
  190. 'required' => false,  
  191. 'group' => array( 'billing' ) 
  192. ); 
  193.  
  194.  
  195. /** 
  196. * Recalculate fee taxes to split tax based on different tax rates contained within cart 
  197. *  
  198. * @param WC_Cart $cart 
  199. */ 
  200. public function do_fee_tax_calculation( WC_Cart $cart ) { 
  201. if ( get_option( 'woocommerce_gzd_fee_tax' ) != 'yes' ) 
  202. return; 
  203. if ( ! empty( $cart->fees ) ) { 
  204. $tax_shares = wc_gzd_get_cart_tax_share( 'fee' ); 
  205. foreach ( $cart->fees as $key => $fee ) { 
  206. if ( ! $fee->taxable && get_option( 'woocommerce_gzd_fee_tax_force' ) != 'yes' ) 
  207. continue;  
  208. // Calculate gross price if necessary 
  209. if ( $fee->taxable ) { 
  210. $fee_tax_rates = WC_Tax::get_rates( $fee->tax_class ); 
  211. $fee_tax = WC_Tax::calc_tax( $fee->amount, $fee_tax_rates, false ); 
  212. $fee->amount += array_sum( $fee_tax ); 
  213. // Set fee to nontaxable to avoid WooCommerce default tax calculation 
  214. $fee->taxable = false; 
  215. // Calculate tax class share 
  216. if ( ! empty( $tax_shares ) ) { 
  217. $fee_taxes = array(); 
  218. foreach ( $tax_shares as $rate => $class ) { 
  219. $tax_rates = WC_Tax::get_rates( $rate ); 
  220. $tax_shares[ $rate ][ 'fee_tax_share' ] = $fee->amount * $class[ 'share' ]; 
  221. $tax_shares[ $rate ][ 'fee_tax' ] = WC_Tax::calc_tax( ( $fee->amount * $class[ 'share' ] ), $tax_rates, true ); 
  222. $fee_taxes += $tax_shares[ $rate ][ 'fee_tax' ]; 
  223. foreach ( $tax_shares as $rate => $class ) { 
  224. $cart->fees[ $key ]->tax_data = $cart->fees[ $key ]->tax_data + $class[ 'fee_tax' ]; 
  225. // Add fee taxes to cart taxes 
  226. foreach ( array_keys( $cart->taxes + $fee_taxes ) as $sub ) { 
  227. $cart->taxes[ $sub ] = ( isset( $fee_taxes[ $sub ] ) ? $fee_taxes[ $sub ] : 0 ) + ( isset( $cart->taxes[ $sub ] ) ? $cart->taxes[ $sub ] : 0 ); 
  228. // Update fee 
  229. $cart->fees[ $key ]->tax = array_sum( $cart->fees[ $key ]->tax_data ); 
  230. $cart->fees[ $key ]->amount = $cart->fees[ $key ]->amount - $cart->fees[ $key ]->tax; 
  231.  
  232. /** 
  233. * Temporarily removes all shipping rates (except chosen one) from packages to only show chosen package within checkout.  
  234. */ 
  235. public function remove_shipping_rates() { 
  236. if ( get_option( 'woocommerce_gzd_display_checkout_shipping_rate_select' ) == 'no' ) 
  237. return; 
  238. $packages = WC()->shipping->get_packages(); 
  239. foreach ( $packages as $i => $package ) { 
  240. $chosen_method = isset( WC()->session->chosen_shipping_methods[ $i ] ) ? WC()->session->chosen_shipping_methods[ $i ] : ''; 
  241. if ( ! empty( $package[ 'rates' ] ) ) { 
  242. foreach ( $package[ 'rates' ] as $key => $rate ) { 
  243. if ( $key != $chosen_method ) 
  244. unset( WC()->shipping->packages[ $i ][ 'rates' ][ $key ] ); 
  245. }  
  246.  
  247. /** 
  248. * Adds product description to order meta 
  249. *  
  250. * @param int $order_id  
  251. * @param int $item_id  
  252. * @param object $product  
  253. * @param int $qty  
  254. * @param array $args  
  255. */ 
  256. public function set_order_meta( $order_id, $item_id, $product, $qty, $args ) { 
  257. wc_add_order_item_meta( $item_id, '_units', wc_gzd_get_gzd_product( $product )->get_product_units_html() ); 
  258. wc_add_order_item_meta( $item_id, '_delivery_time', wc_gzd_get_gzd_product( $product )->get_delivery_time_html() ); 
  259. wc_add_order_item_meta( $item_id, '_item_desc', wc_gzd_get_gzd_product( $product )->get_mini_desc() ); 
  260. wc_add_order_item_meta( $item_id, '_unit_price', wc_gzd_get_gzd_product( $product )->get_unit_html( false ) ); 
  261.  
  262. /** 
  263. * Hide product description from order meta default output 
  264. *  
  265. * @param array $metas 
  266. */ 
  267. public function set_order_meta_hidden( $metas ) { 
  268. array_push( $metas, '_item_desc' ); 
  269. array_push( $metas, '_units' ); 
  270. array_push( $metas, '_delivery_time' ); 
  271. array_push( $metas, '_unit_price' ); 
  272. return $metas; 
  273.  
  274. public function set_formatted_billing_address( $fields = array(), $order ) { 
  275.  
  276. if ( 'yes' !== get_option( 'woocommerce_gzd_checkout_address_field' ) ) 
  277. return $fields; 
  278.  
  279. if ( $order->billing_title ) 
  280. $fields[ 'title' ] = $this->get_customer_title( $order->billing_title ); 
  281. return $fields; 
  282.  
  283. public function set_formatted_shipping_address( $fields = array(), $order ) { 
  284.  
  285. if ( 'yes' !== get_option( 'woocommerce_gzd_checkout_address_field' ) ) 
  286. return $fields; 
  287.  
  288. if ( $order->shipping_title ) 
  289. $fields[ 'title' ] = $this->get_customer_title( $order->shipping_title ); 
  290. return $fields; 
  291.  
  292. public function get_customer_title( $option = 1 ) { 
  293. return ( 1 == $option ? __( 'Mr.', 'woocommerce-germanized' ) : __( 'Ms.', 'woocommerce-germanized' ) ); 
  294.  
  295. public function set_formatted_address( $placeholder, $args ) { 
  296. if ( isset( $args[ 'title' ] ) ) { 
  297. $placeholder[ '{title}' ] = $args[ 'title' ]; 
  298. $placeholder[ '{title_upper}' ] = strtoupper( $args[ 'title' ] ); 
  299. $placeholder[ '{name}' ] = $placeholder[ '{title}' ] . ' ' . $placeholder[ '{name}' ]; 
  300. $placeholder[ '{name_upper}' ] = $placeholder[ '{title_upper}' ] . ' ' . $placeholder[ '{name_upper}' ]; 
  301. return $placeholder; 
  302.  
  303. public function set_custom_fields( $fields = array(), $type = 'billing' ) { 
  304. $new = array(); 
  305. if ( ! empty( $this->custom_fields ) ) { 
  306. foreach ( $this->custom_fields as $key => $custom_field ) { 
  307. if ( in_array( $type, $custom_field[ 'group' ] ) ) { 
  308. if ( ! empty( $fields ) ) { 
  309. foreach ( $fields as $name => $field ) { 
  310. if ( $name == $type . '_' . $custom_field[ 'before' ] && ! isset( $custom_field[ 'override' ] ) ) 
  311. $new[ $type . '_' . $key ] = $custom_field; 
  312. $new[ $name ] = $field; 
  313. if ( $name == $type . '_' . $key && isset( $custom_field[ 'override' ] ) ) 
  314. $new[ $name ] = array_merge( $field, $custom_field ); 
  315. return ( ! empty( $new ) ? $new : $fields ); 
  316.  
  317. public function set_custom_fields_shipping( $fields ) { 
  318. return $this->set_custom_fields( $fields, 'shipping' ); 
  319.  
  320. public function set_custom_fields_admin( $fields = array() ) { 
  321. $new = array(); 
  322. if ( ! empty( $this->custom_fields_admin ) ) { 
  323. foreach ( $this->custom_fields_admin as $key => $custom_field ) { 
  324. if ( ! empty( $fields ) ) { 
  325. foreach ( $fields as $name => $field ) { 
  326. if ( $name == $custom_field[ 'before' ] && ! isset( $custom_field[ 'override' ] ) ) 
  327. $new[ $key ] = $custom_field; 
  328. $new[ $name ] = $field; 
  329. return ( ! empty( $new ) ? $new : $fields ); 
  330.  
  331. public function save_fields( $order_id ) { 
  332. $checkout = WC()->checkout(); 
  333. if ( ! empty( $this->custom_fields ) ) { 
  334. foreach ( $this->custom_fields as $key => $custom_field ) { 
  335. if ( ! empty( $custom_field[ 'group' ] ) && ! isset( $custom_field[ 'override' ] ) ) { 
  336. foreach ( $custom_field[ 'group' ] as $group ) { 
  337. $val = ( isset( $checkout->posted[ $group . '_' . $key ] ) ? $checkout->posted[ $group . '_' . $key ] : '' ); 
  338. if ( $group == 'shipping' || $group == 'billing' ) 
  339. $val = $checkout->get_posted_address_data( $key, $group ); 
  340. if ( ! empty( $val ) ) 
  341. update_post_meta( $order_id, '_' . $group . '_' . $key, sanitize_text_field( $val ) ); 
  342.