Magento 2.4:为每个订单创建 CSV 文件

Magento 2.4: create CSV File for each order placed

我是 magento 的新手。每当客户下订单时,我都需要自动创建一个 csv 文件。
我正在使用事件“sales_order_place_after”,但观察器中的代码不起作用。
任何人都可以指导我如何在下订单时成功生成 csv 文件。
我不断收到如下错误: class Magento\Sales\Model\Order\Address 的对象无法转换为字符串

Class Order implements ObserverInterface
{

    protected $_request;
    protected $_order;
    protected $_productRepository;
    protected $_scopeConfig;
    protected $_customer;
    protected $_storemanager;
 
    public function __construct(
        \Magento\Framework\App\RequestInterface $request,
        \Magento\Sales\Model\Order $order,
        \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
        Filesystem $filesystem,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Customer\Model\Customer $customer,
        \Magento\Store\Model\StoreManagerInterface $storemanager,

        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Sales\Model\OrderFactory $orderFactory,
        \Magento\Framework\ObjectManager\ObjectManager $objectManager,

        
        \Psr\Log\LoggerInterface $logger,
        \Magento\Catalog\Model\ProductFactory $productFactory,

        \Magento\Catalog\Model\ProductRepository $productRepository

        

    ) { 
        $this->_scopeConfig = $scopeConfig;
        $this->_customer = $customer;
        $this->_storemanager = $storemanager;
        $this->_request = $request;
        $this->_order = $order;
        $this->_fileFactory = $fileFactory;
        $this->directory = $filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
        $this->_productRepository = $productRepository;
        
    }
 
    public function execute(\Magento\Framework\Event\Observer $observer) {
        
        
        
        $order = $observer->getEvent()->getOrder();
    

 
        $websiteID = $this->_storemanager->getStore()->getWebsiteId();


        $headers = array( 'Customer Name', 'Customer Email', ' Phone','Shipping Address' ,'SKU','Quantity','Price','Total','Weight');

           
                
            
            $name = strtotime('now');
            $file = 'customorderexport/'.$name.'_detailed_orderexport.csv';
            $this->directory->create('customorderexport');
            $stream = $this->directory->openFile($file, 'w+');
            $stream->lock();
            $stream->writeCsv($headers);
            
         //   $orderdetail['Customer Name'] = 
            $orderdetail['Customer Email'] = $order->getCustomerEmail();
            $orderdetail['Contact Phone'] = $order->getTelephone();

            $orderdetail['Shipping Address'] = $order->getShippingAddress();
        
        

            $items = $order->getAllItems();
            foreach ($items as $item) {
            

                $orderdetail['SKU'] = $item->getSKU();
              
                $orderdetail['Quantity'] = $item->getQtyOrdered();
                $orderdetail['Price'] = $item->getPrice();
                $orderdetail['Total'] = $item->getGrandTotal();
               $orderdetail['Weight'] = $item->getWeight();
                $stream->writeCsv($orderdetail);
            }

            $stream->unlock();
            $stream->close();
        }
    
 

}

这是因为

$order->getShippingAddress();

将 return 一个 OrderAddressInterface 对象。 (Magento\Sales\Api\Data\OrderAddressInterface)

例如,您可以执行以下操作:

$shippingAddress = $order->getShippingAddress();
$orderdetail['Shipping Address'] = $shippingAddress->getStreet()[0];