Wordpress - 以编程方式将购物车运送总额更改为任何自定义值

时间:2017-03-23 15:58:28

标签: php wordpress woocommerce

我使用Wordpress插件设置了一个虚拟WooCommerce网站。

我还有一个虚拟产品:“Cool Watch”。

我的目标非常简单。用户在购物车中添加一个“Cool Watch”后。我想changeshipping total custom value改为programmatically,例如:67.89 USD。

我需要做些什么才能获得Shipping total: Free!

现在我得到:<? 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); ?>

这里我给你一些当前情况的背景信息:

我的购物车上有一个“酷手表”。

使用此代码:

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!

我明白了:

minimumInteritemSpacing = 0

2 个答案:

答案 0 :(得分:2)

嘿,这应该这样做:

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

shipping_total是Cart Class的公共属性 https://docs.woocommerce.com/wc-apidocs/class-WC_Cart.html

答案 1 :(得分:0)

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

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