get_items() as $item ) { $design_id = $item->get_meta( '_eazign_design_id' ); // Products made by the generator: the service knows their print // design per colour. $generated = ! $design_id && '1' === (string) get_post_meta( $item->get_product_id(), self::GENERATED, true ); if ( ! $design_id && ! $generated ) { continue; } $product = $item->get_product(); // Option values in attribute order, e.g. "White / A3": the service // takes the colour and the print size of size options from it. $title = $product && $product->is_type( 'variation' ) ? implode( ' / ', array_filter( array_values( $product->get_attributes() ) ) ) : ''; $entry = array( 'lineItemId' => (string) $item->get_id(), 'variantTitle' => $title, ); if ( $design_id ) { $entry['designId'] = $design_id; } else { $entry['productId'] = $item->get_product_id(); } $items[] = $entry; } return $items; } /** * Order paid (processing) or completed: render the print files once. * * @param int $order_id Order id. * @param bool $force Send again (button in the order box). * @return true|WP_Error|null Null when there is nothing to send. */ public static function send( $order_id, $force = false ) { $order = wc_get_order( $order_id ); if ( ! $order || ( ! $force && $order->get_meta( self::SENT ) ) ) { return null; } $items = self::designed_items( $order ); if ( ! $items ) { return null; } $result = Eazign_Client::post( 'orders', array( 'orderId' => (string) $order->get_id(), 'orderNumber' => '#' . $order->get_order_number(), 'items' => $items, ) ); if ( is_wp_error( $result ) ) { /* translators: %s: error message */ $order->add_order_note( sprintf( __( 'Eazign: print files could not be requested: %s', 'eazign' ), $result->get_error_message() ) ); return $result; } $order->update_meta_data( self::SENT, time() ); $order->save(); /* translators: %d: number of designed items */ $order->add_order_note( sprintf( _n( 'Eazign: print files for %d designed item are being created.', 'Eazign: print files for %d designed items are being created.', count( $items ), 'eazign' ), count( $items ) ) ); return true; } /** * Box on the order screen (classic and HPOS). * * @param string $screen Screen id. * @param mixed $object Post or order. */ public static function meta_box( $screen, $object = null ) { $screens = array( 'shop_order', 'woocommerce_page_wc-orders' ); if ( ! in_array( $screen, $screens, true ) ) { return; } $order = $object instanceof WC_Order ? $object : wc_get_order( $object instanceof WP_Post ? $object->ID : 0 ); if ( ! $order || ! self::designed_items( $order ) ) { return; } add_meta_box( 'eazign-print-files', __( 'Print files (Eazign)', 'eazign' ), array( __CLASS__, 'render_box' ), $screen, 'normal', 'high' ); } /** * Box content. * * @param mixed $object Post or order. */ public static function render_box( $object ) { $order = $object instanceof WC_Order ? $object : wc_get_order( $object->ID ); $data = Eazign_Client::get( 'orders/' . rawurlencode( (string) $order->get_id() ) ); if ( is_wp_error( $data ) ) { echo '
' . esc_html( $data->get_error_message() ) . '
'; return; } if ( empty( $data['jobs'] ) ) { echo '' . esc_html__( 'The print files are created when the order is paid (status "Processing").', 'eazign' ) . '
'; } else { echo '| ' . esc_html__( 'Preview', 'eazign' ) . ' | ' . esc_html__( 'Item', 'eazign' ) . ' | ' . esc_html__( 'Variant', 'eazign' ) . ' | ' . esc_html__( 'Status', 'eazign' ) . ' | ' . esc_html__( 'Files', 'eazign' ) . ' |
|---|
' . esc_html__( 'Create print files now', 'eazign' ) . '
'; } /** * One print job as table row (order box and print files page). * * @param array $job Job from the service. * @param bool $with_order First column links to the order. * @param WC_Order|null $order Order of the box. */ public static function job_row( $job, $with_order, $order = null ) { $labels = array( 'pending' => __( 'Waiting', 'eazign' ), 'rendering' => __( 'Rendering…', 'eazign' ), 'ready' => __( 'Ready', 'eazign' ), 'printed' => __( 'Printed', 'eazign' ), 'failed' => __( 'Failed', 'eazign' ), ); echo '