b374k
m1n1 1.01
Apache/2.4.41 (Ubuntu)
Linux vmi616275.contaboserver.net 5.4.0-84-generic #94-Ubuntu SMP Thu Aug 26 20:27:37 UTC 2021 x86_64
uid=33(www-data) gid=33(www-data) groups=33(www-data)
server ip : 62.171.164.128 | your ip : 127.0.0.1
safemode OFF
 >  / home / dev2.destoffenstraat.com / vendor / mailcampaigns / magento2connector / Cron /
Filename/home/dev2.destoffenstraat.com/vendor/mailcampaigns/magento2connector/Cron/QuoteCron.php
Size6.43 kb
Permissionrw-r--r--
Ownerroot : root
Create time17-Aug-2025 10:26
Last modified03-Jun-2020 10:16
Last accessed23-Aug-2025 03:56
Actionsedit | rename | delete | download (gzip)
Viewtext | code | image
<?php

namespace MailCampaigns\Connector\Cron;

class QuoteCron {

protected $helper;
protected $resource;
protected $connection;
protected $tn__mc_api_pages;
protected $tn__mc_api_queue;
protected $mcapi;
protected $subscriberfactory;
protected $quoterepository;
protected $productrepository;
protected $taxhelper;
protected $quotefactory;

public function __construct(
\MailCampaigns\Connector\Helper\Data $dataHelper,
\Magento\Framework\App\ResourceConnection $Resource,
\Magento\Newsletter\Model\SubscriberFactory $SubscriberFactory,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
\Magento\Quote\Model\QuoteRepository $quoteRepository,
\Magento\Quote\Model\QuoteFactory $quoteFactory,
\Magento\Catalog\Helper\Data $taxHelper,
\MailCampaigns\Connector\Helper\MailCampaigns_API $mcapi
) {
$this->resource = $Resource;
$this->helper = $dataHelper;
$this->mcapi = $mcapi;
$this->subscriberfactory = $SubscriberFactory;
$this->quoterepository = $quoteRepository;
$this->productrepository = $productRepository;
$this->quotefactory = $quoteFactory;
$this->taxhelper = $taxHelper;
}

public function execute()
{
//database connection
$this->connection = $this->resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);

try
{
// Get table names
$tn__sales_flat_quote = $this->resource->getTableName('quote');
$tn__sales_flat_order = $this->resource->getTableName('sales_order');
$tn__sales_flat_quote_item = $this->resource->getTableName('quote_item');
$tn__mc_api_status = $this->resource->getTableName('mc_api_status');

// default time
$last_process_time = time() - 300; // default

// select latest time
$sql = "SELECT datetime FROM ".$tn__mc_api_status." WHERE type = 'quote_cron' ORDER BY datetime DESC LIMIT 1";
$rows = $this->connection->fetchAll($sql);
foreach ($rows as $row) { $last_process_time = $row["datetime"]; }

// delete old times
$sql = "DELETE FROM `".$tn__mc_api_status."` WHERE type = 'quote_cron'";
$this->connection->query($sql);

// save new one
$this->connection->insert($tn__mc_api_status, array(
'type' => 'quote_cron',
'datetime' => time()
));

// abandonded carts quotes
$quote_sql = "SELECT q.*
FROM `".$tn__sales_flat_quote."` AS q
WHERE q.updated_at >= '".gmdate("Y-m-d H:i:s", $last_process_time)."' OR q.created_at >= '".gmdate("Y-m-d H:i:s", $last_process_time)."'
ORDER BY `q`.`updated_at` DESC";
$quote_rows = $this->connection->fetchAll($quote_sql);

foreach ($quote_rows as $quote_row)
{
if ($this->helper->getConfig('mailcampaignsrealtimesync/general/import_quotes', $quote_row["store_id"]))
{
// If not unknown
if ($quote_row["customer_email"] != "" || $quote_row["customer_id"] > 0)
{
// Set API
$this->mcapi->APIStoreID = $quote_row["store_id"];
$this->mcapi->APIKey = $this->helper->getConfig('mailcampaignsapi/general/api_key', $this->mcapi->APIStoreID);
$this->mcapi->APIToken = $this->helper->getConfig('mailcampaignsapi/general/api_token', $this->mcapi->APIStoreID);

// get quote
$objectMan = \Magento\Framework\App\ObjectManager::getInstance();
$quote = $objectMan->create('Magento\Quote\Model\Quote')->load($quote_row["entity_id"]);

$quote_data = $quote_row;
$quote_data["store_id"] = $quote_row["store_id"];

if(is_object($quote->getShippingAddress()))
{
$address = $quote->getShippingAddress();

$quote_data["BaseShippingAmount"] = $address->getBaseShippingAmount();
$quote_data["BaseShippingDiscountAmount"] = $address->getBaseShippingDiscountAmount();
$quote_data["BaseShippingHiddenTaxAmount"] = $address->getBaseShippingHiddenTaxAmount();
$quote_data["BaseShippingInclTax"] = $address->getBaseShippingInclTax();
$quote_data["BaseShippingTaxAmount"] = $address->getBaseShippingTaxAmount();

$quote_data["ShippingAmount"] = $address->getShippingAmount();
$quote_data["ShippingDiscountAmount"] = $address->getShippingDiscountAmount();
$quote_data["ShippingHiddenTaxAmount"] = $address->getShippingHiddenTaxAmount();
$quote_data["ShippingInclTax"] = $address->getShippingInclTax();
$quote_data["ShippingTaxAmount"] = $address->getShippingTaxAmount();
}

// vat
$quote_data["grand_total_vat"] = $quote_data["grand_total"] - $quote_data["subtotal"];
$quote_data["base_grand_total_vat"] = $quote_data["base_grand_total"] - $quote_data["base_subtotal"];
$quote_data["grand_total_with_discount_vat"] = $quote_data["grand_total"] - $quote_data["subtotal_with_discount"];
$quote_data["base_grand_total_with_discount_vat"] = $quote_data["base_grand_total"] - $quote_data["base_subtotal_with_discount"];

// update quote
$this->mcapi->DirectOrQueueCall("update_magento_abandonded_cart_quotes", array($quote_data));

// delete products first
$this->mcapi->DirectOrQueueCall("delete_magento_abandonded_cart_products", array("quote_id" => $quote_row["entity_id"], "store_id" => $quote_row["store_id"]));

// update items
$quote_object = $this->quotefactory->create()->loadByIdWithoutStore($quote_row["entity_id"]);
$items = $quote_object->getAllVisibleItems();

$i = 0;
$quote_item_data = array();
foreach ($items as $item)
{
$getdata = array(); foreach ($item->getData() as $y => $tmp) if (!is_array($tmp) && !is_object($tmp)) { $getdata[$y] = $tmp; };

$simpleproduct = $this->productrepository->get($getdata["sku"]);
$getdata["image"] = $simpleproduct->getData('image');

$quote_item_data[$i] = $getdata;
$i++;
}

if ($i > 0)
{
// insert products
$this->mcapi->DirectOrQueueCall("update_magento_abandonded_cart_products", $quote_item_data);
}
}
}
}
}
catch (\Magento\Framework\Exception\NoSuchEntityException $e){
$this->mcapi->DebugCall($e->getMessage());
}
catch (Exception $e){
$this->mcapi->DebugCall($e->getMessage());
}

return $this;
}
}