【问题标题】:Add notice with ajax when user select local pickup shipping method on WooCommerce cart and checkout pages当用户在 WooCommerce 购物车和结帐页面上选择本地取货方式时,使用 ajax 添加通知
【发布时间】:2021-09-17 05:06:13
【问题描述】:

我正在为客户制作一个与 WooCommerce 集成的插件,当在购物车和结帐页面上选择本地取货运输方式时,我需要添加一个通知,wc_add_notice

我尝试在woocommerce_shipping_method_chosen 操作中添加一个钩子,如下所示:

add_action("woocommerce_shipping_method_chosen", "local_pickup_notice", 10, 1);
function local_pickup_notice($chosen_method){
    $woocommerce = WC();
    $shipping_method = $woocommerce->session->get("chosen_shipping_methods")[0];

    if($shipping_method == "local_pickup"){
        wc_add_notice(__("Local pickup shipping", "myplugin"), "notice");
    }
}

但是,它没有用。选择本地取货时(以及更改送货方式时),购物车或结帐页面上不会显示任何通知。

谁能帮我解决这个问题?

-- 编辑 1--

我注意到woocommerce_shipping_method_chosen 没有被触发,所以我将动作挂钩更改为woocommerce_cart_calculate_fees。现在,动作被触发了,但仍然没有显示消息。

还尝试从wc_add_notice更改为wc_print_notice,但没有成功。

-- 编辑 2--

我的错误在于检查$shipping_method,因为我有两个local_pickup 选项。这样,存储在$shipping_method 上的字符串是local_pickup:<option_id> 而不仅仅是local_pickup。因此,将if 语句更改为使用strpos 而不是== 使其工作。

目前的结果是:

add_action("woocommerce_cart_calculate_fees", "local_pickup_notice", 10, 1);
function local_pickup_notice($cart){
    $woocommerce = WC();
    $shipping_method = $woocommerce->session->get("chosen_shipping_methods")[0];

    wc_clear_notices();

    if(strpos($shipping_method, "local_pickup") !== false){
        wc_add_notice(__("Local pickup shipping", "myplugin"), "notice");
    }
}

现在,它正在工作,但仅在加载购物车/结帐页面时。下一个挑战是在 AJAX 调用上进行这项工作,以便在用户更改运输方式时显示通知。

如果有人知道如何在 WooCommerce ajax 上添加通知,请告诉我

【问题讨论】:

标签: ajax woocommerce cart checkout shipping-method


【解决方案1】:

wc_add_notice 应该用在允许您处理自定义 AJAX 端点的钩子中。当然不是在woocommerce_cart_calculate_fees 钩子中,那个钩子是用于其他目的的。

要在用户选择本地取货作为送货方式时添加通知,您可以使用:

// jQuery - Ajax script
function action_wp_footer() {
    // Only cart & checkout pages
    if ( is_cart() || ( is_checkout() && ! is_wc_endpoint_url() ) ) {
        $js_variable = is_cart() ? 'wc_cart_params' : 'wc_checkout_params';

        // jQuery Ajax code
        ?>
        <script type="text/javascript">
        jQuery(function($) {
            if ( typeof <?php echo $js_variable; ?> === 'undefined' )
                return false;
            
            // Get chosen shipping method
            var c_s_m = $( 'input[name^="shipping_method"]:checked' ).val();
            
            // Function that sen the Ajax request
            function sendAjaxRequest( c_s_m ) {
                $.ajax({
                    type: 'POST',
                    url: <?php echo $js_variable; ?>.ajax_url,
                    data: {
                        'action': 'chosen_shipping_method',
                        'chosen_shipping_method_val': c_s_m
                    },
                    success: function ( result ) {
                        // Remove other notices
                        $( '.woocommerce-info' ).remove();
                        
                        // Display notices cart
                        $( '.woocommerce-cart-form' ).before( result );                     
                        
                        // Display notices checkout
                        $( 'form.checkout' ).before( result );
                        
                        // console.log(result); // Uncomment for testing
                    }
                });
            }
            
            // On ready (DOM loaded)
            sendAjaxRequest( c_s_m );

            // On live "change event"
            $( document.body ).on( 'change', 'input[name^="shipping_method"]', function() {
                sendAjaxRequest( $( this ).val() );
            });
        });
        </script>
        <?php
    }
}
add_action( 'wp_footer', 'action_wp_footer', 10, 0 );

// Php Ajax
function chosen_shipping_method_handler() { 
    if ( isset( $_POST['chosen_shipping_method_val'] ) ) {
        $shipping_method = sanitize_key( $_POST['chosen_shipping_method_val'] );
        
        if ( strpos( $shipping_method, 'local_pickup' ) !== false ) {           
            // Add notice
            wc_add_notice( __( 'Local pickup shipping', 'woocommerce' ), 'notice' );
        }
    }
    
    // Return printed notices to jQuery response.
    wc_print_notices();
    
    // Alway at the end (to avoid server error 500)
    die();
}
add_action( 'wp_ajax_chosen_shipping_method', 'chosen_shipping_method_handler' );
add_action( 'wp_ajax_nopriv_chosen_shipping_method', 'chosen_shipping_method_handler' );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    • 2018-05-08
    • 2018-01-24
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    相关资源
    最近更新 更多