Question

I'm implementing two api. The first one create an empty cart and assign a product to it. Then wrap the cart id with a QuoteMask id and retrive it.

The second api load the cart from the quotemaskid, set a customer then place an order.

The first api works fine, but when i try the second one it never ends. The strange thing is that the order is placed with correct data.

I paste the code of the first and second api.

first api

    public function createCart($dealerCode, VehicleInterface $vehicle, $lang)
{
    // creazione prodotto virtuale sulla base dei dati ricevuti in $vehicle
    // se esiste lo recuperiamo utilizzando lo sku
    // altrimenti lo creiamo (il prodotto sarà virtuale)
    try {
        $product = $this->productRepository->get($vehicle->getVehicleVin());
    } catch (NoSuchEntityException $e) {
        $product = $this->productFactory->create(['\Magento\Catalog\Model\Product']);
        $product->setSku($vehicle->getVehicleVin());
        $product->setName($vehicle->getVehicleDescription());
        $product->setPrice($vehicle->getBookingPrice());
        $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL);
        $product->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
        $product->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
        $product->setStockData(
            [
                'use_config_manage_stock' => 0,
                'manage_stock' => 1,
                'is_in_stock' => 1,
                'qty' => 100
            ]
        );
        $product->save();
    }

    // Generiamo un carrello vuoto e gli assegniamo un QuoteIdMask, utile per gestire il carrello Guest
    /** @var $quoteIdMask \Magento\Quote\Model\QuoteIdMask */
    $quoteIdMask = $this->quoteIdMaskFactory->create();
    $cartId = $this->quoteManagement->createEmptyCart();
    $quoteIdMask->setQuoteId($cartId)->save();

    // aggiunta al carrello del prodotto virtuale precedentemente creato
    $cart = $this->cartRepository->get($cartId);
    $cart->addProduct($product);
    $cart->save();

    // Restituzione dell'Id maschera per il carrello creato
    return $quoteIdMask->getMaskedId();
}

second api

    public function placeOrder(
    $cartId,
    $dealerCode,
    CustomerInterface $customerData,
    PaymentInterface $paymentMethod,
    $timeout1,
    $timeout2,
    $timeout1Hours,
    $timeout2Hours,
    $personalPageUrl,
    $thankyouPageUrl,
    $agreementsPageUrl,
    $lang
) {
    $store=$this->storeManager->getStore();
    $websiteId = $this->storeManager->getStore()->getWebsiteId();

    // Recupero carrello
    /** @var $quoteIdMask QuoteIdMask */
    $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
    $quote = $this->cartRepository->get($quoteIdMask->getQuoteId());


    // Creazione customer
    try {
        $customer = $this->customerRepository->get($customerData->getEmail());
    } catch (NoSuchEntityException $e) {
        $customer = $this->customerFactory->create();
        $customer->setWebsiteId($websiteId);
        $customer->setFirstname($customerData->getFirstname());
        $customer->setLastname($customerData->getLastname());
        $customer->setEmail($customerData->getEmail());
        //$this->customerRepository->save($customer);
    }

    $quote->setStore($store);
    $quote->setCustomer($customer);
    $quote->setCustomerIsGuest(1);
    // Creazione shipping address e method
    $quote->getBillingAddress()->addData(self::ADDRESS['billing_address']);
    $quote->getShippingAddress()->addData(self::ADDRESS['shipping_address']);

    // Collect Rates and Set Shipping & Payment Method
    $this->shippingRate
        ->setCode('freeshipping_freeshipping')
        ->getPrice(1);
    $shippingAddress = $quote->getShippingAddress();

    $shippingAddress->setCollectShippingRates(true)
        ->collectShippingRates()
        ->setShippingMethod('flatrate_flatrate'); //shipping method
    $quote->getShippingAddress()->addShippingRate($this->shippingRate);
    $quote->setPaymentMethod($paymentMethod->getMethod()); //payment method
    $quote->setInventoryProcessed(false); //not effect inventory

    // Set Sales Order Payment
    $quote->getPayment()->importData(['method' => 'checkmo']);
    $quote->collectTotals()->save();

    // Create Order From Quote
    $order = $this->quoteManagement->placeOrder($quote->getId());

    return $order;
}
Was it helpful?

Solution

The problem is that after i place the order, Magento try to send an email. This was the reason to the never ending order placing.

In the backend I've disable this function setting yes in Magento > Stores > Configuration > Advanced > System > Disable Email Communications

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top