简体   繁体   中英

Wordpress - Programmatically change cart shipping total to whatever custom value

I set up a dummy Wordpress website, just with the WooCommerce plugin.

I also have one dummy product: " Cool Watch ".

My goal is very simple. After the user adds one "Cool Watch" to the cart. I wanna change his shipping total to whatever custom value , for example: 67.89 USD.

What do I need to do to get this programmatically ?

For now I get: Shipping total: Free!

Here I give you some background info of the current situation:

Having one "Cool Watch" on my cart.

With this code:

<?
if (!function_exists("echoc")) {
    function echoc($data) {
        echo "\n<pre>\n";
        print_r($data);
        echo "</pre>\n";
    }
}

$cart_items = $woocommerce->cart->get_cart();
echoc($cart_items);

$shipping_total = $woocommerce->cart->get_cart_shipping_total();
echoc("Shipping total: ".$shipping_total);

?>

I get this:

Array
(
    [c74d97b01eae257e44aa9d5bade97baf] => Array
        (
            [product_id] => 16
            [variation_id] => 0
            [variation] => Array
                (
                )

            [quantity] => 1
            [line_total] => 50
            [line_tax] => 3.075
            [line_subtotal] => 50
            [line_subtotal_tax] => 3.075
            [line_tax_data] => Array
                (
                    [total] => Array
                        (
                            [1] => 3.075
                        )

                    [subtotal] => Array
                        (
                            [1] => 3.075
                        )

                )

            [data] => WC_Product_Simple Object
                (
                    [id] => 16
                    [post] => WP_Post Object
                        (
                            [ID] => 16
                            [post_author] => 1
                            [post_date] => 2017-03-23 15:05:00
                            [post_date_gmt] => 2017-03-23 15:05:00
                            [post_content] => This is a very cool Watch!
                            [post_title] => Cool Watch
                            [post_excerpt] => 
                            [post_status] => publish
                            [comment_status] => open
                            [ping_status] => closed
                            [post_password] => 
                            [post_name] => cool-watch
                            [to_ping] => 
                            [pinged] => 
                            [post_modified] => 2017-03-23 10:09:35
                            [post_modified_gmt] => 2017-03-23 15:09:35
                            [post_content_filtered] => 
                            [post_parent] => 0
                            [guid] => http://dummy.development.lagoon.com/?post_type=product&p=16
                            [menu_order] => 0
                            [post_type] => product
                            [post_mime_type] => 
                            [comment_count] => 0
                            [filter] => raw
                        )

                    [product_type] => simple
                    [shipping_class:protected] => 
                    [shipping_class_id:protected] => 0
                    [total_stock] => 
                    [supports:protected] => Array
                        (
                            [0] => ajax_add_to_cart
                        )

                    [price] => 50.00
                )

        )

)

Shipping total: Free!

Hey this should do it:

$cart = $woocommerce->cart ;
$cart->shipping_total = "what ever u want"

shipping_total is a public property of Cart Class https://docs.woocommerce.com/wc-apidocs/class-WC_Cart.html

add_action('woocommerce_calculate_totals', 'calculate_totals', 10, 1);

function calculate_totals($totals){
//your code
// return your own value
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM