Guest User

Elenaptix

a guest
Feb 25th, 2020
142
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Function to check starting char of a string
  2. function startsWith($haystack, $needle){
  3. return $needle === '' || strpos($haystack, $needle) === 0;
  4. }
  5.  
  6.  
  7. // Custom function to display the Billing Address form to registration page
  8. function zk_add_billing_form_to_registration(){
  9. global $woocommerce;
  10. $checkout = $woocommerce->checkout();
  11. ?>
  12. <?php foreach ( $checkout->get_checkout_fields( 'billing' ) as $key => $field ) : ?>
  13.  
  14. <?php if($key!='billing_email'){
  15. woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
  16. } ?>
  17.  
  18. <?php endforeach;
  19. }
  20. add_action('woocommerce_register_form_start','zk_add_billing_form_to_registration');
  21.  
  22. // Custom function to save Usermeta or Billing Address of registered user
  23. function zk_save_billing_address($user_id){
  24. global $woocommerce;
  25. $address = $_POST;
  26. foreach ($address as $key => $field){
  27. if(startsWith($key,'billing_')){
  28. // Condition to add firstname and last name to user meta table
  29. if($key == 'billing_first_name' || $key == 'billing_last_name'){
  30. $new_key = explode('billing_',$key);
  31. update_user_meta( $user_id, $new_key[1], $_POST[$key] );
  32. }
  33. update_user_meta( $user_id, $key, $_POST[$key] );
  34. }
  35. }
  36.  
  37. }
  38. add_action('woocommerce_created_customer','zk_save_billing_address');
  39.  
  40.  
  41. // Registration page billing address form Validation
  42. function zk_validation_billing_address( $errors ) {
  43. $address = $_POST;
  44. foreach ($address as $key => $field) :
  45. if(startsWith($key,'billing_')){
  46. if($key == 'billing_country' && $field == ''){
  47. add_the_error($errors, $key, 'Country');
  48. }
  49. if($key == 'billing_first_name' && $field == ''){
  50. add_the_error($errors, $key, 'First Name');
  51. }
  52. if($key == 'billing_last_name' && $field == ''){
  53. add_the_error($errors, $key, 'Last Name');
  54. }
  55. if($key == 'billing_address_1' && $field == ''){
  56. add_the_error($errors, $key, 'Address');
  57. }
  58. if($key == 'billing_city' && $field == ''){
  59. add_the_error($errors, $key, 'City');
  60. }
  61. if($key == 'billing_state' && $field == ''){
  62. add_the_error($errors, $key, 'State');
  63. }
  64. if($key == 'billing_postcode' && $field == ''){
  65. add_the_error($errors, $key, 'Post Code');
  66. }
  67. if($key == 'billing_phone' && $field == ''){
  68. add_the_error($errors, $key, 'Phone Number');
  69. }
  70.  
  71. }
  72. endforeach;
  73.  
  74. return $errors;
  75. }
  76. add_filter( 'woocommerce_registration_errors', 'zk_validation_billing_address', 10 );
  77.  
  78. function add_the_error( $errors, $key, $field_name ) {
  79. $message = sprintf( __( '%s is a required field.', 'iconic' ), '<strong>' . $field_name . '</strong>' );
  80. $errors->add( $key, $message );
  81. }
  82.  
  83. add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_gateway_disable_shipping_326' );
  84.  
  85. function bbloomer_gateway_disable_shipping_326( $available_gateways ) {
  86.  
  87. if ( ! is_admin() ) {
  88.  
  89. $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
  90.  
  91. $chosen_shipping = $chosen_methods[0];
  92.  
  93. if ( isset( $available_gateways['cheque'] ) && 0 === strpos( $chosen_shipping, 'wc_pickup_store' ) ) {
  94. unset( $available_gateways['cheque'] );
  95. }
  96.  
  97. if ( isset( $available_gateways['cod'] ) && 0 === strpos( $chosen_shipping, 'flat_rate' ) ) {
  98. unset( $available_gateways['cod'] );
  99. }
  100.  
  101. }
  102.  
  103. return $available_gateways;
  104.  
  105. }
  106.  
  107.  
  108. add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_add_checkout_fee_for_gateway' );
  109.  
  110. function bbloomer_add_checkout_fee_for_gateway() {
  111. $chosen_gateway = WC()->session->get( 'chosen_payment_method' );
  112. if ( $chosen_gateway == 'cheque' ) {
  113. WC()->cart->add_fee( 'Αντικαταβολή', 5 );
  114. }
  115. }
  116.  
  117. // Part 2: reload checkout on payment gateway change
  118.  
  119. add_action( 'woocommerce_review_order_before_payment', 'bbloomer_refresh_checkout_on_payment_methods_change' );
  120.  
  121. function bbloomer_refresh_checkout_on_payment_methods_change(){
  122. ?>
  123. <script type="text/javascript">
  124. (function($){
  125. $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
  126. $('body').trigger('update_checkout');
  127. });
  128. })(jQuery);
  129. </script>
  130. <?php
  131. }
RAW Paste Data