繁体   English   中英

创建订单后如何获取挂钩内的项目数据

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

创建订单时,我尝试移动上传文件。 在每个产品项目数据中,我将文件路径和文件名存储到项目数据数组中

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

但是我如何才能在这些钩子中获取产品项目数据?

/* 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);
    }
}

您可以使用此 wc_get_order() function 通过传递 order_id 获取订单 object,然后您必须循环查找项目。 对于自定义元,您可以使用 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);
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM