• Resolved conceptcorenl

    (@conceptcorenl)


    Hello,

    I need to edit the quantity using php when a form had been posted.
    I am doing the following actions :

    $order = new WC_Order($orderId);
    $order->update_status($data['order_status']);
    $order->update_product($orderId, array('qty' => $data['order_item_qty']));

    The bottom function does not fire and change the quantity.
    Am I missing something ?

    https://ww.wp.xz.cn/plugins/woocommerce/

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Contributor Mike Jolley

    (@mikejolley)

    update_product takes an item_id, not the order_id.

    Thread Starter conceptcorenl

    (@conceptcorenl)

    and the item id would be : order_item_id, from the wp_woocommerce_order_itemmeta table ?

    Plugin Contributor Mike Jolley

    (@mikejolley)

    Yes.

    Thread Starter conceptcorenl

    (@conceptcorenl)

    That is strange since it does not update the quantity if I do that.
    This is what I have now :

    /**
     * Update een order voor een klant
     * @param  int $userId  user id
     * @param  int $orderNr order id
     * @return bool          return true or false
     */
    function updateOrder($orderId, $data) {
    var_dump($orderId);
    	/**
    	 * Verander de order status
    	 * @var WC_Order
    	 */
    	$order = new WC_Order($orderId);
    	$order->update_status($data['order_status']);
    	$order->update_product($data['metaId'], array('qty' => $data['order_item_qty']));
    }

    Plugin Contributor Mike Jolley

    (@mikejolley)

    I can’t tell if metaId is valid from that paste.

    You should also use wc_get_order() function, not the new WC_Order.

    Thread Starter conceptcorenl

    (@conceptcorenl)

    $orderId dumps : 340 << which also is shown in admin panel at orders.
    $data[‘metaId’] dumps : 5 << which corrospodends with the order_item_id

    Thread Starter conceptcorenl

    (@conceptcorenl)

    So the correct code should be :

    /**
    	 * Verander de order status
    	 * @var WC_Order
    	 */
    	$order = wc_get_order($orderId);
    	$order->update_status($data['order_status']);
    	$order->update_product($orderId, $data['metaId'], array('qty' => $data['order_item_qty']));

    Plugin Contributor Mike Jolley

    (@mikejolley)

    No. see https://github.com/woothemes/woocommerce/blob/master/includes/abstracts/abstract-wc-order.php#L327

    public function update_product( $item_id, $product, $args ) {
    Thread Starter conceptcorenl

    (@conceptcorenl)

    Ok so the update_product function should be written out like :

    /**
     * Verander de order status
     * @var WC_Order
     */
    
    // var_dump($data['metaId']);
    // string(1) “5” 
    
    // var_dump($orderId);
    // string(3) “340”
    
    $order = wc_get_order($orderId);
    $order->update_status($data['order_status']);
    $order->update_product($data['metaId'], $orderId, array('qty' => $data['order_item_qty']));

    Plugin Contributor Mike Jolley

    (@mikejolley)

    No 🙂 Arg #2 is $product. It’s a product object.

    Plugin Contributor Mike Jolley

    (@mikejolley)

    You could just call the update directly you know:

    wc_update_order_item_meta( $item_id, '_qty', wc_stock_amount( $args['qty'] ) );
    Thread Starter conceptcorenl

    (@conceptcorenl)

    Thank you very much for helping me that did the job.
    And I actually did not see the wc_update_order_item_meta function on :https://docs.woothemes.com/wc-apidocs/.

    I looked at the classes since that made sense to me.

Viewing 12 replies - 1 through 12 (of 12 total)

The topic ‘Updare quantity with php not working’ is closed to new replies.