• Resolved snuffybg

    (@snuffybg)


    Hello,
    What would be the best approach to change the author of an order? By default all orders are assigned to the admin.

    Currently i’ve added

    add_post_type_support( 'product', 'author' );
     add_post_type_support( 'shop_order', 'author' );

    I cant understand this hook “woocommerce_checkout_create_order” and how to update the author meta based on the product author.
    What i tried is:

    add_action('woocommerce_checkout_create_order', 'custom_set_order_author', 20, 2);
    function custom_set_order_author( $order, $data ) {
        $items = $order->get_items(); 
        foreach ( $items as $item ) {
            $product_id = wc_get_product($item->get_product_id());
            $author_id = get_post_field( 'post_author', $product_id );
        }
        $order_id = $order->get_order_number();
        update_post_meta( $order_id,'post_author', $author_id );
    }

    I already restricted customer to add products only if they are from the same author so it will have same author for order and product.

    • This topic was modified 5 years, 6 months ago by snuffybg.
Viewing 1 replies (of 1 total)
  • Thread Starter snuffybg

    (@snuffybg)

    This is working for me

    add_action('woocommerce_checkout_update_order_meta', 'custom_set_order_author', 20, 2);
    function custom_set_order_author( $order_id, $data ) {
        
        $order = wc_get_order( $order_id );
        $items = $order->get_items(); 
        $product_id = reset($items)->get_product_id();
        $post_obj = get_post($product_id);
        $author_id = $post_obj->post_author;
        wp_update_post(array('ID' => $order_id, 'post_author' => $author_id));
    }

    If you have any notes please share

Viewing 1 replies (of 1 total)
  • The topic ‘Change order author’ is closed to new replies.