将产品添加到购物车并覆盖价格

将产品添加到购物车并覆盖价格

本文介绍了WooCommerce:将产品添加到购物车并覆盖价格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$replace_order = new WC_Cart();
$replace_order->empty_cart( true );
$replace_order->add_to_cart( "256", "1");

以上代码将产品256添加到购物车1时间.但我遇到的问题是我希望能够完全覆盖产品价格...据我所知,我唯一能做的就是将优惠券应用到购物车.

The above code add product 256 to the Cart 1 time. But the issue I'm having is that I want to be able to completely override the product price... as far as I can tell, the only thing I can do it apply a coupon to the Cart.

有没有办法将价格完全覆盖为完全定制的东西?

Is there a way to completely override the price to something totally custom?

推荐答案

这里是覆盖购物车中产品价格的代码

Here is the code for overriding price of product in cart

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

function add_custom_price( $cart_object ) {
    $custom_price = 10; // This will be your custome price
    foreach ( $cart_object->cart_contents as $key => $value ) {
        $value['data']->price = $custom_price;
        // for WooCommerce version 3+ use:
        // $value['data']->set_price($custom_price);
    }
}

希望有用...

这篇关于WooCommerce:将产品添加到购物车并覆盖价格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 14:08