1

I have event_id column in my sales_order table. I want to place an order & I am not sending event_id because its zero by default. When am trying to place an order, Its throwing me the error Column not found: 1054 Unknown column 'so.event_id' in 'where clause', query was: INSERT INTO sales_order ...

enter image description here

I have already run all cache & upgrade commands many times but I don't know why its doing this. Here is my code:

public function createMageOrder($orderData) {
        $store = $this->_storeManager->getStore();
        $websiteId = $this->_storeManager->getStore()->getWebsiteId();
        $customer = $this->customerFactory->create();
        $customer->setWebsiteId($websiteId);
        $customer->loadByEmail($orderData["entity"]["customer_email"]); // load customet by email address
        if (!$customer->getEntityId()) {
            echo "Customer does not exist. please create here...";
            exit;
            //If not avilable then create this customer 
            $customer->setWebsiteId($websiteId)
                    ->setStore($store)
                    ->setFirstname($orderData['shipping_address']['firstname'])
                    ->setLastname($orderData['shipping_address']['lastname'])
                    ->setEmail($orderData['email'])
                    ->setPassword($orderData['email']);
            $customer->save();
        }
        $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 items in quote
        foreach ($orderData["entity"]["items"] as $item) {
            $product = $this->_product->load($item['product_id']);
            $product->setPrice($item['price']);
            $quote->addProduct(
                    $product, intval($item['qty_ordered'])
            );
        }

        //Set Address to quote
        $quote->getBillingAddress()->addData($orderData["entity"]["billing_address"]);
        $quote->getShippingAddress()->addData($orderData["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]);

        // Collect Rates and Set Shipping & Payment Method
        $shippingAddress = $quote->getShippingAddress();
        $shippingAddress->setCollectShippingRates(true)
                ->collectShippingRates()
                ->setShippingMethod($orderData["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["method"]); //shipping method
        $quote->setPaymentMethod($orderData["entity"]["payment"]["method"]); //payment method
        $quote->setInventoryProcessed(false); //not effetc inventory
        $quote->save(); //Now Save quote and your quote is ready
        // Set Sales Order Payment
        $quote->getPayment()->importData(['method' => $orderData["entity"]["payment"]["method"]]);
//        $quote->setData('event_id', $orderData["entity"]["event_id"]);
        // Collect Totals & Save Quote
        $quote->collectTotals()->save();

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

        $order->setEmailSent(0);
        $increment_id = $order->getRealOrderId();
        if ($order->getEntityId()) {
            $result['order_id'] = $order->getRealOrderId();
        } else {
            $result = ['error' => 1, 'msg' => 'Your custom message'];
        }
        return $result;
    }

I am calling this function in placeGTOrder($post) function where $post is my $orderData. Here is the function on which am sending my request as an action:

public function placeGTOrder($post) {
        $formData = $this->_helper->unserializeForm($post['formData']);
        $order = array();

        //fill cart items to order items 
        $cart = $this->_objectManager->get('\Magento\Checkout\Model\Cart');
        $itemsCollection = $cart->getQuote()->getItemsCollection();
        $itemsVisible = $cart->getQuote()->getAllVisibleItems();
        $items = $cart->getQuote()->getAllItems();
        $getItems = [];
        foreach ($items as $item) {
            $collect = array();
            $collect["base_original_price"] = 45;
            $collect["base_price"] = 45;
            $collect["base_price_incl_tax"] = 45;
            $collect["base_row_invoiced"] = 0;
            $collect["base_row_total"] = 45;
            $collect["base_tax_amount"] = 0;
            $collect["base_tax_invoiced"] = 0;
            $collect["discount_amount"] = 4.5;
            $collect["discount_percent"] = 10;
            $collect["free_shipping"] = 0;
            $collect["is_virtual"] = 0;
            $collect["name"] = $item->getName();
            $collect["original_price"] = 45;
            $collect["price"] = 45;
            $collect["price_incl_tax"] = 45;
            $collect["product_id"] = $item->getProductId();
            $collect["product_type"] = "simple";
            $collect["qty_ordered"] = $item->getQty();
            $collect["row_total"] = 45;
            $collect["row_total_incl_tax"] = 45;
            $collect["sku"] = $item->getSku();
            $collect["store_id"] = 1;
            $getItems[] = $collect;
        }


//        $order["entity"]["event_id"] = $formData["eventID"];
        $order["entity"]["base_currency_code"] = "USD";
        $order["entity"]["base_discount_amount"] = -4.5;
        $order["entity"]["base_grand_total"] = 45.5;
        $order["entity"]["base_shipping_amount"] = 5;
        $order["entity"]["base_subtotal"] = 45;
        $order["entity"]["base_tax_amount"] = 0;
        $order["entity"]["customer_email"] = urldecode($formData['Email']);
        $order["entity"]["customer_firstname"] = $formData['FirstName'];
        $order["entity"]["customer_group_id"] = 3;
        $order["entity"]["customer_id"] = $formData['selected_customer_id'];
        $order["entity"]["customer_is_guest"] = 0;
        $order["entity"]["customer_lastname"] = $formData['LastName'];
        $order["entity"]["customer_note_notify"] = 1;
        $order["entity"]["discount_amount"] = -4.5;
        $order["entity"]["email_sent"] = 1;
        $order["entity"]["coupon_code"] = "";
        $order["entity"]["discount_description"] = "Test1";
        $order["entity"]["grand_total"] = 45.5;
        $order["entity"]["is_virtual"] = 0;
        $order["entity"]["order_currency_code"] = "USD";
        $order["entity"]["shipping_amount"] = 5;
        $order["entity"]["shipping_description"] = "Flat Rate - Fixed";
        $order["entity"]["state"] = "new";
        $order["entity"]["status"] = "pending";
        $order["entity"]["store_currency_code"] = "USD";
        $order["entity"]["store_id"] = 1;
        $order["entity"]["store_name"] = "Main Website\nMain Website Store\n";
        $order["entity"]["subtotal"] = 45;
        $order["entity"]["subtotal_incl_tax"] = 45;
        $order["entity"]["tax_amount"] = 0;
        $order["entity"]["total_item_count"] = 1;
        $order["entity"]["total_qty_ordered"] = 1;
        $order["entity"]["weight"] = 1;
        $order["entity"]["items"] = $getItems;
        $order["entity"]["billing_address"]["address_type"] = "billing";
        $order["entity"]["billing_address"]["city"] = urldecode($formData['MainAddress.City']);
        $order["entity"]["billing_address"]["company"] = "";
        $order["entity"]["billing_address"]["country_id"] = urldecode($formData["MainAddress.Country"]);
        $order["entity"]["billing_address"]["email"] = urldecode($formData['Email']);
        $order["entity"]["billing_address"]["firstname"] = urldecode($formData['FirstName']);
        $order["entity"]["billing_address"]["lastname"] = urldecode($formData['LastName']);
        $order["entity"]["billing_address"]["postcode"] = urldecode($formData['MainAddress.Zip']);
        $order["entity"]["billing_address"]["region"] = urldecode($formData['region']);
        $order["entity"]["billing_address"]["region_code"] = urldecode($formData['region']);
        $order["entity"]["billing_address"]["region_id"] = 19;
        $order["entity"]["billing_address"]["street"] = array(urldecode($formData["MainAddress.Address1"]), urldecode($formData["MainAddress.Address2"]));
        $order["entity"]["billing_address"]["telephone"] = urldecode($formData["PrimaryPhone"]);
        $order["entity"]["payment"]["method"] = "checkmo";
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["address_type"] = "shipping";
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["city"] = urldecode($formData["MailingAddress.City"]);
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["company"] = "";
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["country_id"] = urldecode($formData["MailingAddress.Country"]);
//        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["customer_address_id"] = 2;
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["email"] = urldecode($formData["Email"]);
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["firstname"] = urldecode($formData["FirstName"]);
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["lastname"] = urldecode($formData["LastName"]);
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["postcode"] = urldecode($formData["MainAddress.Zip"]);
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["region"] = urldecode($formData["region"]);
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["region_code"] = urldecode($formData["region"]);
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["region_id"] = 19;
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["street"] = array(urldecode($formData["MailingAddress.Address1"]), urldecode($formData["MailingAddress.Address2"]));
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["telephone"] = urldecode($formData["PrimaryPhone"]);
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["method"] = "flatrate_flatrate";

        $result = $this->createMageOrder($order);
        echo json_encode($result); exit; 
//        $ApiResponse = $this->_helper->placeOrderByAPI($order);
//        print_r($ApiResponse);
        exit;
        exit;
    }
  • your code has exit after "Customer does not exist. please create here..." is this by mistake? and in Magento 2.2.3's table sales_order has not column event_id. Is this your custom column? – Knight017 Mar 26 at 13:06
  • In my case customer is already exists so its not by mistake. Its perfectly handled. Yes event_id is my own column I have created custom attributes for orders according to magento and magento created this column in sales_order table – Abdulrehman Sheikh Mar 26 at 13:11
  • Okay, then this blog may help you atwix.com/magento-2/… – Knight017 Mar 26 at 13:55
  • There shouldn't be an exit anywhere in your code. It should be handled differently. – Shawn Abramson Mar 27 at 4:30
  • Exit is not an issue. I just want to insert an order – Abdulrehman Sheikh Mar 27 at 6:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.