У нас есть клиенты с разным процентом скидок. Все это запрограммировано и работает во внешнем интерфейсе при добавлении продуктов в корзину, но если использовать администратора внутреннего заказа для добавления нового продукта, я не могу найти способ рассчитать цену нового продукта на основе скидки пользователя , Есть ли способ изменить цену в хуке woocommerce_new_order_item при добавлении товара в существующий заказ в администраторе заказа?
Вот что у меня есть:
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 );
Любая помощь будет с благодарностью! Спасибо