Skip to content

Commit

Permalink
lower min version for newletter subscribers && add try catch to baske…
Browse files Browse the repository at this point in the history
…t tracking
  • Loading branch information
stubbedev committed Dec 5, 2024
1 parent dc84958 commit 50dc767
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 107 deletions.
99 changes: 55 additions & 44 deletions clerk.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,17 @@ class Clerk extends Module
* @var int
*/
protected $shop_id;
/**
* @var Clerk_Api
*/
protected $api;
/**
* @var ClerkLogger
*/
private $logger;
/**
* @var
*/
protected $language;

/**
Expand Down Expand Up @@ -2322,10 +2331,9 @@ public function renderForm()
}

$ClerkConfirm = <<<CLERKJS
<script>
function DOMready(fn) {
if (document.readyState != "loading") {
if (document.readyState !== "loading") {
fn();
} else if (document.addEventListener) {
document.addEventListener("DOMContentLoaded", fn);
Expand Down Expand Up @@ -2459,7 +2467,7 @@ class ConfirmDialog {
}
}
var before_logging_level;
let before_logging_level;
window.DOMready(function() {
Expand Down Expand Up @@ -2514,7 +2522,7 @@ class ConfirmDialog {
'name' => 'Debug message',
'html_content' => '<hr><strong>PrestaShop Debug Mode is disabled</strong>' .
'<p>When debug mode is disabled, PrestaShop hides a lot of errors and making it impossible for Clerk logger to detect and catch these errors.</p>' .
'<p>To make it possibel for Clerk logger to catch all errors you have to enable debug mode.</p>' .
'<p>To make it possible for Clerk logger to catch all errors you have to enable debug mode.</p>' .
'<p>Debug is not recommended in production in a longer period of time.</p>' .
'</br><p><strong>When you store is in debug mode</strong></p>' .
'<ul>' .
Expand Down Expand Up @@ -3318,63 +3326,65 @@ public function hookDisplayCartModalFooter($params)
*/
public function hookActionCartSave()
{
try {
$cookie = $this->context->cookie;

$cookie = $this->context->cookie;

if (Tools::getValue('add')) {
$this->context->cookie->clerk_show_powerstep = true;
$this->context->cookie->clerk_last_product = Tools::getValue('id_product');
}
if (Tools::getValue('add')) {
$this->context->cookie->clerk_show_powerstep = true;
$this->context->cookie->clerk_last_product = Tools::getValue('id_product');
}

$collect_baskets = Configuration::get('CLERK_DATASYNC_COLLECT_BASKETS', $this->context->language->id, null, $this->context->shop->id);
$collect_baskets = Configuration::get('CLERK_DATASYNC_COLLECT_BASKETS', $this->context->language->id, null, $this->context->shop->id);

if ($collect_baskets) {
if ($collect_baskets) {
if ($this->context->cart) {

if ($this->context->cart) {
$cart_products = [];
try {
$cart_products = $this->context->cart->getProducts();
} catch (Exception $e) {
return;
}

$cart_products = [];
try {
$cart_products = $this->context->cart->getProducts();
} catch (Exception $e) {
return;
}
$cart_product_ids = [];

$cart_product_ids = [];
foreach ($cart_products as $product)
$cart_product_ids[] = (int) $product['id_product'];

foreach ($cart_products as $product)
$cart_product_ids[] = (int) $product['id_product'];
if ($this->context->customer->email) {
$Endpoint = 'https://api.clerk.io/v2/log/basket/set';

if ($this->context->customer->email) {
$Endpoint = 'https://api.clerk.io/v2/log/basket/set';
$data_string = json_encode([
'key' => Configuration::get('CLERK_PUBLIC_KEY', $this->context->language->id, null, $this->context->shop->id),
'products' => $cart_product_ids,
'email' => $this->context->customer->email
]);

$data_string = json_encode([
'key' => Configuration::get('CLERK_PUBLIC_KEY', $this->context->language->id, null, $this->context->shop->id),
'products' => $cart_product_ids,
'email' => $this->context->customer->email
]);
$curl = curl_init();

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $Endpoint);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
curl_exec($curl);
} else {
if (isset($cookie->clerk_cart_products)) {

curl_setopt($curl, CURLOPT_URL, $Endpoint);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
curl_exec($curl);
} else {
if (isset($cookie->clerk_cart_products)) {
if ($cookie->clerk_cart_products != json_encode($cart_product_ids)) {

if ($cookie->clerk_cart_products != json_encode($cart_product_ids)) {
$this->context->cookie->clerk_cart_update = true;
$this->context->cookie->clerk_cart_products = json_encode($cart_product_ids);
}
} else {

$this->context->cookie->clerk_cart_update = true;
$this->context->cookie->clerk_cart_products = json_encode($cart_product_ids);
}
} else {

$this->context->cookie->clerk_cart_update = true;
$this->context->cookie->clerk_cart_products = json_encode($cart_product_ids);
}
}
}
} catch (Exception $e) {
$this->logger->log("Collect Baskets Exception", $e->getMessage());
}
}

Expand Down Expand Up @@ -3439,7 +3449,7 @@ public function hookDisplayOrderConfirmation($params)
/**
* Add clerk css to backend
*
* @param $arr
* @param $params
*/
public function hookActionAdminControllerSetMedia($params)
{
Expand Down Expand Up @@ -3627,7 +3637,7 @@ public function trackingScriptContent($params)
'clerk_datasync_collect_emails' => Configuration::get('CLERK_DATASYNC_COLLECT_EMAILS', $this->context->language->id, null, $this->context->shop->id),
'custom_clerk_js' => $custom_clerk_js_path,
'clerk_language' => $this->language,
'customer_logged_in' => ($this->context->customer->logged == 1) ? true : false,
'customer_logged_in' => $this->context->customer->logged == 1,
'customer_group_id' => (Customer::getDefaultGroupId((int) $this->context->customer->id) !== null) ? Customer::getDefaultGroupId((int) $this->context->customer->id) : false,
'customer_email' => $this->context->customer->email,
'currency_conversion_rate' => $currency_conversion_rate,
Expand Down Expand Up @@ -3704,3 +3714,4 @@ public function trackingScriptContent($params)
return $View;
}
}

109 changes: 46 additions & 63 deletions controllers/front/customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,9 @@ class ClerkCustomerModuleFrontController extends ClerkAbstractFrontController
public function __construct()
{
parent::__construct();

require_once (_PS_MODULE_DIR_. $this->module->name . '/controllers/admin/ClerkLogger.php');

$context = Context::getContext();

$this->logger = new ClerkLogger();

//Needed for PHP 5.3 support
$context = $this->context;

}
/**
* Get response
Expand Down Expand Up @@ -93,7 +86,7 @@ public function getJsonResponse()
foreach ($customers as $index => $customer) {
$customer_id = $customer['id'];
$address_id = Address::getFirstCustomerAddressId($customer_id);
$country_object = $this->getCustomerAddress($address_id, $customer_id);
$country_object = $this->getCustomerAddress($customer_id, $address_id);
if (count($country_object) === 1) {
$country_object = $country_object[0];
}
Expand All @@ -112,8 +105,8 @@ public function getJsonResponse()
}

if($get_sub_status){
$customers[$index]['subscribed'] = ($customers[$index]['subscribed'] == 1) ? true : false;
$customers[$index]['optin'] = ($customers[$index]['optin'] == 1) ? true : false;
$customers[$index]['subscribed'] = $customers[$index]['subscribed'] == 1;
$customers[$index]['optin'] = $customers[$index]['optin'] == 1;
} else {
unset($customers[$index]['subscribed']);
unset($customers[$index]['optin']);
Expand Down Expand Up @@ -142,26 +135,26 @@ public function getJsonResponse()

if($get_sub_status && $get_email){
if (version_compare(_PS_VERSION_, '1.7.0', '>=')) {
// Default newletter table for ^1.7.0 is ps_emailsubscription
$dbquery = new DbQuery();
$dbquery->select('CONCAT(\'N\', e.`id`) AS `id`, e.`email`, e.`active` AS `subscribed`');
$dbquery->from('emailsubscription', 'e');
$dbquery->leftJoin('shop', 's', 's.id_shop = e.id_shop');
$dbquery->leftJoin('lang', 'l', 'l.id_lang = e.id_lang');
$dbquery->where('e.id_shop = ' . $this->getShopId() . ' AND e.id_lang = ' . $this->getLanguageId());
$non_customers = Db::getInstance()->executeS($dbquery->build());
}elseif(version_compare(_PS_VERSION_, '1.6.2', '>=')) {
// Default newletter table for ^1.6.2 is ps_newsletter
$dbquery = new DbQuery();
$dbquery->select('CONCAT(\'N\', n.`id`) AS `id`, n.`email`, n.`active` AS `subscribed`');
$dbquery->from('newsletter', 'n');
$dbquery->leftJoin('shop', 's', 's.id_shop = n.id_shop');
$dbquery->where('n.id_shop = ' . $this->getShopId());
$non_customers = Db::getInstance()->executeS($dbquery->build());
// Default newsletter table for ^1.7.0 is ps_emailsubscription
$query = new DbQuery();
$query->select('CONCAT(\'N\', e.`id`) AS `id`, e.`email`, e.`active` AS `subscribed`');
$query->from('emailsubscription', 'e');
$query->leftJoin('shop', 's', 's.id_shop = e.id_shop');
$query->leftJoin('lang', 'l', 'l.id_lang = e.id_lang');
$query->where('e.id_shop = ' . $this->getShopId() . ' AND e.id_lang = ' . $this->getLanguageId());
$non_customers = Db::getInstance()->executeS($query->build());
}elseif(version_compare(_PS_VERSION_, '1.6.0', '>=')) {
// Default newsletter table for ^1.6.0 is ps_newsletter
$query = new DbQuery();
$query->select('CONCAT(\'N\', n.`id`) AS `id`, n.`email`, n.`active` AS `subscribed`');
$query->from('newsletter', 'n');
$query->leftJoin('shop', 's', 's.id_shop = n.id_shop');
$query->where('n.id_shop = ' . $this->getShopId());
$non_customers = Db::getInstance()->executeS($query->build());
}
if(isset($non_customers) && !empty($non_customers)){
if(!empty($non_customers)){
foreach ($non_customers as $index => $subscriber){
$non_customers[$index]['subscribed'] = ($non_customers[$index]['subscribed'] == 1) ? true : false;
$non_customers[$index]['subscribed'] = $subscriber['subscribed'] == 1;
}
$customers = array_merge($customers, $non_customers);
}
Expand All @@ -178,52 +171,42 @@ public function getJsonResponse()
}
}

public function getCustomerAddress($idAddress = null, $idCustomer)
/**
* @param $id_customer
* @param $id_address
* @return array|void
*/
public function getCustomerAddress($id_customer, $id_address = null)
{

$idLang = $this->getLanguageId();
$shareOrder = (bool) Context::getContext()->shop->getGroup()->share_order;

$csql = 'SELECT DISTINCT
a.`id_address` AS `id`,
a.`alias`,
a.`firstname`,
a.`lastname`,
a.`company`,
a.`address1`,
a.`address2`,
a.`postcode`,
a.`city`,
a.`id_state`,
s.name AS state,
s.`iso_code` AS state_iso,
a.`id_country`,
cl.`name` AS country,
co.`iso_code` AS country_iso,
a.`other`,
a.`phone`,
a.`phone_mobile`,
a.`vat_number`,
a.`dni`
$id_lang = $this->getLanguageId();
$share_order = (bool) Context::getContext()->shop->getGroup()->share_order;

$query = 'SELECT DISTINCT
a.`id_address` AS `id`, a.`alias`, a.`firstname`, a.`lastname`, a.`company`, a.`address1`,
a.`address2`, a.`postcode`, a.`city`, a.`id_state`, s.name AS state, s.`iso_code` AS state_iso,
a.`id_country`, cl.`name` AS country, co.`iso_code` AS country_iso, a.`other`, a.`phone`,
a.`phone_mobile`, a.`vat_number`, a.`dni`
FROM `' . _DB_PREFIX_ . 'address` a
LEFT JOIN `' . _DB_PREFIX_ . 'country` co ON (a.`id_country` = co.`id_country`)
LEFT JOIN `' . _DB_PREFIX_ . 'country_lang` cl ON (co.`id_country` = cl.`id_country`)
LEFT JOIN `' . _DB_PREFIX_ . 'state` s ON (s.`id_state` = a.`id_state`)
' . ($shareOrder ? '' : Shop::addSqlAssociation('country', 'co')) . '
' . ($share_order ? '' : Shop::addSqlAssociation('country', 'co')) . '
WHERE
`id_lang` = ' . (int) $idLang . '
AND `id_customer` = ' . (int) $idCustomer . '
`id_lang` = ' . (int) $id_lang . '
AND `id_customer` = ' . (int) $id_customer . '
AND a.`deleted` = 0
AND a.`active` = 1';

if (null !== $idAddress) {
$csql .= ' AND a.`id_address` = ' . (int) $idAddress;
if (null !== $id_address) {
$query .= ' AND a.`id_address` = ' . (int) $id_address;
}

$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($csql);

return $result;
try {
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
} catch (PrestaShopDatabaseException $e) {
$this->logger->log('PrestaShopDatabaseException', ['error' => $e->getMessage()]);
}
}


}

0 comments on commit 50dc767

Please sign in to comment.