通过结帐中的按钮单击更改订单审核表的运费价值#woocommerce

问题描述 投票:0回答:1

当用户单击我已创建的账单详细信息部分中的按钮时,我一直在尝试更新结帐页面中订单审核表中的手动运费值。在这种情况下我使用了手动测试值。但订单审核表不会更新订单审核表中的该值。你能帮忙吗?

我尝试隐藏现有的运输标签并添加一个名为“Delivery”的新标签。然后我声明了一个测试值并分配给该标签。我通过 ajax 响应获得了更新后的值。但订单审核表“送货”标签的值未更新。 (请查看php代码)

*注意 - 如果我在结帐页面的初始加载中直接分配给“送货”标签,它会显示测试值。(请参阅下面的代码)

function modify_shipping_cost($testvalue) {
        
    $testvalue=40;
    WC()->cart->add_fee('Delivery', $testvalue, true, 'standard');

    return $testvalue;
}

PHP

// Remove the shipping row from the order review in WooCommerce checkout
add_action('woocommerce_review_order_before_payment', 'remove_shipping_row_from_order_review');

function remove_shipping_row_from_order_review() {
    include_once(plugin_dir_path(__FILE__) . 'distancecal.php'); // Include PHP functions file
    // Check if we are on the checkout page
    if (is_checkout()) {
        // Output custom style to hide the shipping row
        echo '<style>.woocommerce-checkout-review-order-table tr.shipping { display: none; }</style>';
    }
}

// Remove the shipping cost and add a manual fee
add_action('woocommerce_cart_calculate_fees', 'modify_shipping_cost');

function modify_shipping_cost($testvalue) {
        
        
    // Add the manual shipping fee
    WC()->cart->add_fee('Delivery', $testvalue, true, 'standard');

    return $testvalue;
}


// AJAX handler for updating checkout
add_action('wp_ajax_update_checkout', 'update_checkout_callback');

function update_checkout_callback() {

    $testvalue = 100;
    
    // Update the shipping cost with the new test value
    $newvalue = modify_shipping_cost($testvalue);
    
    wp_send_json(array('updated_fee' => $newvalue));

    // Make sure to exit after handling the AJAX request
    wp_die();

} ?>

JS

jQuery(document).ready(function($) {
    // Add your custom JavaScript here
    $('#custom-button').on('click', function() {
        var streetAddress = $('#billing_address_1').val();
        $.ajax({
            type: 'POST',
            url: ajax_object.ajaxurl,
            data: {
                action: 'update_checkout',
                street_address: streetAddress
            },
            success: function(response) {
                console.log(response);
                $('body').trigger('update_checkout', { update_shipping_method: true });
                
            },
            error: function(error) {
                // Handle AJAX error if needed
                console.error('AJAX error:', error);
            }
        });
    });
});
wordpress woocommerce checkout custom-wordpress-pages custom-widgets
1个回答
0
投票

我最近创建了一个自定义结帐流程,必须从单独的数据库中提取运费。

它使用 woocommerce 中现有的运输方式,只是调整了其费率,这意味着您不必隐藏现有的运输标签。

我已经调整了你的代码以使用与我使用的相同的方法。

function modify_shipping_cost($shipping_cost) {
    if ( isset($shipping_cost) ){
        WC()->session->set('shipping_cost', $shipping_cost);
        return $shipping_cost;
    }
}

add_filter( 'woocommerce_package_rates', 'shipping_package_rates_filter_callback', 100, 2 );
function shipping_package_rates_filter_callback( $rates, $package ) {
    // The defined rate id
    $shipping_rate_id = 'flat_rate:31'; //Use your own shipping method id

    $tax_rate = 0.1; // 10%

    if( isset(WC()->session->get('shipping_cost') ) ) {
        $shipping_cost = WC()->session->get('shipping_cost' );
        $rates = array( $shipping_rate_id => $rates[ $shipping_rate_id ] );
        // Set rate cost
        $rates[$shipping_rate_id ]->cost = $shipping_cost;
        // Set taxes rate cost (if enabled)
        $taxes = array();
        foreach ($rates[$shipping_rate_id ]->taxes as $key => $tax){
            if( $rates[$shipping_rate_id ]->taxes[$key] > 0 )
                $taxes[$key] = $shipping_cost * $tax_rate;
        }
        $rates[$shipping_rate_id ]->taxes = $taxes;
    } else {
        unset( $rates[ $shipping_rate_id ] );
    }

    return $rates;
}

add_action('wp_ajax_update_checkout', 'update_checkout_callback');
function update_checkout_callback() {

    $testvalue = 100;
    
    // Update the shipping cost with the new test value
    $newvalue = modify_shipping_cost($testvalue);
    
    wp_send_json(array('updated_fee' => $newvalue));

    // Make sure to exit after handling the AJAX request
    wp_die();

}

您现有的 JS 代码应该可以正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.