sander
sander

Reputation: 1581

How to get the item data inside hook after order is created

I try to move upload files when the order is created. Inside each product item data i stored the filepath and filename into item data array

$idOrder = $woocommerce->cart->add_to_cart($product_id, $qty, '', '', $item_data_array = array(
      "custom_price"        =>  $custom_price,
      "fotoafdrukken_data"  =>  $fotoafdrukken_data,
      "subdir"              =>  $subdir
    ));

But how i can reach the product item data in these hooks?

/* add_action( 'woocommerce_new_order', 'move_upload_files' );
add_action( 'woocommerce_thankyou', 'move_upload_files', 10, 1 ); */
add_action( 'woocommerce_checkout_order_processed', 'move_upload_files' );
function move_upload_files($order_id, $posted_data, $order ) {
    if( $order_id ){
        $full_path = $_SERVER['DOCUMENT_ROOT'] . "/" . "uploads" . "/". "order_" . $order_id . "/";
        if( !file_exists($full_path) && !is_dir($full_path) ) mkdir($full_path, 0777, true);
    }
}

Upvotes: 1

Views: 418

Answers (1)

Bhautik
Bhautik

Reputation: 11282

You can get the order object using this wc_get_order() function by passing order_id and then you have to loop for items. for custom meta you can use get_meta() function.

add_action( 'woocommerce_checkout_order_processed', 'move_upload_files' );
function move_upload_files($order_id, $posted_data, $order ) {
    if( $order_id ){

        $order = wc_get_order( $order_id ); // instance of the WC_Order object

        foreach ( $order->get_items() as $item_id => $item ) {

            $subdir = $item->get_meta('subdir'); // your item data key

            $full_path = $_SERVER['DOCUMENT_ROOT'] . "/" . "uploads" . "/". "order_" . $order_id . "/";
            if( !file_exists($full_path) && !is_dir($full_path) ) mkdir($full_path, 0777, true);
        }
    }
}

Upvotes: 2

Related Questions