【问题标题】:WooCommerce extra fee on shipping cost if checkbox ship to other address is enabled如果启用复选框运送到其他地址,WooCommerce 将收取额外的运费
【发布时间】:2020-01-22 05:50:33
【问题描述】:

是否有一个选项,例如,如果客户想要运送到其帐单邮寄地址以外的其他地址,是否可以根据该复选框收取额外费用?我希望如果客户取消选中该复选框,则会在运费中增加 12.50 欧元的额外费用。如果有人知道,请告诉我:)

复选框是默认的 woocommerce 复选框。

【问题讨论】:

    标签: php wordpress checkbox woocommerce shipping


    【解决方案1】:

    我已经通过在您自己的主题中将以下代码添加到 functions.php 来修复它:

    add_filter( 'woocommerce_form_field' , 'remove_order_comments_optional_fields_label', 10, 4 );
    function remove_order_comments_optional_fields_label( $field, $key, $args, $value ) {
        // Only on checkout page for Order notes field
        if( 'ship_to_different_address' === $key && is_checkout() ) {
            $optional = '&nbsp;<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
            $field = str_replace( $optional, '', $field );
        }
        return $field;
    }
    
    // Ajax / jQuery script
    add_action( 'wp_footer', 'ship_to_different_address_script' );
    function ship_to_different_address_script() {
        // On checkoutpage
        if( ( is_checkout() && ! is_wc_endpoint_url() ) ) :
            ?>
            <script type="text/javascript">
                jQuery( function($){
                    if (typeof woocommerce_params === 'undefined')
                        return false;
    
                    console.log('defined');
    
                    $('input[name=ship_to_different_address]').click( function(){
                        var fee = $(this).prop('checked') === true ? '1' : '';
    
                        $.ajax({
                            type: 'POST',
                            url: woocommerce_params.ajax_url,
                            data: {
                                'action': 'ship_to_different_address',
                                'ship_to_different_address': fee,
                            },
                            success: function (result) {
                                $('body').trigger('update_checkout');
                                console.log(result);
                            },
                        });
                    });
                });
            </script>
        <?php
        endif;
    }
    
    // Get the ajax request and set value to WC session
    add_action( 'wp_ajax_ship_to_different_address', 'get_ajax_ship_to_different_address' );
    add_action( 'wp_ajax_nopriv_ship_to_different_address', 'get_ajax_ship_to_different_address' );
    function get_ajax_ship_to_different_address() {
        if ( isset($_POST['ship_to_different_address']) ) {
            WC()->session->set('ship_to_different_address', ($_POST['ship_to_different_address'] ? '1' : '0') );
            echo WC()->session->get('ship_to_different_address');
        }
        die();
    }
    
    // Add / Remove a custom fee
    add_action( 'woocommerce_cart_calculate_fees', 'add_remove_ship_to_different_address', 10, 1 );
    function add_remove_ship_to_different_address( $cart )
    {
        // Only on checkout
        if ((is_admin() && !defined('DOING_AJAX')) || is_cart())
            return;
    
        $fee_amount = 12.50;
    
        if (WC()->session->get('ship_to_different_address'))
            $cart->add_fee(__('Shipping fee', 'woocommerce'), $fee_amount);
    }
    

    【讨论】:

      猜你喜欢
      • 2019-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-26
      • 2021-05-08
      • 2014-08-29
      相关资源
      最近更新 更多