magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.5k stars 9.3k forks source link

Create order programmatically with no item magento 2 dont work #10005

Closed goben2489 closed 7 years ago

goben2489 commented 7 years ago

Preconditions

  1. Magento 2.1.5

Steps to reproduce

I try create order by manual with no item, have only customer info, payment and shipping but order create has stage complete, i think it check quote have item or total > 0 but i have not find it

` public function execute() { $post = $this->getRequest()->getPostValue(); $this->inlineTranslation->suspend(); try { $postObject = new \Magento\Framework\DataObject(); $postObject->setData($post); $last_name=trim($post['name']); $first_name=trim($post['vorname']); $company_name=trim($post['firma']); $email=trim($post['email']); $telephone=trim($post['telephone']); $comment=trim($post['comment']); $status="rueckruf";

    $countryCode = $this->getConfig()->getValue('general/country/default');
    $currency_code = $this->_storeManager->getStore()->getCurrentCurrency()->getCode();
    $websiteId = $this->_storeManager->getStore()->getWebsiteId();
    $store = $this->_storeManager->getStore();
    $storeId= $this->_storeManager->getStore()->getId();

    // Setting customer
    $customer= $this->_customerFactory->create();
    $customer->setWebsiteId($websiteId);
    $customer->loadByEmail($email);

    if(!$customer->getEntityId()){
        //If not avilable then create this customer
        $customer->setWebsiteId($websiteId)
            ->setStore($store)
            ->setFirstname($first_name)
            ->setLastname($last_name)
            ->setEmail($email)
            ->setPassword($email);
        $customer->save();
    }
    $address = $this->_addressFactory->create();
    $shippingID = $customer->getDefaultShipping();
    if($shippingID != 0)
    {
        $address->load($shippingID);
    }

    $address->setCustomerId($customer->getEntityId())
        ->setFirstname($first_name)
        ->setLastname($last_name)
        ->setPostcode('no-content')
        ->setCity('city')
        ->setCountryId('DE')
        ->setTelephone($telephone)
        ->setFax($telephone)
        ->setCompany($company_name)
        ->setStreet('no-content')
        ->setIsDefaultBilling('1')
        ->setIsDefaultShipping('1')
        ->setSaveInAddressBook('1');
    $address->save();
    $shippingID = $customer->getDefaultShipping();
    $shippingData = $address->load($shippingID);
    $customer= $this->_customerRepository->getById($customer->getEntityId());
    //Creat order
    $quote=$this->_quoteFactory->create();
    $quote->setStoreId($storeId)
        ->assignCustomer($customer)
        ->setCustomerEmail($email);

    $billingAddress  = $quote->getBillingAddress()->addData($shippingData->getData());
    $shippingAddress = $quote->getShippingAddress()->addData($shippingData->getData());
    $shippingAddress->setCollectShippingRates(true)->collectShippingRates()
        ->setShippingMethod('abholung_abholung')
        ->setPaymentMethod('cashondelivery');

    $quote->getShippingAddress()->setShippingMethod('abholung_abholung');
    $quote->setPaymentMethod('cashondelivery');
    $quote->getPayment()->setQuote($quote);
    $quote->getPayment()->importData(['method' => 'cashondelivery']);
    $quote->setInventoryProcessed(false);
    $quote->save();

    $order = $this->orderFactory->create();
    $order ->setStoreId($storeId)
        ->setGlobal_currency_code($currency_code)
        ->setBase_currency_code($currency_code)
        ->setStore_currency_code($currency_code)
        ->setOrder_currency_code($currency_code);

    //Shipping Method to order
    $this->dataObjectHelper->mergeDataObjects(
        '\Magento\Sales\Api\Data\OrderInterface',
        $order,
        $this->quoteAddressToOrder->convert($quote->getBillingAddress(), array())
    );
    $shippingAddress = $this->quoteAddressToOrderAddress->convert(
        $quote->getShippingAddress(),
        [
            'address_type' => 'shipping',
            'email' => $quote->getCustomerEmail()
        ]
    );
    $addresses[] = $shippingAddress;
    $order->setShippingAddress($shippingAddress);
    $order->setShippingMethod($quote->getShippingAddress()->getShippingMethod());
    //Billing Address to order
    $billingAddress = $this->quoteAddressToOrderAddress->convert(
        $quote->getBillingAddress(),
        [
            'address_type' => 'billing',
            'email' => $quote->getCustomerEmail()
        ]
    );
    $addresses[] = $billingAddress;
    $order->setBillingAddress($billingAddress);
    $order->setAddresses($addresses);
    $order->setPayment($this->quotePaymentToOrderPayment->convert($quote->getPayment()));
    if ($quote->getCustomer()) {
        $order->setCustomerId($quote->getCustomer()->getId());
    }
    $order->setCustomerEmail($email);
    $order->setCustomerFirstname($first_name);
    $order->setCustomerLastname($last_name);
    $order->setStatusButton(1);
    $order->setGrandTotal(0);
    $order->setBaseToGlobalRate(1);
    $order->setBaseToOrderRate(1);
    $order->setBaseShippingAmount(0);
    $order->setBaseShippingTaxAmount(0);
    $order->setStoreToBaseRate(0);
    $order->setStoreToBaseRate(0);
    $order->setCustomerIsGuest(0);
    $order->setCustomerNoteNotify(1);
    $order->setBaseShippingDiscountAmount(0);
    $order->setShippingDiscountAmount(0);
    $order->setShippingTaxAmount(0);
    $order->setState('new');
    $order->setStatus($status);
    $order->addStatusToHistory($status, '', false);
    $order->addStatusToHistory($status, $comment, false);
    $order->save();
    $this->inlineTranslation->resume();
    $this->messageManager->addSuccess(
        __('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.')
    );
    $this->_redirect('kontakt/rueckruf.html/');
    return;
} catch (\Exception $e) {
    $this->inlineTranslation->resume();
    $this->messageManager->addError(
        __('Unable to submit your request. Please, try again later'.$e->getMessage())
    );
    $this->_redirect('kontakt/rueckruf.html/');
    return;
}

}`

Expected result

  1. [Screenshot, logs] h11 Order create has stage complete and null status
korostii commented 7 years ago

This is very interesting, but why would you even want an order with no items inside? Have you tried creating an order with at least a virtual item inside?

I doubt any of the behavior you seem to be relying on is covered by @api annotations, and I don't think it is symptomatic of any bugs neither. When doing a deep customization like that, you're largely on your own. The GitHub issue tracker is only intended for technical issues resulting from underlying bugs in the Magento software.

Please refer to the Community Forums or the Magento Stack Exchange for technical questions and advice regarding development.

goben2489 commented 7 years ago

because request from my customer, they want user can request a order but no item, they manage order by multi status, they want doing with user first by phone, after they add product and complete order.

I found resolve problem at Magento\Sales\Model\ResourceModel\Order

public function save(\Magento\Framework\Model\AbstractModel $object)
    {
        /** @var \Magento\Sales\Model\Order $object */
        //$this->stateHandler->check($object); -> this is check order and replace complete if have not item
        return parent::save($object);
    }
magento-engcom-team commented 7 years ago

@goben2489 please refer to the Community Forums or the Magento Stack Exchange site for advice or general discussion about this issue. The GitHub issue tracker is intended for Magento Core technical issues only.