Skip to content

I want to place magento order using custom script using order Management interface but it’s give error of invalid email format

I want to place magento order using custom script using order Management interface but it’s give error of invalid email format.

`<?php
use MagentoFrameworkAppBootstrap;
use MagentoSalesApiDataOrderInterface;
use MagentoSalesApiDataOrderItemInterface;
use MagentoSalesApiOrderManagementInterface;
use MagentoSalesApiDataOrderAddressInterface;
use MagentoSalesApiDataOrderPaymentInterface;
use MagentoSalesModelOrderFactory;
use MagentoSalesModelOrderPaymentFactory;
use MagentoFrameworkApiDataObjectHelper;
use MagentoSalesModelResourceModelOrderAddress as OrderAddressResource;
use MagentoSalesModelResourceModelOrderPayment as OrderPaymentResource;
use MagentoSalesModelResourceModelOrderItem as OrderItemResource;
use MagentoFrameworkAppState;

require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();

// Set your data here
$customerId = 1;
$store = $objectManager->get(MagentoStoreModelStoreManagerInterface::class)->getStore();
$currencyCode = 'USD';

// Sample product data
$productId = 1;
$productQty = 2;
$productPrice = 50.00;

// Sample shipping and billing address data
$shippingAddressData = [
    'firstname' => 'John',
    'lastname' => 'Doe',
    'street' => '123 Shipping Street',
    'city' => 'New York',
    'country_id' => 'US',
    'region' => 'NY',
    'postcode' => '10001',
    'telephone' => '1234567890',
];

$billingAddressData = [
    'firstname' => 'John',
    'lastname' => 'Doe',
    'street' => '456 Billing Street',
    'city' => 'New York',
    'country_id' => 'US',
    'region' => 'NY',
    'postcode' => '10001',
    'telephone' => '9876543210',
];

try {
    // Create order
    /** @var State $state */
    $state = $objectManager->get(State::class);
    $state->setAreaCode(MagentoFrameworkAppArea::AREA_ADMINHTML);

    /** @var OrderInterface $order */
    $order = $objectManager->create(OrderInterface::class);
    $order->setCustomerId($customerId);
    $order->setStoreId($store->getId());
    $order->setCurrencyCode($currencyCode);

    // Add products to the order
    /** @var OrderItemInterface $orderItem */
    $orderItem = $objectManager->create(OrderItemInterface::class);
    $orderItem->setProductId($productId);
    $orderItem->setQtyOrdered($productQty);
    $orderItem->setPrice($productPrice);
    $orderItem->setBasePrice($productPrice);
    $orderItem->setRowTotal($productPrice * $productQty);
    $order->setItems([$orderItem]);

    // Validate the email format
    function isValidEmail($email)
    {
        return filter_var($email, FILTER_VALIDATE_EMAIL);
    }

    $shippingEmail = '[email protected]'; // Replace with your email address
    $billingEmail = '[email protected]'; // Replace with your email address

    // Set shipping address
    /** @var OrderAddressInterface $shippingAddress */
    $shippingAddress = $objectManager->create(OrderAddressInterface::class);
    $shippingAddressData['email'] = $shippingEmail;
    $shippingAddress->setData($shippingAddressData);
    $order->setShippingAddress($shippingAddress);

    // Set billing address
    /** @var OrderAddressInterface $billingAddress */
    $billingAddress = $objectManager->create(OrderAddressInterface::class);
    $billingAddressData['email'] = $billingEmail;
    $billingAddress->setData($billingAddressData);
    $order->setBillingAddress($billingAddress);

    // Set payment information
    /** @var OrderPaymentInterface $payment */
    $payment = $objectManager->create(OrderPaymentInterface::class);
    $payment->setMethod('checkmo');
    $order->setPayment($payment);

    // Save order
    $orderManagement = $objectManager->get(OrderManagementInterface::class);
    try {
        $orderManagement->place($order);
    } catch (Exception $e) {
        echo "Error :" . $e->getMessage() . PHP_EOL;
    }

    // Get the increment ID of the placed order
    $incrementId = $order->getIncrementId();
    echo "Order placed successfully. Increment ID: " . $incrementId . " Order ID: " . $order->getId() .PHP_EOL;
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . PHP_EOL;
}`

Error: We can't save the address: Email has a wrong format