WooCommerce以编程方式修改购物车总计

时间:2019-03-15 08:47:53

标签: php wordpress woocommerce cart

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

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

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;
}

}

1 个答案:

答案 0 :(得分:1)

add_filter( 'woocommerce_cart_total', 'wc_modify_cart_price' ); 

function wc_modify_cart_price( $price ) {

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

Here

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

function modify_calculated_total( $total, $cart ) {

    return $total + 10;

}

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

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);
    }
 }

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

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 );