/**
  * Returns custom order attributes.
  *
  * @return array
  */
 public function toOptionArray()
 {
     $fields = $this->dataHelper->getOrderTableDescription();
     $customFields[] = ['label' => __('---- Default Option ----'), 'value' => '0'];
     foreach ($fields as $field) {
         $customFields[] = ['value' => $field['COLUMN_NAME'], 'label' => $field['COLUMN_NAME']];
     }
     return $customFields;
 }
 /**
  * Set the order data information.
  *
  * @param $orderData
  *
  * @return $this
  */
 public function setOrderData($orderData)
 {
     $this->id = $orderData->getIncrementId();
     $this->quoteId = $orderData->getQuoteId();
     $this->email = $orderData->getCustomerEmail();
     $this->storeName = $orderData->getStoreName();
     $this->purchaseDate = $this->localeDate->date($orderData->getCreatedAt())->format(\Zend_Date::ISO_8601);
     $this->deliveryMethod = $orderData->getShippingDescription();
     $this->deliveryTotal = (double) number_format($orderData->getShippingAmount(), 2, '.', '');
     $this->currency = $orderData->getStoreCurrencyCode();
     if ($payment = $orderData->getPayment()) {
         $this->payment = $payment->getMethodInstance()->getTitle();
     }
     $this->couponCode = $orderData->getCouponCode();
     /*
      * custom order attributes
      */
     $website = $this->_storeManager->getStore($orderData->getStore())->getWebsite();
     $customAttributes = $this->helper->getConfigSelectedCustomOrderAttributes($website);
     if ($customAttributes) {
         $fields = $this->helper->getOrderTableDescription();
         $this->custom = [];
         foreach ($customAttributes as $customAttribute) {
             if (isset($fields[$customAttribute])) {
                 $field = $fields[$customAttribute];
                 $value = $this->_getCustomAttributeValue($field, $orderData);
                 if ($value) {
                     $this->_assignCustom($field, $value);
                 }
             }
         }
     }
     /*
      * Billing address.
      */
     if ($orderData->getBillingAddress()) {
         $billingData = $orderData->getBillingAddress()->getData();
         $this->billingAddress = ['billing_address_1' => $this->_getStreet($billingData['street'], 1), 'billing_address_2' => $this->_getStreet($billingData['street'], 2), 'billing_city' => $billingData['city'], 'billing_region' => $billingData['region'], 'billing_country' => $billingData['country_id'], 'billing_postcode' => $billingData['postcode']];
     }
     /*
      * Shipping address.
      */
     if ($orderData->getShippingAddress()) {
         $shippingData = $orderData->getShippingAddress()->getData();
         $this->deliveryAddress = ['delivery_address_1' => $this->_getStreet($shippingData['street'], 1), 'delivery_address_2' => $this->_getStreet($shippingData['street'], 2), 'delivery_city' => $shippingData['city'], 'delivery_region' => $shippingData['region'], 'delivery_country' => $shippingData['country_id'], 'delivery_postcode' => $shippingData['postcode']];
     }
     $syncCustomOption = $this->helper->getWebsiteConfig(\Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_ORDER_PRODUCT_CUSTOM_OPTIONS, $website);
     /*
      * Order items.
      */
     foreach ($orderData->getAllItems() as $productItem) {
         //product custom options
         $customOptions = [];
         if ($syncCustomOption) {
             $customOptions = $this->_getOrderItemOptions($productItem);
         }
         $productModel = $productItem->getProduct();
         if ($productModel) {
             // category names
             $categoryCollection = $productModel->getCategoryCollection()->addAttributeToSelect('name');
             $productCat = [];
             foreach ($categoryCollection as $cat) {
                 $categories = [];
                 $categories[] = $cat->getName();
                 $productCat[]['Name'] = substr(implode(', ', $categories), 0, 244);
             }
             $attributes = [];
             //selected attributes from config
             $configAttributes = $this->helper->getWebsiteConfig(\Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_ORDER_PRODUCT_ATTRIBUTES, $orderData->getStore()->getWebsite());
             if ($configAttributes) {
                 $configAttributes = explode(',', $configAttributes);
                 //attributes from attribute set
                 $attributesFromAttributeSet = $this->_getAttributesArray($productModel->getAttributeSetId());
                 foreach ($configAttributes as $attributeCode) {
                     //if config attribute is in attribute set
                     if (in_array($attributeCode, $attributesFromAttributeSet)) {
                         //attribute input type
                         $inputType = $productModel->getResource()->getAttribute($attributeCode)->getFrontend()->getInputType();
                         //fetch attribute value from product depending on input type
                         switch ($inputType) {
                             case 'multiselect':
                             case 'select':
                             case 'dropdown':
                                 $value = $productModel->getAttributeText($attributeCode);
                                 break;
                             case 'date':
                                 $value = $this->localeDate->date($productModel->getData($attributeCode))->format(\Zend_Date::ISO_8601);
                                 break;
                             default:
                                 $value = $productModel->getData($attributeCode);
                                 break;
                         }
                         if ($value && !is_array($value)) {
                             // check limit on text and assign value to array
                             $attributes[][$attributeCode] = $this->_limitLength($value);
                         } elseif (is_array($value)) {
                             $value = implode($value, ', ');
                             $attributes[][$attributeCode] = $this->_limitLength($value);
                         }
                     }
                 }
             }
             $attributeSetName = $this->getAttributeSetName($productModel);
             $productData = ['name' => $productItem->getName(), 'sku' => $productItem->getSku(), 'qty' => (int) number_format($productItem->getData('qty_ordered'), 2), 'price' => (double) number_format($productItem->getPrice(), 2, '.', ''), 'attribute-set' => $attributeSetName, 'categories' => $productCat, 'attributes' => $attributes, 'custom-options' => $customOptions];
             if (!$customOptions) {
                 unset($productData['custom-options']);
             }
             $this->products[] = $productData;
         } else {
             // when no product information is available limit to this data
             $productData = ['name' => $productItem->getName(), 'sku' => $productItem->getSku(), 'qty' => (int) number_format($productItem->getData('qty_ordered'), 2), 'price' => (double) number_format($productItem->getPrice(), 2, '.', ''), 'attribute-set' => '', 'categories' => [], 'attributes' => [], 'custom-options' => $customOptions];
             if (!$customOptions) {
                 unset($productData['custom-options']);
             }
             $this->products[] = $productData;
         }
     }
     $this->orderSubtotal = (double) number_format($orderData->getData('subtotal'), 2, '.', '');
     $this->discountAmount = (double) number_format($orderData->getData('discount_amount'), 2, '.', '');
     $orderTotal = abs($orderData->getData('grand_total') - $orderData->getTotalRefunded());
     $this->orderTotal = (double) number_format($orderTotal, 2, '.', '');
     $this->orderStatus = $orderData->getStatus();
     unset($this->_storeManager);
     return $this;
 }