Skip to content

Commit

Permalink
Merge pull request #53 from feedaty/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
altravista committed Mar 23, 2022
2 parents 0354c56 + 1bb7ebf commit 3f366a1
Show file tree
Hide file tree
Showing 9 changed files with 421 additions and 324 deletions.
38 changes: 19 additions & 19 deletions Controller/Adminhtml/Index/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ class Index extends \Magento\Backend\App\Action
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;

/**
* @var \Magento\Framework\ObjectManagerInterface
*/
*/
protected $objectManager;

/**
Expand Down Expand Up @@ -66,15 +66,15 @@ public function __construct(

/*
* Execute
*
*
*/
public function execute() {

# INIT FIELDS

$store_id = (int) $this->_request->getParam('store', 0);

if ($store_id === 0)
if ($store_id === 0)
{
$store_id = $this->_storeManager->getStore()->getId();
}
Expand All @@ -84,7 +84,7 @@ public function execute() {
$fdDebugEnabled = $this->_scopeConfig->getValue('feedaty_global/debug/debug_enabled', $scope_store);
$last4months = date('Y-m-d', strtotime("-4 months"));

# END INIT FIELDS
# END INIT FIELDS

# DEBUG

Expand All @@ -95,7 +95,7 @@ public function execute() {
$feedatyHelper->feedatyDebug($message, "FEEDATY CSV PARAMS");
}

# END DEBUG
# END DEBUG

# HANDLER

Expand All @@ -120,7 +120,7 @@ public function execute() {

$this->_csv->setDelimiter($delimiter);
$this->_csv->setEnclosure($enclosure);

# END HANDLER

# FORMAT DATA
Expand All @@ -139,15 +139,15 @@ public function execute() {

$tmp['Id'] = $item->getProductId();

$tmp['Url'] = $fd_oProduct->getUrlModel()->getUrl($fd_oProduct);
$tmp['Url'] = $fd_oProduct->setStoreId($item->getStoreId())->getUrlInStore();

if ($fd_oProduct->getImage() != "no_selection")
{
if ($fd_oProduct->getImage() != "no_selection")
{
// TODO: Use store manager
$store = $this->_objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore();
$store = $this->_objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore($item->getStoreId());
$tmp['ImageUrl'] = $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product' . $fd_oProduct->getImage();
}
else
else
{
$tmp['ImageUrl'] = "";
}
Expand All @@ -158,13 +158,13 @@ public function execute() {

$row = [
$order->getId(),
$order->getBillingAddress()->getEmail(),
$order->getBillingAddress()->getEmail(),
$order->getCreatedAt(),
$item->getProductId(),
str_replace('"','""',$tmp['Name']),
$tmp['Url'],
$tmp['ImageUrl'],
$order->getBillingAddress()->getEmail(),
$order->getBillingAddress()->getEmail(),
$order->getCreatedAt(),
$item->getProductId(),
str_replace('"','""',$tmp['Name']),
$tmp['Url'],
$tmp['ImageUrl'],
"Magento".$productMetadata->getVersion()."CSV"
];

Expand Down
152 changes: 93 additions & 59 deletions Cron/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Feedaty\Badge\Cron;

use Feedaty\Badge\Helper\Data;
use Feedaty\Badge\Helper\Orders as OrdersHelper;
use Feedaty\Badge\Model\Config\Source\WebService;
use Psr\Log\LoggerInterface;
Expand All @@ -23,19 +24,25 @@ class Orders
*/
private $webService;

protected $dataHelper;

/**
* @param LoggerInterface $logger
* @param OrdersHelper $ordersHelper
* @param WebService $webService
* @param Data $dataHelper
*/
public function __construct(
LoggerInterface $logger,
OrdersHelper $ordersHelper,
WebService $webService
WebService $webService,
Data $dataHelper
)
{
$this->logger = $logger;
$this->ordersHelper = $ordersHelper;
$this->webService = $webService;
$this->dataHelper = $dataHelper;
}

/**
Expand All @@ -48,87 +55,114 @@ public function execute()
*/
$this->webService->fdSendInstallationInfo();


//Starter Log
$this->logger->info("Feedaty | START Cronjob | Set Feedaty Orders | date: " . date('Y-m-d H:i:s') );

$orders = $this->ordersHelper->getOrders();
/**
* Get stores
*/
$storesIds = $this->dataHelper->getAllStoresIds();

foreach ($storesIds as $storeId) {

$data = array();
/**
* Get Orders
*/
$orders = $this->ordersHelper->getOrders($storeId);

/* Order Increment */
$i = 0;
$data = [];

if(count($orders) > 0){
foreach ($orders as $order){
/* Order Increment */
$i = 0;

/* Get all visible order products */
$items = $order->getAllVisibleItems();
if(count($orders) > 0){
foreach ($orders as $order){

$storeId = $order->getStoreId();
/**
* Get Locale
*/
$localeCode = $this->ordersHelper->getCulture($storeId);
/* Get all visible order products */
$items = $order->getAllVisibleItems();

$data[$i] = [
'ID' => $order->getEntityId(),
'Date' => $order->getCreatedAt(),
'CustomerEmail' => $order->getCustomerEmail(),
'CustomerID' => $order->getCustomerEmail(),
'Culture' => $localeCode,
'Platform' => $this->ordersHelper->getPlatform(),
'Products' => []
];
/**
* Get Locale
*/
$localeCode = $this->ordersHelper->getCulture($storeId);

foreach ($items as $item){
$data[$i] = [
'ID' => $order->getEntityId(),
'Date' => $order->getCreatedAt(),
'CustomerEmail' => $order->getCustomerEmail(),
'CustomerID' => $order->getCustomerEmail(),
'Culture' => $localeCode,
'Platform' => $this->ordersHelper->getPlatform(),
'Products' => []
];

$productId = $item->getProductId();
foreach ($items as $item){

$productThumbnailUrl = $this->ordersHelper->getProductThumbnailUrl($item);
/**
* Get Product Id
*/
$productId = $item->getProductId();

if ($item->getParentItem()) {
$product = $item->getParentItem()->getProduct();
} else {
$product = $item->getProduct();
}
$productUrl = '';
if($product){
$productUrl = $product->getProductUrl();
/**
* Get Product Thumbnail
*/
$productThumbnailUrl = $this->ordersHelper->getProductThumbnailUrl($item);

if ($item->getParentItem()) {
$product = $item->getParentItem()->getProduct();
} else {
$product = $item->getProduct();
}

/*
* Get Product Url
*/
$productUrl = '';
if($product){
$productUrl = $product->getProductUrl();
}

array_push($data[$i]['Products'],
[
'SKU' => $productId ,
'URL' => $productUrl,
'ThumbnailURL' => $productThumbnailUrl,
'Name' => $item->getName()
]
);
}
array_push($data[$i]['Products'],
[
'SKU' => $productId ,
'URL' => $productUrl,
'ThumbnailURL' => $productThumbnailUrl,
'Name' => $item->getName()
]
);
}

$this->ordersHelper->setFeedatyCustomerNotified($order->getEntityId());
$i++;
}
/**
* Set Order As Sent
*/
$this->ordersHelper->setFeedatyCustomerNotified($order->getEntityId());

$response = (array) $this->webService->sendOrder($data);
$i++;
}

if(!empty($response)){
if(isset($response['Data'])){
foreach ($response['Data'] as $data){
//if order Success or Duplicated set Feedaty Customer Notification true
if($data['Status'] == '1' || $data['Status'] == '201'){
$this->logger->info("Feedaty | Order sent successfull: order ID " . $order->getEntityId() . ' - date: ' . date('Y-m-d H:i:s') );
}
else {
$this->logger->critical("Feedaty | Order not sent: order ID " .$order->getEntityId() . ' - date: ' . date('Y-m-d H:i:s') );
$response = (array) $this->webService->sendOrder($data, $storeId);

if(!empty($response)){
if(isset($response['Data'])){
foreach ($response['Data'] as $dataResponse){
//if order Success or Duplicated set Feedaty Customer Notification true
if($dataResponse['Status'] == '1' || $dataResponse['Status'] == '201'){
$this->logger->info("Feedaty | Order sent successfull: order ID " . $order->getEntityId() . ' - date: ' . date('Y-m-d H:i:s') );
}
else {
$this->logger->critical("Feedaty | Order not sent: order ID " . $order->getEntityId() . ' - date: ' . date('Y-m-d H:i:s') );
}
}
}
else {
$this->logger->critical("Feedaty | No Data Response" . print_r($response,true));
}
}
else {
$this->logger->critical("Feedaty | Empty Response" );
}
}
}

}

}

0 comments on commit 3f366a1

Please sign in to comment.