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

编程入门 行业动态 更新时间:2024-10-12 01:33:17
本文介绍了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以编程方式修改购物车总计

本文发布于:2023-11-24 21:52:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626989.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:购物车   方式   WooCommerce

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!