Если при использовании хука «woocommerce_new_order» — вы пытаетесь получить информацию о товарах в заказе, то скорее всего это не получится, так как заказ ещё не сформирован полностью.
Для решения этой проблемы воспользуйтесь «woocommerce_checkout_update_order_meta», вызывать его нужно с двумя параметрами. В этом случае вы свободно сможете получить всю нужную информацию о товарах в заказе.
1 2 3 |
add_action('woocommerce_checkout_update_order_meta', array($this, 'createNewOrder'), 1, 2); |
Первый параметр ИД заказа, второй общая информация о записи.
В Woo он вызывается в функции:(/wp-content/plugins/woocommerce/includes/class-wc-checkout.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
public function create_order( $data ) { // Give plugins the opportunity to create an order themselves. if ( $order_id = apply_filters( 'woocommerce_create_order', null, $this ) ) { return $order_id; } try { $order_id = absint( WC()->session->get( 'order_awaiting_payment' ) ); $cart_hash = md5( json_encode( wc_clean( WC()->cart->get_cart_for_session() ) ) . WC()->cart->total ); $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); /** * If there is an order pending payment, we can resume it here so * long as it has not changed. If the order has changed, i.e. * different items or cost, create a new order. We use a hash to * detect changes which is based on cart items + order total. */ if ( $order_id && ( $order = wc_get_order( $order_id ) ) && $order->has_cart_hash( $cart_hash ) && $order->has_status( array( 'pending', 'failed' ) ) ) { // Action for 3rd parties. do_action( 'woocommerce_resume_order', $order_id ); // Remove all items - we will re-add them later. $order->remove_order_items(); } else { $order = new WC_Order(); } foreach ( $data as $key => $value ) { if ( is_callable( array( $order, "set_{$key}" ) ) ) { $order->{"set_{$key}"}( $value ); // Store custom fields prefixed with wither shipping_ or billing_. This is for backwards compatibility with 2.6.x. // TODO: Fix conditional to only include shipping/billing address fields in a smarter way without str(i)pos. } elseif ( ( 0 === stripos( $key, 'billing_' ) || 0 === stripos( $key, 'shipping_' ) ) && ! in_array( $key, array( 'shipping_method', 'shipping_total', 'shipping_tax' ) ) ) { $order->update_meta_data( '_' . $key, $value ); } } $order->set_created_via( 'checkout' ); $order->set_cart_hash( $cart_hash ); $order->set_customer_id( apply_filters( 'woocommerce_checkout_customer_id', get_current_user_id() ) ); $order->set_currency( get_woocommerce_currency() ); $order->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) ); $order->set_customer_ip_address( WC_Geolocation::get_ip_address() ); $order->set_customer_user_agent( wc_get_user_agent() ); $order->set_customer_note( isset( $data['order_comments'] ) ? $data['order_comments'] : '' ); $order->set_payment_method( isset( $available_gateways[ $data['payment_method'] ] ) ? $available_gateways[ $data['payment_method'] ] : $data['payment_method'] ); $order->set_shipping_total( WC()->cart->get_shipping_total() ); $order->set_discount_total( WC()->cart->get_discount_total() ); $order->set_discount_tax( WC()->cart->get_discount_tax() ); $order->set_cart_tax( WC()->cart->get_cart_contents_tax() + WC()->cart->get_fee_tax() ); $order->set_shipping_tax( WC()->cart->get_shipping_tax() ); $order->set_total( WC()->cart->get_total( 'edit' ) ); $this->create_order_line_items( $order, WC()->cart ); $this->create_order_fee_lines( $order, WC()->cart ); $this->create_order_shipping_lines( $order, WC()->session->get( 'chosen_shipping_methods' ), WC()->shipping->get_packages() ); $this->create_order_tax_lines( $order, WC()->cart ); $this->create_order_coupon_lines( $order, WC()->cart ); /** * Action hook to adjust order before save. * @since 3.0.0 */ do_action( 'woocommerce_checkout_create_order', $order, $data ); // Save the order. $order_id = $order->save(); do_action( 'woocommerce_checkout_update_order_meta', $order_id, $data ); return $order_id; } catch ( Exception $e ) { return new WP_Error( 'checkout-error', $e->getMessage() ); } } |