Şu yazıyı okuyorsunuz: Magento 2’de Programlı Olarak Teklif Oluşturma ve Sipariş Verme
namespaceYourNamespace\ModülAdı\Yardımcı;
Magento\Framework\App\Helper\AbstractHelper’ı kullanın;
Veri sınıfı AbstractHelper’ı genişletiyor
{
public function __construct(\Magento\Framework\App\Helper\Context $bağlam, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Model\Product $ürün, \Magento\Framework\Data\Form\FormKey $ formkey, \Magento\Quote\Model\QuoteFactory $quote, \Magento\Quote\Model\QuoteManagement $quoteManagement, \Magento\Customer\Model\CustomerFactory $customerFactory, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, \Magento\Sales \Model\Service\ServiceOrder $serviceorder)
{
$this->storeManager = $storeManager;
$this->ürün = $ürün;
$this->formkey = $formkey;
$this->alıntı = $alıntı;
$this->quoteManagement = $quoteManagement;
$this->customerFactory = $customerFactory;
$this->müşteriRepository = $customerRepository;
$this->orderService = $orderService;
parent::__construction($context);
}
genel işlev createOrder($sipariş)
{
$mağaza = $this->storeManager->getStore();
$websiteId = $this->storeManager->getStore()->getWebsiteId();
$müşteri = $this->customerFactory->create();
$client->setWebsiteId($websiteId);
$müşteri->loadByEmail($order[’email’]); // istemciyi e-posta adresine göre yükle
if (!$müşteri->getEntityId()) {
//Mevcut değilse bu istemciyi oluşturun
$customer->setWebsiteId($websiteId)->setStore($store)->setFirstname($order[‘shipping_address’][‘firstname’])->setSoyadı($sipariş[‘shipping_address’][‘lastname’])->setEmail($sipariş[’email’])->setPassword($sipariş[’email’]);
$istemci->kaydet();
}
$alıntı = $this->alıntı->oluştur(); // Alıntı nesnesi oluştur
$bütçe->setStore($mağaza); // Mağazayı ayarla
$client = $this->clientrepository->getById($client->getEntityId());
$bütçe->setCurrency();
$bütçe->atamaMüşteri($müşteri); // Müşteriye teklif ata
//alıntıya öğeler ekliyoruz
foreach ($sipariş[‘items’] $öğe olarak) {
$ürün = $this->ürün->yük($item[‘product_id’]);
$product->setPrice($item[‘price’]);
$budget->addProduct($product, intval($item)[‘qty’]));
}
$budget->getBillingAddress()->addData($order[‘shipping_address’]);
$quote->getShippingAddress()->addData($order[‘shipping_address’]);
// Ücretleri toplayın ve gönderim ve ödeme yöntemlerini ayarlayın
$shippingaddress = $quote->getShippingAddress();
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()->setShippingMethod(‘freeshipping_freeshipping’);
$budget->setPaymentMethod(‘checkmo’);
$budget->setInventoryProcessed(false);
$alıntı->kaydet();
//Satış siparişi ödemesini ayarlayın
$budget->getPayment()->importData([‘method’ => ‘checkmo’]);
// Toplamları topla ve teklifi kaydet
$quote->collectTotals()->save();
// Tekliften sipariş oluştur
$siparişverisi = $this->quoteManagement->gönder($quote);
$orderdata->setEmailSent(0);
$increment_id = $order->getRealOrderId();
if ($orderdata->getEntityId()) {
$sonuç[‘order_id’] = $siparişverisi->getRealOrderId();
} geri kalan {
$sonuç = [‘error’ => 1, ‘msg’ => ‘Your custom message’];
}
$sonucu döndür;
}
}
?>