[英]WooCommerce modify cart totals programmatically
我正在嘗試使用代碼更改購物車總數,但我不知道如何。 我設法使用過濾器woocommerce_cart_item_price
更改了購物車中每件商品的價格。 購物車總數是否有這樣的過濾器(見圖片中的箭頭)?
這是每個單獨項目的代碼:
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;
}
項目總數 = 固定數量 * 數量
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 );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.