Automattic\WooCommerce\Internal\Admin\EmailPreview

EmailPreview::get_dummy_orderprivateWC 1.0

Get a dummy order object without the need to create in the database.

Метод класса: EmailPreview{}

Хуки из метода

Возвращает

WC_Order.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->get_dummy_order();

Код EmailPreview::get_dummy_order() WC 10.5.0

private function get_dummy_order() {
	$product              = $this->get_dummy_product();
	$variation            = $this->get_dummy_product_variation();
	$downloadable_product = $this->get_dummy_downloadable_product();

	$order = new WC_Order();
	$order->set_id( 12345 );

	// Create and add product items manually without saving to database.
	// Use add_item() instead of add_product() to avoid immediate database writes.
	if ( $product ) {
		$item = new WC_Order_Item_Product();
		$item->set_props(
			array(
				'name'         => $product->get_name(),
				'tax_class'    => $product->get_tax_class(),
				'product_id'   => $product->get_id(),
				'variation_id' => 0,
				'quantity'     => 2,
				'subtotal'     => $product->get_price() * 2,
				'total'        => $product->get_price() * 2,
			)
		);
		$order->add_item( $item );
	}
	if ( $variation ) {
		$item = new WC_Order_Item_Product();
		$item->set_props(
			array(
				'name'         => $variation->get_name(),
				'tax_class'    => $variation->get_tax_class(),
				'product_id'   => $variation->get_parent_id(),
				'variation_id' => $variation->get_id(),
				'variation'    => $variation->get_attributes(),
				'quantity'     => 1,
				'subtotal'     => $variation->get_price(),
				'total'        => $variation->get_price(),
			)
		);
		$order->add_item( $item );
	}
	if ( $downloadable_product ) {
		$item = new WC_Order_Item_Product();
		$item->set_props(
			array(
				'name'         => $downloadable_product->get_name(),
				'tax_class'    => $downloadable_product->get_tax_class(),
				'product_id'   => $downloadable_product->get_id(),
				'variation_id' => 0,
				'quantity'     => 1,
				'subtotal'     => $downloadable_product->get_price(),
				'total'        => $downloadable_product->get_price(),
			)
		);
		$order->add_item( $item );
	}

	$order->set_date_created( time() );
	$order->set_currency( 'USD' );
	$order->set_discount_total( 10 );
	$order->set_shipping_total( 5 );
	$order->set_total( 80 );
	$order->set_payment_method_title( __( 'Direct bank transfer', 'woocommerce' ) );
	$order->set_transaction_id( '999999999' );
	$order->set_customer_note( __( "This is a customer note. Customers can add a note to their order on checkout.\n\nIt can be multiple lines. If there's no note, this section is hidden.", 'woocommerce' ) );

	$order = $this->apply_dummy_order_status( $order );

	// Add shipping method.
	$shipping_item = new WC_Order_Item_Shipping();
	$shipping_item->set_props(
		array(
			'method_title' => __( 'Flat rate', 'woocommerce' ),
			'method_id'    => 'flat_rate',
			'total'        => '5.00',
		)
	);
	$order->add_item( $shipping_item );

	$address = $this->get_dummy_address();
	$order->set_billing_address( $address );
	$order->set_shipping_address( $address );

	/**
	 * A dummy WC_Order used in email preview.
	 *
	 * @param WC_Order $order The dummy order object.
	 * @param string   $email_type The email type to preview.
	 *
	 * @since 9.6.0
	 */
	return apply_filters( 'woocommerce_email_preview_dummy_order', $order, $this->email_type );
}