Gekissimo.net - Opi ansaitsemaan rahaa webilläsi Internetissä!

Tarjouksen luominen & Tilaa ohjelmallisesti Magento 2:ssa

Seuraava artikkeli auttaa sinua: Tarjouksen luominen & Tilaa ohjelmallisesti Magento 2:ssa

nimiavaruusOmaNimiSpace\Moduulinimi\Apu;

käytä Magento\Framework\App\Helper\AbstractHelper;

luokka Data laajentaa AbstractHelperiä

{

julkinen funktio __construct(\Magento\Framework\App\Helper\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Model\Product $product, \Magento\Framework\Data\FormKe $y muotoavaimen les \Model\Service\OrderService $orderService)

{

$this->storeManager = $storeManager;

$tämä->tuote = $tuote;

$this->formkey = $lomakeavain;

$this->quote = $lainaus;

$this->quoteManagement = $quoteManagement;

$this->customerFactory = $customerFactory;

$this->customerRepository = $customerRepository;

$this->orderService = $tilauspalvelu;

vanhempi::__konstrukti($konteksti);

}

julkinen toiminto createOrder($order)

{

$kauppa = $this->storeManager->getStore();

$websiteId = $this->storeManager->getStore()->getWebsiteId();

$asiakas = $this->customerFactory->luo();

$asiakas->setWebsiteId($websiteId);

$asiakas->loadByEmail($tilaus[’email’]); // lataa customet sähköpostiosoitteen mukaan

if (!$customer->getEntityId()) {

//Jos ei ole saatavilla, luo tämä asiakas

$customer->setWebsiteId($websiteId)->setStore($store)->setFirstname($order[‘shipping_address’][‘firstname’])->setLastname($order[‘shipping_address’][‘lastname’])->setEmail($tilaus[’email’])->setPassword($order[’email’]);

$asiakas->tallenna();

}

$lainaus = $this->quote->create(); // Luo lainausobjekti

$quote->setStore($kauppa); // Aseta Store

$asiakas = $this->customerRepository->getById($customer->getEntityId());

$quote->setCurrency();

$quote->assignCustomer($customer); // Anna tarjous asiakkaalle

//lisää kohteita lainaukseen

foreach ($order[‘items’] $kohteena) {

$tuote = $this->product->load($item[‘product_id’]);

$tuote->setPrice($tuote[‘price’]);

$quote->addProduct($tuote, intval($item[‘qty’]));

}

$quote->getBillingAddress()->addData($order[‘shipping_address’]);

$quote->getShippingAddress()->addData($tilaus[‘shipping_address’]);

// Kerää hinnat ja aseta toimitus- ja maksutapa

$lähetysosoite = $lainaus->getToimitusosoite();

$shippingAddress->setCollectShippingRates(true)->collectShippingRates()->setShippingMethod(‘freeshipping_freeshipping’);

$quote->setPaymentMethod(‘checkmo’);

$quote->setInventoryProcessed(false);

$lainaus->tallenna();

// Aseta myyntitilauksen maksu

$quote->getPayment()->importData([‘method’ => ‘checkmo’]);

// Kerää summat ja tallenna tarjous

$quote->collectTotals()->tallenna();

// Luo tilaus tarjouksesta

$tilaustiedot = $this->quoteManagement->submit($quote);

$tilaustiedot->setEmailSent(0);

$lisäystunnus = $tilaus->getRealTilauksenId();

if ($orderdata->getEntityId()) {

$tulos[‘order_id’] = $tilaustiedot->getRealOrderId();

} muu {

$tulos = [‘error’ => 1, ‘msg’ => ‘Your custom message’];

}

palauttaa $tulos;

}

}

?>

🔥 Empfohlen:  Kuinka integroida Facebook Shop Magento 2:een [2023]