WooCommerce以编程方式修改购物车总计 [英] WooCommerce modify cart totals programmatically

查看:106
本文介绍了WooCommerce以编程方式修改购物车总计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用代码更改购物车总数,但我不知道如何。我设法使用过滤器 woocommerce_cart_item_price 更改了购物车中每个项目的价格。推车总数是否有这样的筛选器(请参阅图片中的箭头)?

I'm trying to change the cart totals with code but I don't know how. I have managed to change the price for each item in the cart using the filter woocommerce_cart_item_price. Is there such a filter for the cart totals(see arrow in picture)?

这是每个单独项目的代码:

This is the code for each individual item:

add_filter( 'woocommerce_cart_item_price', 'func_change_product_price_cart', 10, 3 );
function func_change_product_price_cart($price, $cart_item, $cart_item_key){
if ( isset($cart_item['tau_lengde']) ) {
if ( WC()->cart->display_prices_including_tax() ) {

    $product_price = wc_get_price_including_tax( $cart_item['data'] );
} else {
    $product_price = wc_get_price_excluding_tax( $cart_item['data'] );
}
    $price = wc_price( (($product_price * $cart_item['tau_lengde']) + $cart_item['price_one_end'] + $cart_item['price_other_end']));

return $price;
}

}

推荐答案

add_filter( 'woocommerce_cart_total', 'wc_modify_cart_price' ); 

function wc_modify_cart_price( $price ) {

    $addition = 10;
    return  $price+addition;
}

此处

add_filter( 'woocommerce_calculated_total', 'modify_calculated_total', 20, 2 );

function modify_calculated_total( $total, $cart ) {

    return $total + 10;

}

项目总计=固定金额*数量

Item total = A fixed amount * Quantity

add_action( 'woocommerce_before_calculate_totals', 'modify_cart_price', 20, 1);

function modify_cart_price( $cart_obj ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) || ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ))
        return;

    foreach ( $cart_obj->get_cart() as $cart_item ) {
        $cart_item['data']->set_price( 1000);
    }
 }

项目总计=项目价格*数量(默认值)

Item total = Item price * Quantity ( Default )

add_action( 'woocommerce_before_calculate_totals', 'modify_cart_price', 20, 1);

function modify_cart_price( $cart_obj ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) || ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ))
        return;

    foreach ( $cart_obj->get_cart() as $cart_item ) {

        $cart_item['data']->set_price( $cart_item['data']->get_price());
    }
 }

购物车中的产品小计

function filter_woocommerce_cart_product_subtotal( $product_subtotal, $product, $quantity, $instance ) { 

    $product_subtotal = $product->get_price()*$quantity;
    return $product_subtotal; 
}; 


add_filter( 'woocommerce_cart_product_subtotal', 'filter_woocommerce_cart_product_subtotal', 10, 4 ); 

这篇关于WooCommerce以编程方式修改购物车总计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆