Wordpress 在管理中将产品添加到订单时更改价格
我们有不同折扣率的客户。在向购物车添加产品时,这些都是在前端编程和工作的,但是如果使用后端订单管理添加新产品,我似乎找不到基于用户折扣计算新产品价格的方法。在订单管理中将产品添加到现有订单时,是否有方法更改woocommerce\u new\u order\u item挂钩中的价格 以下是我目前掌握的情况:Wordpress 在管理中将产品添加到订单时更改价格,wordpress,woocommerce,Wordpress,Woocommerce,我们有不同折扣率的客户。在向购物车添加产品时,这些都是在前端编程和工作的,但是如果使用后端订单管理添加新产品,我似乎找不到基于用户折扣计算新产品价格的方法。在订单管理中将产品添加到现有订单时,是否有方法更改woocommerce\u new\u order\u item挂钩中的价格 以下是我目前掌握的情况: function action_woocommerce_new_order_item( $item_id, $item, $order_id ) { // only run thi
function action_woocommerce_new_order_item( $item_id, $item, $order_id ) {
// only run this from the WP admin section
if ( !is_admin() )
return;
$item_type = $item->get_type();
// return if this is not a product (i.e. fee, tax, etc.)
if ( $item_type != 'line_item' )
return;
$product = wc_get_product( $item->get_product_id() );
if ( !$product )
return;
$current_price = $product->get_price();
$quantity = $item->get_quantity();
// here I get the order's user's discount percentage and calculate the new discounted price
// custom function
$discounted_price = get_discounted_price( $current_price, $users_discount );
$new_price = ( !empty($discounted_price) ) ? $discounted_price : $current_price;
// this doesn't work
$item->set_price( $new_price );
// and this doesn't work
$product->set_price( $new_price );
// this appears to work but I'm not sure if this the best way to accomplish this
$item->set_total( $price * $quantity );
}
add_action( 'woocommerce_new_order_item', 'action_woocommerce_new_order_item', 10, 3 );
任何帮助都将不胜感激!
感谢这适用于在订单管理屏幕中添加商品元和更改商品价格:
function action_woocommerce_new_order_item( $item_id, $item, $order_id ) {
// only run this from the WP admin section
if ( !is_admin() )
return;
$item_type = $item->get_type();
// return if this is not a product (i.e. fee, tax, etc.)
if ( $item_type != 'line_item' )
return;
$product = wc_get_product( $item->get_product_id() );
if ( !$product )
return;
$current_price = $product->get_price();
$size = $product->get_attribute('size');
// add custom item meta to order
if ( $size ) {
wc_add_order_item_meta( $item_id, 'Size', $size , false );
}
$order = wc_get_order( $order_id );
$order_user = $order->get_user();
$order_user_id = $order->get_user_id();
// get this order's user's discount %
$users_discount = get_user_meta( $order_user_id, 'discount', true );
$users_discount = ( $users_discount > 0 ) ? $users_discount : 0;
// calculate the discounted price based on the user's discount (custom function)
$discounted_price = get_discounted_price( $current_price, $users_discount );
$quantity = $item->get_quantity();
$new_price = ( !empty($discounted_price) ) ? $discounted_price : $current_price;
$item->set_total( $new_price * $quantity );
};
add_action( 'woocommerce_new_order_item', 'action_woocommerce_new_order_item', 10, 3 );
这适用于在订单管理屏幕中添加项目元和更改项目价格:
function action_woocommerce_new_order_item( $item_id, $item, $order_id ) {
// only run this from the WP admin section
if ( !is_admin() )
return;
$item_type = $item->get_type();
// return if this is not a product (i.e. fee, tax, etc.)
if ( $item_type != 'line_item' )
return;
$product = wc_get_product( $item->get_product_id() );
if ( !$product )
return;
$current_price = $product->get_price();
$size = $product->get_attribute('size');
// add custom item meta to order
if ( $size ) {
wc_add_order_item_meta( $item_id, 'Size', $size , false );
}
$order = wc_get_order( $order_id );
$order_user = $order->get_user();
$order_user_id = $order->get_user_id();
// get this order's user's discount %
$users_discount = get_user_meta( $order_user_id, 'discount', true );
$users_discount = ( $users_discount > 0 ) ? $users_discount : 0;
// calculate the discounted price based on the user's discount (custom function)
$discounted_price = get_discounted_price( $current_price, $users_discount );
$quantity = $item->get_quantity();
$new_price = ( !empty($discounted_price) ) ? $discounted_price : $current_price;
$item->set_total( $new_price * $quantity );
};
add_action( 'woocommerce_new_order_item', 'action_woocommerce_new_order_item', 10, 3 );
此操作“woocommerce\u new\u order\u item”是在订单项目保存后完成的,即您应该在只读模式下使用它。是否有其他更合适的钩子可用?我发现我可以在上面函数中的$item对象上使用这些方法:我错了!有两个位置执行此操作,只有最后一个位置在订单项保存后执行。我调查了第二个位置的代码,但我不知道该位置的代码,并且不认为该代码正常使用,因此您在抽象中调用此操作。\u WC\u order\u item\u Type\u Data\u Store::create()这将使订单项目成为只读。此方法似乎可行,但我不确定这是否是正确的方法:
$item->set\u total($new\u price*$quantity)代码>此操作“woocommerce\u new\u order\u item”是在订单项目保存后完成的,即您应该在只读模式下使用它。是否有其他更合适的钩子可用?我发现我可以在上面函数中的$item对象上使用这些方法:我错了!有两个位置执行此操作,只有最后一个位置在订单项保存后执行。我调查了第二个位置的代码,但我不知道该位置的代码,并且不认为该代码正常使用,因此您在抽象中调用此操作。\u WC\u order\u item\u Type\u Data\u Store::create()这将使订单项目成为只读。此方法似乎可行,但我不确定这是否是正确的方法:$item->set\u total($new\u price*$quantity)代码>