|null */ public function build_from_order_id( int $order_id ): ?array { $order = wc_get_order( $order_id ); if ( ! $order instanceof \WC_Order ) { return null; } return $this->build( $order ); } /** * Build a snapshot for an abandoned cart ID. * * @param int $abandoned_cart_id Abandoned cart ID. * @return array|null */ public function build_from_abandoned_cart_id( int $abandoned_cart_id ): ?array { $cart = (new AbandonedCartRepository())->get( $abandoned_cart_id ); if ( ! $cart ) { return null; } return array( 'billing_phone' => $cart['phone'], 'billing_email' => $cart['email'], 'user_phone' => $cart['phone'], 'user_email' => $cart['email'], 'email' => $cart['email'], 'phone' => $cart['phone'], 'user_id' => $cart['user_id'] ?? $cart['session_id'], 'items' => $cart['cart_data'] ? json_decode( $cart['cart_data'], true ) : array(), ); } /** * Build a normalized snapshot from an order instance. * * @param \WC_Order $order Order instance. * @return array */ public function build( \WC_Order $order ): array { $variables = Variables::fromOrderInstance($order); $items = array(); foreach ( $order->get_items() as $item ) { $items[] = array( 'product_id' => (int) $item->get_product_id(), 'variation_id' => (int) $item->get_variation_id(), 'name' => (string) $item->get_name(), 'quantity' => (int) $item->get_quantity(), 'total' => (string) $item->get_total(), ); } $array = array(); foreach (Variables::getVariables() as $key => $variable) { $array[$key] = $variables->get($key); } $array['items'] = $items; $array['store'] = Variables::getOrderIndependentVariables(); unset($array['dummy'], $array['admin_email']); return array('order' => $array); } }