【问题标题】:WooCommerce modify cart totals programmaticallyWooCommerce 以编程方式修改购物车总数
【发布时间】:2019-03-15 08:47:53
【问题描述】:

我正在尝试使用代码更改购物车总数,但我不知道如何操作。我设法使用过滤器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;
}

}

【问题讨论】:

  • 添加一些代码。您购物车的代码等...
  • 我在问题中添加了代码

标签: php wordpress woocommerce cart


【解决方案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 ); 

【讨论】:

  • 当我尝试这段代码时,每件商品的总价仍然是变化价格(见图中的箭头),但页面底部的总价现在是 0。
  • @Paudun 试试新的 sn-p 答案——这就是你要找的?
  • 新的 sn-p 作用于页面底部显示的总价,但不适用于箭头旁边的总价。
  • @Paudun 旁边的箭头是什么意思?商品总价?你能说得更具体点吗?
  • 我更新了问题中的图片。我正在尝试修改箭头指向的价格。现在它只显示项目变化价格,即 50。您的代码将底部的价格更改为 10269。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-13
  • 2016-06-04
  • 2016-02-12
  • 2014-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多