Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Function to check starting char of a string
- function startsWith($haystack, $needle){
- return $needle === '' || strpos($haystack, $needle) === 0;
- }
- // Custom function to display the Billing Address form to registration page
- function zk_add_billing_form_to_registration(){
- global $woocommerce;
- $checkout = $woocommerce->checkout();
- ?>
- <?php foreach ( $checkout->get_checkout_fields( 'billing' ) as $key => $field ) : ?>
- <?php if($key!='billing_email'){
- woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
- } ?>
- <?php endforeach;
- }
- add_action('woocommerce_register_form_start','zk_add_billing_form_to_registration');
- // Custom function to save Usermeta or Billing Address of registered user
- function zk_save_billing_address($user_id){
- global $woocommerce;
- $address = $_POST;
- foreach ($address as $key => $field){
- if(startsWith($key,'billing_')){
- // Condition to add firstname and last name to user meta table
- if($key == 'billing_first_name' || $key == 'billing_last_name'){
- $new_key = explode('billing_',$key);
- update_user_meta( $user_id, $new_key[1], $_POST[$key] );
- }
- update_user_meta( $user_id, $key, $_POST[$key] );
- }
- }
- }
- add_action('woocommerce_created_customer','zk_save_billing_address');
- // Registration page billing address form Validation
- function zk_validation_billing_address( $errors ) {
- $address = $_POST;
- foreach ($address as $key => $field) :
- if(startsWith($key,'billing_')){
- if($key == 'billing_country' && $field == ''){
- add_the_error($errors, $key, 'Country');
- }
- if($key == 'billing_first_name' && $field == ''){
- add_the_error($errors, $key, 'First Name');
- }
- if($key == 'billing_last_name' && $field == ''){
- add_the_error($errors, $key, 'Last Name');
- }
- if($key == 'billing_address_1' && $field == ''){
- add_the_error($errors, $key, 'Address');
- }
- if($key == 'billing_city' && $field == ''){
- add_the_error($errors, $key, 'City');
- }
- if($key == 'billing_state' && $field == ''){
- add_the_error($errors, $key, 'State');
- }
- if($key == 'billing_postcode' && $field == ''){
- add_the_error($errors, $key, 'Post Code');
- }
- if($key == 'billing_phone' && $field == ''){
- add_the_error($errors, $key, 'Phone Number');
- }
- }
- endforeach;
- return $errors;
- }
- add_filter( 'woocommerce_registration_errors', 'zk_validation_billing_address', 10 );
- function add_the_error( $errors, $key, $field_name ) {
- $message = sprintf( __( '%s is a required field.', 'iconic' ), '<strong>' . $field_name . '</strong>' );
- $errors->add( $key, $message );
- }
- add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_gateway_disable_shipping_326' );
- function bbloomer_gateway_disable_shipping_326( $available_gateways ) {
- if ( ! is_admin() ) {
- $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
- $chosen_shipping = $chosen_methods[0];
- if ( isset( $available_gateways['cheque'] ) && 0 === strpos( $chosen_shipping, 'wc_pickup_store' ) ) {
- unset( $available_gateways['cheque'] );
- }
- if ( isset( $available_gateways['cod'] ) && 0 === strpos( $chosen_shipping, 'flat_rate' ) ) {
- unset( $available_gateways['cod'] );
- }
- }
- return $available_gateways;
- }
- add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_add_checkout_fee_for_gateway' );
- function bbloomer_add_checkout_fee_for_gateway() {
- $chosen_gateway = WC()->session->get( 'chosen_payment_method' );
- if ( $chosen_gateway == 'cheque' ) {
- WC()->cart->add_fee( 'Αντικαταβολή', 5 );
- }
- }
- // Part 2: reload checkout on payment gateway change
- add_action( 'woocommerce_review_order_before_payment', 'bbloomer_refresh_checkout_on_payment_methods_change' );
- function bbloomer_refresh_checkout_on_payment_methods_change(){
- ?>
- <script type="text/javascript">
- (function($){
- $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
- $('body').trigger('update_checkout');
- });
- })(jQuery);
- </script>
- <?php
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement