我需要将价格为0的商品添加到购物车。
我使用以下代码将其添加到购物车:
$product = wc_get_product($p); $datos_add = array( 'ref-gift' => true, ); $cart_item_id = $cart->add_to_cart($p , 1,0, array(), $datos_add);
更改购物车价格:
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 30, 1); function add_custom_price( $cart_obj ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return; foreach ( $cart_obj->get_cart() as $cart_item ) { if( isset( $cart_item['ref-gift'] ) ) { $cart_item['data']->set_price( 0 ); $cart_item['data']->set_regular_price( 0 ); } } }
我有一个已安装的插件,无法禁用该插件来按角色更改产品价格。我也不能使用另一个,这是强制性的。 此插件会使用适用的折扣覆盖我尝试按产品的正常价格修改的价格。
这是通过插件中的内部功能完成的:
add_filter( 'woocommerce_product_get_price', array( $this, 'get_price' ), 20, 2 ); add_filter( 'woocommerce_product_variation_get_price', array( $this, 'get_price' ), 20, 2 );
public function get_price( $price, $product ) { $return_original_price = apply_filters( 'yith_wcrbp_return_original_price', false, $price, $product ); $is_custom_price = $this->is_custom_price( $product ); if ( ! $product || ( is_admin() && ! is_ajax() ) || $return_original_price || $is_custom_price ) { return $price; } if ( $price !== '' && ! is_null( $product ) && ! $product->is_type( 'variable' ) && ! $product->is_type( 'grouped' ) ) { $role_price = $this->get_role_based_price( $product ); if ( $role_price !== 'no_price' ) { $price = apply_filters( 'yith_wcrbp_get_role_based_price', $role_price, $product ); } } return $price; }
如何在不影响此插件的情况下更改所添加产品的价格?
Ty