-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Description
class OrderCreate
{
public function __construct(
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
\Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Catalog\Model\ProductFactory $_productloader,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Catalog\Model\ProductFactory $productFactory,
\Magento\Quote\Model\QuoteManagement $quoteManagement,
\Magento\Customer\Model\CustomerFactory $customerFactory,
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
\Magento\Sales\Model\Service\OrderService $orderService,
\Magento\Quote\Api\CartRepositoryInterface $cartRepositoryInterface,
\Magento\Quote\Api\CartManagementInterface $cartManagementInterface,
\Magento\Quote\Model\Quote\Address\Rate $shippingRate,
\Magento\Framework\Data\Form\FormKey $formkey,
\Magento\Quote\Model\QuoteFactory $quote
)
{
$this->_transportBuilder = $transportBuilder;
$this->resultJsonFactory = $resultJsonFactory;
$this->_scopeConfig = $scopeConfig;
$this->_productloader = $_productloader;
$this->_storeManager = $storeManager;
$this->_productFactory = $productFactory;
$this->quoteManagement = $quoteManagement;
$this->customerFactory = $customerFactory;
$this->customerRepository = $customerRepository;
$this->orderService = $orderService;
$this->cartRepositoryInterface = $cartRepositoryInterface;
$this->cartManagementInterface = $cartManagementInterface;
$this->shippingRate = $shippingRate;
$this->_formkey = $formkey;
$this->quote = $quote;
}
/**
* Create Order On Your Store
*
* @return int $orderId
*
*/
public function createOrder($_product, $telephone, $customer_email = false)
{
//init the store id and website id
$store = $this->_storeManager->getStore();
$websiteId = $this->_storeManager->getStore()->getWebsiteId();
//init the customer
$customer = $this->customerFactory->create();
if (!$customer_email) {
$conf = $this->_scopeConfig->getValue(
'callback',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
$customer_email = $conf['general']['callback_email'];
}
$customer->setWebsiteId($websiteId);
$customer->loadByEmail($customer_email); // load customer by email address
//init the quote
$orderShipping = $customer->getDefaultShippingAddress();
$orderShipping->setTelephone($telephone);
$quote = $this->quote->create(); //Create object of quote
$quote->setStore($store); //set store for which you create quote
// if you have allready buyer id then you can load customer directly
$customer = $this->customerRepository->getById($customer->getEntityId());
$quote->setCurrency();
$quote->assignCustomer($customer); //Assign quote to customer
//add item in quote
$quote->addProduct(
$_product,
1
);
$this->cartRepositoryInterface->save($quote); // Add this
//set shipping adress from customer to array
$arrShippingAddress = [
'firstname' => $customer->getFirstName(), //address Details
'lastname' => $customer->getLastName(),
'street' => $orderShipping->getStreet(),
'city' => $orderShipping->getCity(),
'country_id' => $orderShipping->getCountry(),
'postcode' => $orderShipping->getPostcode(),
'telephone' => $orderShipping->getTelephone(),
'save_in_address_book' => 1];
//Set Address to quote
$quote->getBillingAddress()->addData($arrShippingAddress);
$quote->getShippingAddress()->addData($arrShippingAddress);
// Collect Rates and Set Shipping & Payment Method
$shippingAddress=$quote->getShippingAddress();
$shippingAddress->setCollectShippingRates(true)
->collectShippingRates()
->setShippingMethod('flatrate_flatrate'); //shipping method
$quote->setPaymentMethod('banktransfer'); //payment method
$quote->getPayment()->setMethod('banktransfer');
$quote->setInventoryProcessed(false); //not effetc inventory
$quote->save(); //Now Save quote and your quote is ready
// Set Sales Order Payment
$quote->getPayment()->importData(['method' => 'banktransfer']);
// Collect Totals & Save Quote
$quote->collectTotals();
// Create Order From Quote
$quote = $this->cartRepositoryInterface->get($quote->getId());
$order = $this->cartManagementInterface->submit($quote);
$order->setEmailSent(0);
$increment_id = $order->getRealOrderId();
return $increment_id;
}
This is my code and it work on 2.1.4, after upgrade to 2.1.7 it don't work at all and throw error:
Fatal error: Uncaught Exception: User Error: Some transactions have not been committed or rolled back in /var/www/preprod.romb.ua/releases/20170629144717Z/public/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php on line 3764 in /var/www/preprod.romb.ua/releases/20170629144717Z/public/vendor/magento/framework/App/ErrorHandler.php:61 Stack trace: #0 [internal function]: Magento\Framework\App\ErrorHandler->handler(256, 'Some transactio...', '/var/www/prepro...', 3764, Array) #1 /var/www/preprod.romb.ua/releases/20170629144717Z/public/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php(3764): trigger_error('Some transactio...', 256) #2 [internal function]: Magento\Framework\DB\Adapter\Pdo\Mysql->__destruct() #3 {main} thrown in /var/www/preprod.romb.ua/releases/20170629144717Z/public/vendor/magento/framework/App/ErrorHandler.php on line 61