最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

hooks - How to Update the Order-Items While Editing an Order on the WooCommerce Admin Order Screen

programmeradmin1浏览0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

Is it possible to edit order item totals and custom fees in the woocommerce admin edit order screen? The only hook that I can find is woocommerce_new_order_item, but when I add/remove a product in the admin I need to adjust the custom order items totals (e.g. like deposit and delivery fees which are order line item fees).

For example, if I increase the quantity of a product (or add a different product to the order) in the edit order admin, I need to also increase the deposit amount on the order. I would also need to do the opposite, if I decrease quantity or remove a product from the order I'll need to decrease the deposit amount.

Is this doable?? What hooks should I be looking at? I've searched several forums and can't seem to find an answer.

Any help at all would be greatly appreciated.

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

Is it possible to edit order item totals and custom fees in the woocommerce admin edit order screen? The only hook that I can find is woocommerce_new_order_item, but when I add/remove a product in the admin I need to adjust the custom order items totals (e.g. like deposit and delivery fees which are order line item fees).

For example, if I increase the quantity of a product (or add a different product to the order) in the edit order admin, I need to also increase the deposit amount on the order. I would also need to do the opposite, if I decrease quantity or remove a product from the order I'll need to decrease the deposit amount.

Is this doable?? What hooks should I be looking at? I've searched several forums and can't seem to find an answer.

Any help at all would be greatly appreciated.

Share Improve this question edited Feb 5, 2020 at 21:43 Yashar 3082 silver badges9 bronze badges asked Feb 5, 2020 at 16:57 kojakkojak 191 silver badge5 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

The woocommerce_order_before_calculate_totals hook executes in the Edit Order admin screen when an item is changed or the 'recalculate' button is pressed. This allows you to loop through each item and fee in the order and edit as required. I have a fee called 'Deposit' which I update if it exists or I add if it doesn't.

function custom_order_before_calculate_totals($and_taxes, $order ) {
    // The loop to get the order items which are WC_Order_Item_Product objects since WC 3+
    // loop all products and calculate total deposit
    $total_deposit = 0;
    foreach( $order->get_items() as $item_id => $item ) {
        // get the WC_Product object
        $product = $item->get_product();

        // get the quantity
        $product_quantity = $item->get_quantity();

        // get the deposit amount
        $deposit = $product->get_attribute('deposit') * $product_quantity;

        // sum of deposits from all products
        $total_deposit += $deposit;
    }


    // update the Deposit fee if it exists
    $deposit_fee_exists = false;
    foreach( $order->get_fees() as $item_id => $item_fee ) {
        $fee_name = $item_fee->get_name();

        if ( $fee_name == 'Deposit' ) {
            $item_fee->set_tax_status('none'); // no tax on deposit
            $item_fee->set_total($total_deposit);
            $deposit_fee_exists = true;
            break;
        }
    }

    // if there isn't an existing deposit fee then add it
    if ( $total_deposit > 0 && !$deposit_fee_exists ) {
        // Get a new instance of the WC_Order_Item_Fee Object
        $item_fee = new WC_Order_Item_Fee();

        $item_fee->set_name( "Deposit" ); // Generic fee name
        $item_fee->set_amount( $total_deposit ); // Fee amount
        $item_fee->set_tax_status( 'none' ); // or 'none'
        $item_fee->set_total( $total_deposit ); // Fee amount

        // Add Fee item to the order
        $order->add_item( $item_fee );
    }
}
add_action( 'woocommerce_order_before_calculate_totals', "custom_order_before_calculate_totals", 10, 3);

First of all, welcome to the community :)

When you add/remove an order item (e.g a product), WooCommerce uses an Ajax call to update the order.

Adding a New Item to the Order

Adding a new item, would be done by WC_AJAX::add_order_item(). This method does the adding process and then fires woocommerce_ajax_order_items_added action hook. We can use that action to do some nasty stuff on the order!

For an example, if I want to add an order fee with total of 1000, when a product from product category 188 is added to the order, this would do the trick:

add_action('woocommerce_ajax_order_items_added', 'tst_update_order', 10, 2);

/**
 * @param \WC_Order_Item[]  $added_items
 * @param \WC_order $order
 */
function tst_update_order( $added_items, $order )
{
    foreach ( $added_items as $added_item ) {

        if ( 'line_item' === $added_item->get_type() ) {

            /** @var \WC_Order_Item_Product $added_item */
            $product = $added_item->get_product();
            $cat_ids = $product->get_category_ids();

            if ( in_array( 188, $cat_ids ) ) {
                $fee = new \WC_Order_Item_Fee();
                $fee->set_name('Order Fee');
                $fee->set_total('1000');
                $fee->save();
                $order->add_item($fee);
                $order->save();
            }
        }
    }
}

Note: There is another action woocommerce_ajax_add_order_item_meta which fires on updating every single order item. You may want to use that instead of looping through $added_items.

Removing an existing Item from the Order

The corresponding method for removing an item from the current order is WC_AJAX::remove_order_item(). this method itself uses wc_delete_order_item() function to remove an order item. You may want to use the woocommerce_before_delete_order_item action which is defined in this function to do your calculations and update the order.

发布评论

评论列表(0)

  1. 暂无评论