htdat / woo-viet

The repo for the WordPress plugin "Woo Viet - WooCommerce for Vietnam"
https://wordpress.org/plugins/woo-viet/
37 stars 22 forks source link

Update city-select.js #44

Closed mrhuy92 closed 6 years ago

mrhuy92 commented 6 years ago

Cập nhật trạng thái giao hàng khi thay đổi Thành Phố

htdat commented 6 years ago

@mrhuy92 - cảm ơn bạn đã gửi PR. Bạn có thể giải thích rõ hơn là đây là issue gì và xảy ra ở đâu?

Longkt commented 6 years ago
update_checkout_action: function( args ) {
            if ( wc_checkout_form.xhr ) {
                wc_checkout_form.xhr.abort();
            }

            if ( $( 'form.checkout' ).length === 0 ) {
                return;
            }

            args = typeof args !== 'undefined' ? args : {
                update_shipping_method: true
            };

            var country          = $( '#billing_country' ).val(),
                state            = $( '#billing_state' ).val(),
                postcode         = $( 'input#billing_postcode' ).val(),
                city             = $( '#billing_city' ).val(),
                address          = $( 'input#billing_address_1' ).val(),
                address_2        = $( 'input#billing_address_2' ).val(),
                s_country        = country,
                s_state          = state,
                s_postcode       = postcode,
                s_city           = city,
                s_address        = address,
                s_address_2      = address_2,
                $required_inputs = $( wc_checkout_form.$checkout_form ).find( '.address-field.validate-required:visible' ),
                has_full_address = true;

            if ( $required_inputs.length ) {
                $required_inputs.each( function() {
                    if ( $( this ).find( ':input' ).val() === '' ) {
                        has_full_address = false;
                    }
                });
            }

            if ( $( '#ship-to-different-address' ).find( 'input' ).is( ':checked' ) ) {
                s_country        = $( '#shipping_country' ).val();
                s_state          = $( '#shipping_state' ).val();
                s_postcode       = $( 'input#shipping_postcode' ).val();
                s_city           = $( '#shipping_city' ).val();
                s_address        = $( 'input#shipping_address_1' ).val();
                s_address_2      = $( 'input#shipping_address_2' ).val();
            }

            var data = {
                security        : wc_checkout_params.update_order_review_nonce,
                payment_method  : wc_checkout_form.get_payment_method(),
                country         : country,
                state           : state,
                postcode        : postcode,
                city            : city,
                address         : address,
                address_2       : address_2,
                s_country       : s_country,
                s_state         : s_state,
                s_postcode      : s_postcode,
                s_city          : s_city,
                s_address       : s_address,
                s_address_2     : s_address_2,
                has_full_address: has_full_address,
                post_data       : $( 'form.checkout' ).serialize()
            };

            if ( false !== args.update_shipping_method ) {
                var shipping_methods = {};

                $( 'select.shipping_method, input[name^="shipping_method"][type="radio"]:checked, input[name^="shipping_method"][type="hidden"]' ).each( function() {
                    shipping_methods[ $( this ).data( 'index' ) ] = $( this ).val();
                } );

                data.shipping_method = shipping_methods;
            }

            $( '.woocommerce-checkout-payment, .woocommerce-checkout-review-order-table' ).block({
                message: null,
                overlayCSS: {
                    background: '#fff',
                    opacity: 0.6
                }
            });

            wc_checkout_form.xhr = $.ajax({
                type:       'POST',
                url:        wc_checkout_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'update_order_review' ),
                data:       data,
                success:    function( data ) {

                    // Reload the page if requested
                    if ( true === data.reload ) {
                        window.location.reload();
                        return;
                    }

                    // Remove any notices added previously
                    $( '.woocommerce-NoticeGroup-updateOrderReview' ).remove();

                    var termsCheckBoxChecked = $( '#terms' ).prop( 'checked' );

                    // Save payment details to a temporary object
                    var paymentDetails = {};
                    $( '.payment_box input' ).each( function() {
                        var ID = $( this ).attr( 'id' );

                        if ( ID ) {
                            if ( $.inArray( $( this ).attr( 'type' ), [ 'checkbox', 'radio' ] ) !== -1 ) {
                                paymentDetails[ ID ] = $( this ).prop( 'checked' );
                            } else {
                                paymentDetails[ ID ] = $( this ).val();
                            }
                        }
                    });

                    // Always update the fragments
                    if ( data && data.fragments ) {
                        $.each( data.fragments, function ( key, value ) {
                            $( key ).replaceWith( value );
                            $( key ).unblock();
                        } );
                    }

                    // Recheck the terms and conditions box, if needed
                    if ( termsCheckBoxChecked ) {
                        $( '#terms' ).prop( 'checked', true );
                    }

                    // Fill in the payment details if possible without overwriting data if set.
                    if ( ! $.isEmptyObject( paymentDetails ) ) {
                        $( '.payment_box input' ).each( function() {
                            var ID = $( this ).attr( 'id' );

                            if ( ID ) {
                                if ( $.inArray( $( this ).attr( 'type' ), [ 'checkbox', 'radio' ] ) !== -1 ) {
                                    $( this ).prop( 'checked', paymentDetails[ ID ] ).change();
                                } else if ( 0 === $( this ).val().length ) {
                                    $( this ).val( paymentDetails[ ID ] ).change();
                                }
                            }
                        });
                    }

                    // Check for error
                    if ( 'failure' === data.result ) {

                        var $form = $( 'form.checkout' );

                        // Remove notices from all sources
                        $( '.woocommerce-error, .woocommerce-message' ).remove();

                        // Add new errors returned by this event
                        if ( data.messages ) {
                            $form.prepend( '<div class="woocommerce-NoticeGroup woocommerce-NoticeGroup-updateOrderReview">' + data.messages + '</div>' );
                        } else {
                            $form.prepend( data );
                        }

                        // Lose focus for all fields
                        $form.find( '.input-text, select, input:checkbox' ).trigger( 'validate' ).blur();

                        wc_checkout_form.scroll_to_notices();
                    }

                    // Re-init methods
                    wc_checkout_form.init_payment_methods();

                    // Fire updated_checkout event.
                    $( document.body ).trigger( 'updated_checkout', [ data ] );
                }

            });
        },

https://github.com/woocommerce/woocommerce/blob/master/assets/js/frontend/checkout.js

Theo đoạn code trên thì country, state, city, address, postcode thay đổi trong billing field đều được trigger update_checkout. Do vậy theo mình thì thêm vào không cần thiết, bạn có thể mô tả thêm lỗi được không?

htdat commented 6 years ago

Thanks Long. Next time, please just refer to this link https://github.com/woocommerce/woocommerce/blob/3.3.5/assets/js/frontend/checkout.js#L235-L405

Closing this.