WooCommerce меняет цену при добавлении товара в заказ в админке - PullRequest
1 голос
/ 04 февраля 2020

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

Любая помощь будет с благодарностью! Спасибо

1 Ответ

0 голосов
/ 10 февраля 2020

Это работает для добавления мета товара и изменения цены товара на экране администратора заказа:

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 ); 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...