1

I am modifying the placeOrder function of the QuoteManagement class, based on some information I found through searches, I was able to change the method so that an order is generated for each seller, but the values are kept the same, ie I get the value total of all items and not just the value of the already separated items. Can someone tell me what place or what should I do so that the total amount is separated according to the items of each order?

PlaceOrder

public function placeOrder($cartId, PaymentInterface $paymentMethod = null)
{
    $quote = $this->quoteRepository->getActive($cartId);

    $paymentMethodString = $quote->getPayment()->getMethod(); // edit 19.10.17

    // get data from addresses and remove ids
    $billingAddress = $quote->getBillingAddress()->getData();
    $shippingAddress = $quote->getShippingAddress()->getData();
    unset($billingAddress['id']);
    unset($billingAddress['quote_id']);
    unset($shippingAddress['id']);
    unset($shippingAddress['quote_id']);

    $itemsPerVendor = [];
    foreach($quote->getAllItems() as $item) {
        // I don't know how do you keep track of your product vendor, use this as reference only
        $product_id = $item->getProduct()->getId();
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $product = $objectManager->create('Magento\Catalog\Model\Product')->load($product_id);

        $itemsPerVendor[$product->getSellerId()][] = $item;
    }

    foreach($itemsPerVendor as $vendor => $items) {
        // init Quote Split
        $quoteSplit = $this->quoteFactory->create();
        $quoteSplit->setStoreId($quote->getStoreId());
        $quoteSplit->setCustomer($quote->getCustomer());
        $quoteSplit->setCustomerIsGuest($quote->getCustomerIsGuest());
        if ($quote->getCheckoutMethod() === self::METHOD_GUEST) {
            $quoteSplit->setCustomerEmail($quote->getBillingAddress()->getEmail());
            $quoteSplit->setCustomerGroupId(\Magento\Customer\Api\Data\GroupInterface::NOT_LOGGED_IN_ID);
        }

        // save quoteSplit in order to have a quote id for item
        $this->quoteRepository->save($quoteSplit);

        $subtotal = 0;  

        foreach ($items as $item) {
            // add item
            $item->setId(null); // init item id for force to be added to quoteSplit collection
            $quoteSplit->addItem($item);           
        }

        // set addresses
        $quoteSplit->getBillingAddress()->setData($billingAddress);
        $quoteSplit->getShippingAddress()->setData($shippingAddress);


    // recollect totals into the quote
    $quoteSplit->setTotalsCollectedFlag(false)->collectTotals();

        // set payment method // edit 19.10.17
        $quoteSplit->getPayment()->setMethod($paymentMethodString);
        if ($paymentMethod) {
            $quoteSplit->getPayment()->setQuote($quoteSplit);
            $data = $paymentMethod->getData();
            $quoteSplit->getPayment()->importData($data);
        }

        $this->quoteRepository->save($quoteSplit);
        // dispatch this event as Magento standard once per each quote split
        $this->eventManager->dispatch('checkout_submit_before', ['quote' => $quoteSplit]);

        $order = $this->submit($quoteSplit);
        $orders[] = $order;


        if (null == $order) {
            throw new LocalizedException(
                __('An error occurred on the server. Please try to place the order again.')
            );
        }

    // disable origin quote
    $quote->setIsActive(false);
    $this->quoteRepository->save($quote); // edit 19.10.17

    $this->checkoutSession->setLastQuoteId($quoteSplit->getId());
    $this->checkoutSession->setLastSuccessQuoteId($quoteSplit->getId());
    $this->checkoutSession->setLastOrderId($order->getId());
    $this->checkoutSession->setLastRealOrderId($order->getIncrementId());
    $this->checkoutSession->setLastOrderStatus($order->getStatus());

    $this->eventManager->dispatch('checkout_submit_all_after', ['orders' => $orders, 'quote' => $quote]);

}
    /**
     * the API declaration and interface describe this function returning int, you can't return an array.
     * in order to do that you will have to create a new end point for that.
     */
    return;
}

How can I set grand total and subtotal of each order after order split?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.