function CreateAmzFBAOrder($order_id)
{
    AmzFBA_Woo_Log("DEBUG", "Order", __FUNCTION__, "Entering");
    $MissingConfig = CheckForMissingConfig();
    $level = "Good";
    $category = "Order";
    if ($MissingConfig == true) {
        $returnmessage = 'Failed sending order to Amazon FBA. Required configuration missing in settings.';
        $order->add_order_note($returnmessage, 0);
        //Add to Log
        $level = "Bad";
        $category = "Order";
        $title = "Failed - Order ID:" . $order_id;
        AmzFBA_Woo_Log($level, $category, $title, $returnmessage);
        return $returnmessage;
    }
    $FBAOrderID = get_post_meta($order_id, "FBA_OrderId", TRUE);
    if (!empty($FBAOrderID) && $FBAOrderID != "") {
        $returnmessage = 'Create FBA Order: FBA Order id is already there: ' . $FBAOrderID;
        $order->add_order_note($returnmessage, 0);
        //Add to Log
        $level = "Bad";
        $title = "Create FBA Order - Order ID:" . $order_id;
        AmzFBA_Woo_Log($level, $category, $title, $returnmessage);
        return $returnmessage;
    }
    $UserSettings = get_option('woocommerce_amazonfba_settings');
    $ChosenMarketplace = $UserSettings['AmzFBA_Marketplace'];
    $MWSEndpointURL = GetMWSEndpointURL('FulfillmentOutboundShipment', $ChosenMarketplace);
    $config = array('ServiceURL' => $MWSEndpointURL, 'ProxyHost' => null, 'ProxyPort' => -1, 'MaxErrorRetry' => 3);
    $service = new FBAOutboundServiceMWS_Client(ACCESS_KEY_ID, SECRET_ACCESS_KEY, $config, APPLICATION_NAME, APPLICATION_VERSION);
    $order = new WC_Order($order_id);
    $dateObj = new DateTime($order->order_date, new DateTimeZone('UTC'));
    $order_date_converted = $dateObj->format(DateTime::ISO8601);
    $datetimeasINT = date("YmdHis");
    $uniqueid_foramazon = "KINIVO-" . $datetimeasINT . "-" . $order_id;
    AmzFBA_Woo_Log($level, $category, $title, "FBA order id - " . $uniqueid_foramazon);
    //set Variables
    $shipping_name = $order->shipping_first_name . ' ' . $order->shipping_last_name;
    $shipping_address_line_1 = $order->shipping_address_1;
    $shipping_address_line_2 = $order->shipping_address_2;
    $shipping_city = $order->shipping_city;
    $shipping_state = $order->shipping_state;
    $shipping_postcode = $order->shipping_postcode;
    $shipping_country = $order->shipping_country;
    $shipping_speed = GetAmazonShippingSpeed($order);
    $UserSettings = get_option('woocommerce_amazonfba_settings');
    //Gets all settings from this App
    $FulfillmentPolicy = $UserSettings['AmzFBA_FulfillmentPolicy'];
    $OrderComment = $UserSettings['AmzFBA_OrderComment'];
    $EmailNotificationAddress = $UserSettings['AmzFBA_EmailNotifyAddress'];
    ////////////////////////////// //Standard Stuff
    $request = new FBAOutboundServiceMWS_Model_CreateFulfillmentOrderRequest();
    $request->setSellerId(SELLER_ID);
    $request->setSellerFulfillmentOrderId($uniqueid_foramazon);
    $request->setDisplayableOrderId($order_id);
    $request->setDisplayableOrderDateTime($order_date_converted);
    $request->setDisplayableOrderComment($OrderComment);
    //     $request->setNotificationEmailList($EmailNotificationAddress);
    $request->setShippingSpeedCategory($shipping_speed);
    //Standard, Expedited, Priority
    $request->setFulfillmentPolicy($FulfillmentPolicy);
    //FillOrKill, FillAll, FillAllAvailable
    ////////////////////////////// //Address
    $requestaddress = new FBAOutboundServiceMWS_Model_Address();
    $requestaddress->setName($shipping_name);
    $requestaddress->setLine1($shipping_address_line_1);
    $requestaddress->setLine2($shipping_address_line_2);
    $requestaddress->setLine3('');
    $requestaddress->setCity($shipping_city);
    $requestaddress->setDistrictOrCounty($shipping_country);
    if ($shipping_state != '') {
        $requestaddress->setStateOrProvinceCode($shipping_state);
    }
    $requestaddress->setCountryCode($shipping_country);
    $requestaddress->setPostalCode($shipping_postcode);
    $requestaddress->setPhoneNumber('');
    $request->setDestinationAddress($requestaddress);
    ////////////////////////////// //Order Item List
    $requestlist = new FBAOutboundServiceMWS_Model_CreateFulfillmentOrderItemList();
    $request->setItems($requestlist);
    ////////////////////////////// //Order Items
    $items = $order->get_items();
    $itemnumber = 0;
    $itemnumber_unful = 0;
    $UnfulfillableItems = '';
    $success = 0;
    $unsuccess = 0;
    foreach ($items as $item) {
        //Get the SKU of the Item
        $Quantity = $item['qty'];
        if ($item['variation_id'] == '' || $item['variation_id'] == NULL || $item['variation_id'] == '0') {
            $IDForSKU = $item['product_id'];
        } else {
            $IDForSKU = $item['variation_id'];
        }
        $GetSKU = new WC_Product($IDForSKU);
        $SKU = $GetSKU->get_sku();
        //Check if available for Amazon Fulfilment
        $fulfillable = false;
        if (AmzFBA_is_sku_fulfillable($SKU) == NULL || AmzFBA_is_sku_fulfillable($SKU) == '') {
            $UnfulfillableItems[$itemnumber_unful]['SKU'] = $SKU;
            $UnfulfillableItems[$itemnumber_unful]['Quantity'] = $Quantity;
            $unsuccess = $unsuccess + $Quantity;
            $itemnumber_unful++;
            $note = "SKU : IDForSKU = " . $IDForSKU . " SKU = " . $SKU . " is not fulfillable by Amazon. Please check product details";
            $order->add_order_note($note, 0);
        } else {
            $ItemArray[$itemnumber] = new FBAOutboundServiceMWS_Model_CreateFulfillmentOrderItem();
            $ItemArray[$itemnumber]->setSellerSKU($SKU);
            $ItemArray[$itemnumber]->setSellerFulfillmentOrderItemId($itemnumber);
            $ItemArray[$itemnumber]->setQuantity($Quantity);
            $success = $success + $Quantity;
            $itemnumber++;
            $fulfillable = true;
        }
        $level = "Neutral";
        $category = "Order";
        $title = "Item details for Order ID:" . $order_id;
        $info = "SKU details: IDForSKU = " . $IDForSKU . " SKU = " . $SKU . " Fulfillable = " . $fulfillable;
        AmzFBA_Woo_Log($level, $category, $title, $info);
    }
    $percentfulfilled = round($success / ($success + $unsuccess) * 100, 2);
    //Load Array of Items into Order
    if ($percentfulfilled != 0) {
        $requestlist->setmember($ItemArray);
    }
    //Check if we now need to fulfil them.
    //If 0% fulfillled
    if ($percentfulfilled == '0') {
        $returnmessage = "Order not submitted. No SKUs in WooCommerce available on Amazon FBA";
        $note = "No SKUs in this order available for Amazon Fulfillment.";
        $order->add_order_note($note, 0);
        $order->update_status('on-hold');
        update_post_meta($order_id, 'PercentFulfilledByAmazon', $percentfulfilled);
        //Add to Log
        $level = "Neutral";
        $category = "Order";
        $title = "Unfulfillable - Order ID:" . $order_id;
        $info = "No SKUs in this order available for Amazon Fulfillment.";
        AmzFBA_Woo_Log($level, $category, $title, $info);
    } elseif ($percentfulfilled < '100' && $percentfulfilled > '0') {
        $returnmessage = "Order not submitted. Only " . $percentfulfilled . "% of the Order is available on Amazon. Did not send anything to Amazon.";
        $note = $returnmessage;
        $order->add_order_note($note, 0);
        $order->update_status('on-hold');
        //Add to Log
        $level = "Neutral";
        $category = "Order";
        $title = "Unfulfillable - Order ID:" . $order_id;
        $info = "Only some sku's were available for Amazon Fulfillment. Did not send anything to Amazon.";
        AmzFBA_Woo_Log($level, $category, $title, $info);
        /*
        $returnmessage = "Order submitted. Sent " . $percentfulfilled . "% of the Order to Amazon. For unfulfillable items, see order notes.";
        $note          = $percentfulfilled . "% of Order sent to Amazon for Fulfillment : FBA_OrderId " . $uniqueid_foramazon;
        $order->add_order_note($note, 0);
        foreach ($UnfulfillableItems as $UnfilItem) {
            $theitems .= $UnfilItem['Quantity'] . " X " . $UnfilItem['SKU'] . ".  ";
        }
        $note = "Unfulfillable Items : " . $theitems;
        $order->add_order_note($note, 0);
        update_post_meta($order_id, 'PercentFulfilledByAmazon', $percentfulfilled);
        //Add meta data of Unique ID sent to Amazon
        update_post_meta($order_id, 'FBA_OrderId', $uniqueid_foramazon);
        //Send Order to Amazon
        invokeCreateFulfillmentOrder($service, $request, $order_id, $percentfulfilled);
        */
    } elseif ($percentfulfilled == '100') {
        $returnmessage = "Order submitted. Sent" . $percentfulfilled . "% of Order to Amazon.";
        $note = "100% of Order sent to Amazon for Fulfillment: FBA_OrderId " . $uniqueid_foramazon;
        $order->add_order_note($note, 0);
        update_post_meta($order_id, 'PercentFulfilledByAmazon', $percentfulfilled);
        //Add meta data of Unique ID sent to Amazon
        update_post_meta($order_id, 'FBA_OrderId', $uniqueid_foramazon);
        //Send Order to amazon
        if (!invokeCreateFulfillmentOrder($service, $request, $order_id, $percentfulfilled)) {
            // It seems creating FBA order failed... Putting the order on hold
            $note = "FBA order failed. Check logs and sellercentral for order id - " . $uniqueid_foramazon;
            $returnmessage = $note;
            $order->add_order_note($note, 0);
            $order->update_status('on-hold');
            AmzFBA_Woo_Log("Bad", $category, "FBA order creation", "Failed - not sure what happened. Check sellercentral as well");
        }
    }
    AmzFBA_Woo_Log("DEBUG", "Order", __FUNCTION__, "Exiting");
    return $returnmessage;
}
function CreateAmzFBAOrder($order_id)
{
    if (CheckForMissingConfig() != '') {
        $returnmessage = 'Failed sending order to Amazon FBA. Required configuration missing in settings.';
        $note = "Failed sending order to Amazon FBA. Required configuration missing in settings.";
        $order->add_order_note($note, 0);
        //Add to Log
        $level = "Bad";
        $category = "Order";
        $title = "Failed - Order ID:" . $order_id;
        $info = "Failed sending order to Amazon FBA. Required configuration missing in settings..";
        AmzFBA_Woo_Log($level, $category, $title, $info);
        return $returnmessage;
    }
    $UserSettings = get_option('woocommerce_amazonfba_settings');
    $ChosenMarketplace = $UserSettings['AmzFBA_Marketplace'];
    $MWSEndpointURL = GetMWSEndpointURL('FulfillmentOutboundShipment', $ChosenMarketplace);
    $config = array('ServiceURL' => $MWSEndpointURL, 'ProxyHost' => null, 'ProxyPort' => -1, 'MaxErrorRetry' => 3);
    $service = new FBAOutboundServiceMWS_Client(ACCESS_KEY_ID, SECRET_ACCESS_KEY, $config, APPLICATION_NAME, APPLICATION_VERSION);
    $order = new WC_Order($order_id);
    $dateObj = new DateTime($order->order_date, new DateTimeZone('UTC'));
    $order_date_converted = $dateObj->format(DateTime::ISO8601);
    $datetimeasINT = date("YmdHis");
    $uniqueid_foramazon = $datetimeasINT . $order_id;
    //set Variables
    $shipping_name = $order->shipping_first_name . ' ' . $order->shipping_last_name;
    $shipping_address_line_1 = $order->shipping_address_1;
    $shipping_address_line_2 = $order->shipping_address_2;
    $shipping_city = $order->shipping_city;
    $shipping_state = $order->shipping_state;
    $shipping_postcode = $order->shipping_postcode;
    $shipping_country = $order->shipping_country;
    $FulfillmentPolicy = get_option('AmzFBA_FulfillmentPolicy');
    $OrderComment = get_option('AmzFBA_OrderComment');
    $EmailNotificationAddress = get_option('AmzFBA_EmailNotifyAddress');
    ////////////////////////////// //Standard Stuff
    $request = new FBAOutboundServiceMWS_Model_CreateFulfillmentOrderRequest();
    $request->setSellerId(SELLER_ID);
    $request->setSellerFulfillmentOrderId($uniqueid_foramazon);
    $request->setDisplayableOrderId($order_id);
    $request->setDisplayableOrderDateTime($order_date_converted);
    $request->setDisplayableOrderComment($OrderComment);
    //     $request->setNotificationEmailList('');
    $request->setShippingSpeedCategory('Standard');
    //Standard, Expedited, Priority
    $request->setFulfillmentPolicy($FulfillmentPolicy);
    //FillOrKill, FillAll, FillAllAvailable
    ////////////////////////////// //Address
    $requestaddress = new FBAOutboundServiceMWS_Model_Address();
    $requestaddress->setName($shipping_name);
    $requestaddress->setLine1($shipping_address_line_1);
    $requestaddress->setLine2($shipping_address_line_2);
    $requestaddress->setLine3('');
    $requestaddress->setCity($shipping_city);
    $requestaddress->setDistrictOrCounty($shipping_country);
    if ($shipping_state != '') {
        $requestaddress->setStateOrProvinceCode($shipping_state);
    }
    $requestaddress->setCountryCode($shipping_country);
    $requestaddress->setPostalCode($shipping_postcode);
    $requestaddress->setPhoneNumber('');
    $request->setDestinationAddress($requestaddress);
    ////////////////////////////// //Order Item List
    $requestlist = new FBAOutboundServiceMWS_Model_CreateFulfillmentOrderItemList();
    $request->setItems($requestlist);
    ////////////////////////////// //Order Items
    $items = $order->get_items();
    $itemnumber = 0;
    $UnfulfillableItems = '';
    $success = 0;
    $unsuccess = 0;
    foreach ($items as $item) {
        //Get the SKU of the Item
        $Quantity = $item['qty'];
        if ($item['variation_id'] == '') {
            $IDForSKU = $item['product_id'];
        } else {
            $IDForSKU = $item['variation_id'];
        }
        $GetSKU = new WC_Product($IDForSKU);
        $SKU = $GetSKU->get_sku();
        //Check if available for Amazon Fulfilment
        if (AmzFBA_is_sku_fulfillable($SKU) == NULL) {
            $UnfulfillableItems[$itemnumber]['SKU'] = $SKU;
            $UnfulfillableItems[$itemnumber]['Quantity'] = $Quantity;
            $unsuccess = $unsuccess + $Quantity;
        } else {
            $ItemArray[$itemnumber] = new FBAOutboundServiceMWS_Model_CreateFulfillmentOrderItem();
            $ItemArray[$itemnumber]->setSellerSKU($SKU);
            $ItemArray[$itemnumber]->setSellerFulfillmentOrderItemId($itemnumber);
            $ItemArray[$itemnumber]->setQuantity($Quantity);
            $success = $success + $Quantity;
            $itemnumber++;
        }
    }
    $percentfulfilled = round($success / ($success + $unsuccess) * 100, 2);
    //Load Array of Items into Order
    if ($percentfulfilled != 0) {
        $requestlist->setmember($ItemArray);
    }
    //Check if we now need to fulfil them.
    //If 0% fulfillled
    if ($percentfulfilled == '0') {
        $returnmessage = "Order not submitted. No SKUs in WooCommerce available on Amazon FBA";
        $note = "No SKUs in this order available for Amazon Fulfillment.";
        $order->add_order_note($note, 0);
        update_post_meta($order_id, 'PercentFulfilledByAmazon', $percentfulfilled);
        //Add to Log
        $level = "Neutral";
        $category = "Order";
        $title = "Unfulfillable - Order ID:" . $order_id;
        $info = "No SKUs in this order available for Amazon Fulfillment.";
        AmzFBA_Woo_Log($level, $category, $title, $info);
    } elseif ($percentfulfilled < '100' && $percentfulfilled > '0') {
        $returnmessage = "Order submitted. Sent " . $percentfulfilled . "% of the Order to Amazon. For unfulfillable items, see order notes.";
        $note = $percentfulfilled . "% of Order sent to Amazon for Fulfillment";
        $order->add_order_note($note, 0);
        foreach ($UnfulfillableItems as $UnfilItem) {
            $theitems .= $UnfilItem['Quantity'] . " X " . $UnfilItem['SKU'] . ".  ";
        }
        $note = "Unfulfillable Items : " . $theitems;
        $order->add_order_note($note, 0);
        update_post_meta($order_id, 'PercentFulfilledByAmazon', $percentfulfilled);
        //Add meta data of Unique ID sent to Amazon
        update_post_meta($order_id, 'FBA_OrderId', $uniqueid_foramazon);
        //Send Order to Amazon
        invokeCreateFulfillmentOrder($service, $request, $order_id, $percentfulfilled);
    } elseif ($percentfulfilled == '100') {
        $returnmessage = "Order submitted. Sent" . $percentfulfilled . "% of Order to Amazon.";
        $note = "100% of Order sent to Amazon for Fulfillment";
        $order->add_order_note($note, 0);
        update_post_meta($order_id, 'PercentFulfilledByAmazon', $percentfulfilled);
        //Add meta data of Unique ID sent to Amazon
        update_post_meta($order_id, 'FBA_OrderId', $uniqueid_foramazon);
        //Send Order to amazon
        invokeCreateFulfillmentOrder($service, $request, $order_id, $percentfulfilled);
    }
    return $returnmessage;
}