Home » Php » php – A server error stopped your order from being placed. Please try to place your order again

php – A server error stopped your order from being placed. Please try to place your order again

Posted by: admin February 25, 2020 Leave a comment

Questions:

i have created a module for creating a Quick order so customer can place there order without registering or login. i am getting 5 fields from customers to process their order and its working fine. i have debug the code below and its working fine except
$order_id = $cartManagementInterface->placeOrder($cart->getId());
this line of code is getting error of A server error stopped your order from being placed. Please try to place your order again. i dont know the issue right now and looking for quick solution. when i try to check the log i found the following error {“0″:”A server error stopped your order from being placed. Please try to place your order again.”,”1″:”

#1 Magento\Quote\Model\QuoteManagement\Interceptor->___callParent('placeOrder', array(11804, NULL)) called at [vendor/magento/framework/Interception/Interceptor.php:138]\n#2 Magento\Quote\Model\QuoteManagement\Interceptor->Magento\Framework\Interception\{closure}(11804, NULL) called at [vendor/magento/module-braintree/Plugin/OrderCancellation.php:63]\n#3 Magento\Braintree\Plugin\OrderCancellation->aroundPlaceOrder(&Magento\Quote\Model\QuoteManagement\Interceptor#0000000068771282000000001567ed28#, &Closure#0000000068771726000000001567ed28#, 11804) called at [vendor/magento/framework/Interception/Interceptor.php:135]\n#4 Magento\Quote\Model\QuoteManagement\Interceptor->Magento\Framework\Interception\{closure}(11804) called at [vendor/magento/framework/Interception/Interceptor.php:153]\n#5 Magento\Quote\Model\QuoteManagement\Interceptor->___callPlugins('placeOrder', array(11804), NULL) called at [generated/code/Magento/Quote/Model/QuoteManagement/Interceptor.php:26]

namespace QuickOrder\QuickBuy\Controller\Index;
use Magento\Framework\App\Bootstrap;

class CustomOrders extends \Magento\Framework\App\Action\Action
{
protected $_pageFactory;
protected $_helper;
//protected $_data;

public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $pageFactory,
\QuickOrder\QuickBuy\Helper\Data $helper)
{
$this->_pageFactory = $pageFactory;
$this->_helper = $helper;
return parent::__construct($context);
}

public function execute()
{
$email = "[email protected]";
$fname = "fname";
$lname = "lname";
$password = "password";
$product_id = 1036;

$object = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $object->get('Magento\Store\Model\StoreManagerInterface');
$customerFactory = $object->get('Magento\Customer\Model\CustomerFactory');
$cartManagementInterface = $object->get('Magento\Quote\Api\CartManagementInterface');
$cartRepositoryInterface = $object->get('Magento\Quote\Api\CartRepositoryInterface');
$customerRepository= $object->get('Magento\Customer\Api\CustomerRepositoryInterface');
$shippingRate= $object->get('Magento\Quote\Model\Quote\Address\Rate');
$productFactory=$object->get('Magento\Catalog\Model\ProductFactory');

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

$customer=$customerFactory->create();
$customer->setWebsiteId($websiteId);
$customer->loadByEmail($email);

if(!$customer->getEntityId()){
$customer->setWebsiteId($websiteId)
->setStore($store)
->setFirstname($fname)
->setLastname($lname)
->setEmail($email)
->setPassword($password);
$customer->save();
}

$cart_id = $cartManagementInterface->createEmptyCart();
$cart = $cartRepositoryInterface->get($cart_id);
$cart->setStore($store);

$customer= $customerRepository->getById($customer->getEntityId());
$cart->setCurrency();
$cart->assignCustomer($customer);

$product = $productFactory->create()->load($product_id);
$cart->addProduct($product,1);

$orderData =[
'currency_id' => 'PKR',
'email' => '[email protected]', //buyer email id
'shipping_address' =>[
'firstname' => 'John', //address Details
'lastname' => 'Deo',
'street' => 'Main Street',
'city' => 'Lahore',
'country_id' => 'PK',
'region' => 'Punjab',
'postcode' => '85001',
'telephone' => '823322565',
'fax' => '3245845623',
'shipping_method' => 'flatrate',
'save_in_address_book' => 1
],
'items'=> [
//array of product which order you want to create
['product_id'=>'1036','price'=> '899','qty'=>2] ] ];
$cart->getBillingAddress()->addData($orderData['shipping_address']);
$cart->getShippingAddress()->addData($orderData['shipping_address']);

$shippingRate->setCode('flatrate_flatrate')->getPrice(1);
$cart->getShippingAddress()->addShippingRate($shippingRate);
$cart->setPaymentMethod('cashondelivery');
$cart->setInventoryProcessed(false);
$cart->getPayment()->importData(['method' => 'cashondelivery']);
$cart->collectTotals();
$cart->save();
$cart = $cartRepositoryInterface->get($cart->getId());
$order_id = $cartManagementInterface->placeOrder($cart->getId());

return $order_id;

}
}

How to&Answers: