diff --git a/src/app/code/Fera/Ai/Block/Footer.php b/src/app/code/Fera/Ai/Block/Footer.php index 7b081b5..1e8ea83 100644 --- a/src/app/code/Fera/Ai/Block/Footer.php +++ b/src/app/code/Fera/Ai/Block/Footer.php @@ -61,6 +61,11 @@ public function getPublicKey() return $this->helper->getPublicKey(); } + public function getAppUrl() + { + return $this->helper->getAppUrl(); + } + public function getApiUrl() { return $this->helper->getApiUrl(); diff --git a/src/app/code/Fera/Ai/Block/Footer/Checkout/Success.php b/src/app/code/Fera/Ai/Block/Footer/Checkout/Success.php deleted file mode 100644 index 417aca2..0000000 --- a/src/app/code/Fera/Ai/Block/Footer/Checkout/Success.php +++ /dev/null @@ -1,125 +0,0 @@ -customerSession = $customerSession; - $this->directoryHelper = $directoryHelper; - $this->helper = $helper; - parent::__construct($context, $checkoutSession, $orderConfig, $httpContext, $data); - } - - /** - * @return bool|mixed - */ - public function isEnabled() - { - return $this->helper->isEnabled(); - } - - /** - * Get last order, JSONify it and return it. - * @return String - */ - public function getOrderJson() - { - $order = $this->_checkoutSession->getLastRealOrder(); - - $customer = null; - if (!empty($this->customerSession->getCustomerId())) { - $customer = $this->customerSession->getCustomer(); - $address = $customer->getDefaultShippingAddress(); - - if (!empty($address)) { - $address = $address->getData(); - } - - $customer = [ - 'id' => $this->customerSession->getCustomerId(), - 'first_name' => $this->customerSession->getCustomer()->getFirstname(), - 'email' => $this->customerSession->getCustomer()->getEmail(), - 'address' => $address - ]; - } else { - $customer = [ - 'id' => $order->getCustomerId(), - 'first_name' => $order->getCustomerFirstname(), - 'email' => $order->getCustomerEmail() - ]; - } - - $currencyCode = $this->_storeManager->getStore()->getCurrentCurrencyCode(); - $total = $order->getGrandTotal(); - $totalUsd = $this->directoryHelper->currencyConvert($total, $currencyCode, 'USD'); - - $orderData = [ - 'id' => $order->getId(), - 'number' => $order->getIncrementId(), - 'total' => $total, - 'total_usd' => $totalUsd, - 'created_at' => $this->helper->formatDate($order->getCreatedAt()), - 'modified_at' => $this->helper->formatDate($order->getUpdatedAt()), - 'line_items' => $this->helper->serializeQuoteItems($order->getAllItems()), - 'customer' => $customer, - 'source_name' => 'web' - ]; - - return $this->helper->jsonEncode($orderData); - } - -} diff --git a/src/app/code/Fera/Ai/Helper/Data.php b/src/app/code/Fera/Ai/Helper/Data.php index e5b9151..a74a443 100644 --- a/src/app/code/Fera/Ai/Helper/Data.php +++ b/src/app/code/Fera/Ai/Helper/Data.php @@ -133,9 +133,22 @@ public function isConfigured() { $publicKey = $this->getPublicKey(); $secretKey = $this->getSecretKey(); + $appUrl = $this->getAppUrl(); $apiUrl = $this->getApiUrl(); $jsUrl = $this->getJsUrl(); - return !empty($publicKey) && !empty($secretKey) && !empty($apiUrl) && !empty($jsUrl); + return !empty($publicKey) && !empty($secretKey) && !empty($appUrl) && !empty($apiUrl) && !empty($jsUrl); + } + + /** + * The URL path to the APP (https). For example: https://app.fera.ai + * @return string + */ + public function getAppUrl() + { + return $this->scopeConfig->getValue( + 'fera_ai/fera_ai_group/app_url', + ScopeInterface::SCOPE_STORE + ); } /** diff --git a/src/app/code/Fera/Ai/Observer/Backend/OrderFulfilledStatusUpdate.php b/src/app/code/Fera/Ai/Observer/Backend/OrderFulfilledStatusUpdate.php new file mode 100644 index 0000000..b2c022b --- /dev/null +++ b/src/app/code/Fera/Ai/Observer/Backend/OrderFulfilledStatusUpdate.php @@ -0,0 +1,63 @@ +helper = $helper; + $this->_curl = $curl; + } + + /** + * Send put request to update order status + */ + public function updateOrderStatus($data) { + $url = $this->helper->getApiUrl()."v3/private/orders/".$data['external_id']."/fulfill"; + $this->_curl->addHeader("Content-Type", "application/json"); + $this->_curl->addHeader("SECRET-KEY", $this->helper->getSecretKey()); + $this->_curl->setOption(CURLOPT_CUSTOMREQUEST, "PUT"); + $this->_curl->post($url, $this->helper->jsonEncode($data)); + $response = $this->_curl->getBody(); + // $this->helper->log("Order status is updated for order". $response); + //echo $response; + } + + /** + * get orders data and and send request + * + * @param Observer $observer + */ + public function execute(\Magento\Framework\Event\Observer $observer) { + if (!$this->helper->isEnabled()) { return; } + + $shipment = $observer->getEvent()->getShipment(); + $orderId = $shipment->getOrder()->getId(); + $shipmentData = [ + 'fulfilled_at' => $this->helper->formatDate($shipment->getCreatedAt()), + 'external_id' => $orderId, + ]; + $this->updateOrderStatus($shipmentData); + + // $this->helper->log("Order id is: ". $orderId); + // $this->helper->log("Order created at is: ". $shipment->getCreatedAt()); + return; + } +} diff --git a/src/app/code/Fera/Ai/Observer/Frontend/OrderPushEvent.php b/src/app/code/Fera/Ai/Observer/Frontend/OrderPushEvent.php new file mode 100644 index 0000000..323150d --- /dev/null +++ b/src/app/code/Fera/Ai/Observer/Frontend/OrderPushEvent.php @@ -0,0 +1,159 @@ +helper = $helper; + $this->_curl = $curl; + $this->order = $order; + $this->directoryHelper = $directoryHelper; + $this->customerSession = $customerSession; + $this->_checkoutSession = $checkoutSession; + $this->_storeManager = $storeManager; + $this->_currency = $currency; + } + + /** + * Send post request to push orders + */ + public function pushOrders($data) { + $url = $this->helper->getApiUrl()."v3/private/orders.json"; + $this->_curl->addHeader("Content-Type", "application/json"); + $this->_curl->addHeader("SECRET-KEY", $this->helper->getSecretKey()); + $this->_curl->post($url, $this->helper->jsonEncode($data)); + + $response = $this->_curl->getBody(); + } + + /** + * get orders data and and send request + * + * @param Observer $observer + */ + public function execute(Observer $observer) { + if (!$this->helper->isEnabled()) { return; } + + $orderId = $this->_checkoutSession->getLastOrderId(); + + $order = $this->order->load($orderId); + $customer = $this->getCustomerData($order); + + $currencyCode = $this->_storeManager->getStore()->getCurrentCurrencyCode(); + $total = $order->getGrandTotal(); + $totalUsd = $this->directoryHelper->currencyConvert($total, $currencyCode, 'USD'); + + $orderData = [ + 'external_id' => $orderId, + 'number' => $order->getIncrementId(), + 'total' => $total, + 'total_usd' => $totalUsd, + 'external_created_at' => $this->helper->formatDate($order->getCreatedAt()), + 'external_updated_at' => $this->helper->formatDate($order->getUpdatedAt()), + 'line_items' => $this->helper->serializeQuoteItems($order->getAllItems()), + 'customer' => $customer, + 'external_customer_id' => $order->getCustomerId(), + 'tags' => [], + 'source_name' => 'web' + ]; + + if (!empty($order->getShippingAddress())) { $orderData['shipping_address'] = $this->getShippingData($order); } + if (!empty($order->getBillingAddress())) { $orderData['shipping_address'] = $this->getBillingData($order); } + + $this->pushOrders($orderData); + + return; + } + + /** + * get customer data + */ + public function getCustomerData($order) { + $customerId = $order->getCustomerId(); + $customerName = $order->getCustomerFirstname() . ' ' . $order->getCustomerLastname(); + + $customer = [ + 'external_id' => $customerId, + 'name' => $customerName, + 'email' => $order->getCustomerEmail(), + ]; + + return $customer; + } + + /** + * get shhipping details + */ + public function getShippingData($order) { + $shippingAddress = $order->getShippingAddress(); + $shippingData = [ + 'name' => $shippingAddress->getData('firstname') . ' ' . $shippingAddress->getData('lastname'), + 'address1' => $shippingAddress->getData('street'), + 'city_name' => $shippingAddress->getData('city'), + 'region_name' => $shippingAddress->getData('region'), + 'zip_code' => $shippingAddress->getData('postcode'), + ]; + + return $shippingData; + } + + /** + * get billing data + */ + + public function getBillingData($order) { + $billingAddress = $order->getBillingAddress(); + $billingData = [ + 'name' => $billingAddress->getData('firstname') . ' ' . $billingAddress->getData('lastname'), + 'address1' => $billingAddress->getData('street'), + 'city_name' => $billingAddress->getData('city'), + 'region_name' => $billingAddress->getData('region'), + 'zip_code' => $billingAddress->getData('postcode'), + ]; + return $billingData; + } +} diff --git a/src/app/code/Fera/Ai/Observer/Frontend/ProductPushEvent.php b/src/app/code/Fera/Ai/Observer/Frontend/ProductPushEvent.php new file mode 100644 index 0000000..dc2a712 --- /dev/null +++ b/src/app/code/Fera/Ai/Observer/Frontend/ProductPushEvent.php @@ -0,0 +1,121 @@ +stockState = $stockState; + $this->helper = $helper; + $this->_curl = $curl; + } + + /** + * Send post request to push orders + */ + public function pushProducts($data) { + $url = $this->helper->getApiUrl(). "v3/private/products.json"; + $this->_curl->addHeader("Content-Type", "application/json"); + $this->_curl->addHeader("SECRET-KEY", $this->helper->getSecretKey()); + $this->_curl->post($url, $this->helper->jsonEncode($data)); + + $response = $this->_curl->getBody(); + } + + /** + * Use curl to push products to our server. + * + * @param Observer $observer + */ + public function execute(Observer $observer) + { + if (!$this->helper->isEnabled()) { return; } + + $p = $observer->getProduct(); + $thumb = $this->helper->getProductThumbnailUrl($p); + $productData = [ + "id" => $p->getId(), // String + "external_id" => $p->getId(), // String + "name" => $p->getName(), // String + "price" => $p->getFinalPrice(), // Float + "status" => $p->getStatus() == 1 ? 'published' : 'draft', // (Optional) String + "created_at" => $this->helper->formatDate($p->getCreatedAt()), // (Optional) String (ISO 8601 format DateTime) + "modified_at" => $this->helper->formatDate($p->getUpdatedAt()), // (Optional) String (ISO 8601 format DateTime) + "stock" => $this->stockState->getStockQty($p->getId(), $p->getStore()->getWebsiteId()), // (Optional) Integer, If null assumed to be infinite. + "in_stock" => $p->isInStock(), // (Optional) Boolean + "url" => $p->getProductUrl(), // String + "thumbnail_url" => $thumb, // String + "needs_shipping" => $p->getTypeId() != 'virtual', // (Optional) Boolean + "hidden" => $p->getVisibility() == '1', // (Optional) Boolean + 'tags' => [], // M2 not included tags by default + "variants" => [], // (Optional) Array: Variants that are applicable to this product. + "platform_data" => [ // (Optional) Hash/Object of attributes to store about the product specific to the integration platform (can be used in future filters) + "sku" => $p->getSku(), + "type" => $p->getTypeId(), + "regular_price" => $p->getPrice() + ] + ]; + + if ($p->getTypeId() == 'configurable') { + $cfgAttr = $p->getTypeInstance()->getConfigurableAttributesAsArray($p); + + foreach ($p->getTypeInstance()->getUsedProducts($p) as $subProduct) { + + $variant = [ + "id" => $subProduct->getId(), + "name" => $subProduct->getName(), // String + "status" => $subProduct->getStatus() == 1 ? 'published' : 'draft', // (Optional) String + "created_at" => $this->helper->formatDate($subProduct->getCreatedAt()), // (Optional) String (ISO 8601 format DateTime) + "modified_at" => $this->helper->formatDate($subProduct->getUpdatedAt()), // (Optional) String (ISO 8601 format DateTime) + "stock" => $this->stockState->getStockQty($subProduct->getId(), $subProduct->getStore()->getWebsiteId()), // (Optional) Integer, If null assumed to be infinite. + "in_stock" => $subProduct->isInStock(), // (Optional) Boolean + "price" => $subProduct->getPrice(), // Float + "platform_data" => [ // (Optional) Hash/Object of attributes to store about the product specific to the integration platform (can be used in future filters) + "sku" => $subProduct->getSku() + ] + ]; + + $variantImage = $this->helper->getProductThumbnailUrl($subProduct); + if ($variantImage != $thumb && stripos($variantImage, '/placeholder') === false){ + $variant['thumbnail_url'] = $variantImage; + } + + $variantAttrVals = []; + foreach ($cfgAttr as $attr) { + $attrValIndex = $subProduct->getData($attr['attribute_code']); + foreach ($attr['values'] as $attrVal) { + if ($attrVal['value_index'] == $attrValIndex) { + $variantAttrVals[] = $attrVal['label']; + } + } + } + $variant['name'] = implode(' / ', $variantAttrVals); + + $productData['variants'][] = $variant; + } + } + + $this->pushProducts($productData); + } +} diff --git a/src/app/code/Fera/Ai/etc/adminhtml/events.xml b/src/app/code/Fera/Ai/etc/adminhtml/events.xml new file mode 100644 index 0000000..3fbf299 --- /dev/null +++ b/src/app/code/Fera/Ai/etc/adminhtml/events.xml @@ -0,0 +1,7 @@ + + + + + + diff --git a/src/app/code/Fera/Ai/etc/adminhtml/system.xml b/src/app/code/Fera/Ai/etc/adminhtml/system.xml index 6ebfde3..d8fa0ce 100644 --- a/src/app/code/Fera/Ai/etc/adminhtml/system.xml +++ b/src/app/code/Fera/Ai/etc/adminhtml/system.xml @@ -31,13 +31,16 @@ You can find your API keys at https://app.fera.ai/store/settings?tab=api - + + + + - + - + diff --git a/src/app/code/Fera/Ai/etc/config.xml b/src/app/code/Fera/Ai/etc/config.xml index 5902006..385658b 100644 --- a/src/app/code/Fera/Ai/etc/config.xml +++ b/src/app/code/Fera/Ai/etc/config.xml @@ -9,8 +9,9 @@ 0 - https://app.fera.ai/api/v2 - https://cdn.fera.ai/js/fera.js + https://app.fera.ai/ + https://app.fera.ai/api/v3 + https://cdn.fera.ai/js/v3/fera.js diff --git a/src/app/code/Fera/Ai/etc/di.xml b/src/app/code/Fera/Ai/etc/di.xml index 55ab5e4..924a37f 100644 --- a/src/app/code/Fera/Ai/etc/di.xml +++ b/src/app/code/Fera/Ai/etc/di.xml @@ -3,7 +3,7 @@ - + Magento\Framework\Filesystem\Driver\File diff --git a/src/app/code/Fera/Ai/etc/frontend/events.xml b/src/app/code/Fera/Ai/etc/frontend/events.xml new file mode 100644 index 0000000..cad7f1e --- /dev/null +++ b/src/app/code/Fera/Ai/etc/frontend/events.xml @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/src/app/code/Fera/Ai/view/frontend/layout/catalog_category_view.xml b/src/app/code/Fera/Ai/view/frontend/layout/catalog_category_view.xml deleted file mode 100644 index efc72d1..0000000 --- a/src/app/code/Fera/Ai/view/frontend/layout/catalog_category_view.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - diff --git a/src/app/code/Fera/Ai/view/frontend/layout/catalog_product_view.xml b/src/app/code/Fera/Ai/view/frontend/layout/catalog_product_view.xml index f8c9e6d..543b834 100644 --- a/src/app/code/Fera/Ai/view/frontend/layout/catalog_product_view.xml +++ b/src/app/code/Fera/Ai/view/frontend/layout/catalog_product_view.xml @@ -3,11 +3,7 @@ - - - + - - - - - - - - diff --git a/src/app/code/Fera/Ai/view/frontend/layout/checkout_onepage_success.xml b/src/app/code/Fera/Ai/view/frontend/layout/checkout_onepage_success.xml deleted file mode 100644 index 6c8ec10..0000000 --- a/src/app/code/Fera/Ai/view/frontend/layout/checkout_onepage_success.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - diff --git a/src/app/code/Fera/Ai/view/frontend/templates/footer.phtml b/src/app/code/Fera/Ai/view/frontend/templates/footer.phtml index 383036a..782d63c 100644 --- a/src/app/code/Fera/Ai/view/frontend/templates/footer.phtml +++ b/src/app/code/Fera/Ai/view/frontend/templates/footer.phtml @@ -4,22 +4,15 @@ window.feraStandaloneMode = true; window.fera = window.fera || []; getDebugJs(), PHP_EOL; ?> + window.fera.push('loadPlatformAdapter', "magento2"); window.fera.push('configure', { - store_pk: "getPublicKey(); ?>", - api_url: "getApiUrl(); ?>" + api_key: "getPublicKey(); ?>", + app_url: "getAppUrl(); ?>", + api_url: "getApiUrl(); ?>", }); - window.fera.push('loadPlatformAdapter', "magento2"); - - getShopperData() ?> - - window.fera.push('setShopper', jsonEncode($shopperData); ?>); - + - window.fera.push('setCart', getCartJson(); ?>); - - getChildHtml('fera_ai.footer.product.list'); ?> getChildHtml('fera_ai.footer.product.view'); ?> - getChildHtml('fera_ai.footer.checkout.success'); ?> diff --git a/src/app/code/Fera/Ai/view/frontend/templates/footer/checkout/success.phtml b/src/app/code/Fera/Ai/view/frontend/templates/footer/checkout/success.phtml deleted file mode 100644 index fbaaf42..0000000 --- a/src/app/code/Fera/Ai/view/frontend/templates/footer/checkout/success.phtml +++ /dev/null @@ -1,6 +0,0 @@ -isEnabled()): ?> - - diff --git a/src/app/code/Fera/Ai/view/frontend/templates/footer/product/list.phtml b/src/app/code/Fera/Ai/view/frontend/templates/footer/product/list.phtml deleted file mode 100644 index 0504ac2..0000000 --- a/src/app/code/Fera/Ai/view/frontend/templates/footer/product/list.phtml +++ /dev/null @@ -1,4 +0,0 @@ - diff --git a/src/app/code/Fera/Ai/view/frontend/templates/footer/product/view/under_add_to_cart.phtml b/src/app/code/Fera/Ai/view/frontend/templates/footer/product/view/under_add_to_cart.phtml deleted file mode 100644 index d2b0b52..0000000 --- a/src/app/code/Fera/Ai/view/frontend/templates/footer/product/view/under_add_to_cart.phtml +++ /dev/null @@ -1,5 +0,0 @@ -isEnabled()): ?> -
-