Skip to content

Commit

Permalink
feat(code): add option to show not logged-in users (#1)
Browse files Browse the repository at this point in the history
Add the possibility to show subscription information on product pages to not logged-in users
  • Loading branch information
zoltan-blueant authored Nov 11, 2022
1 parent 12a6bbf commit b8a8abe
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 44 deletions.
Binary file removed Quickpay subscription - User Manual - 1.0.0.pdf
Binary file not shown.
Binary file added Quickpay subscription - User Manual - 1.0.1.pdf
Binary file not shown.
8 changes: 8 additions & 0 deletions controllers/front/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ public function displayAjaxCheck()
$status = true;
}

if (!Context::getContext()->customer->isLogged() && $statusSubscribe) {
$this->ajaxRender(json_encode([
'status' => true,
'error' => 'login'
]));
return;
}

$plans = array_unique($plans);
$frequencies = array_unique($frequencies);

Expand Down
67 changes: 55 additions & 12 deletions quickpaysubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct()
{
$this->name = 'quickpaysubscription';
$this->tab = 'payments_gateways';
$this->version = '1.0.0';
$this->version = '1.0.1';
$this->author = 'Quickpay';
$this->need_instance = 0;
$this->ps_versions_compliancy = [
Expand Down Expand Up @@ -125,6 +125,7 @@ public function createTabs()
public function hooks()
{
$hookList = [
'actionAuthentication',
'actionProductAdd',
'actionProductUpdate',
'actionProductDelete',
Expand Down Expand Up @@ -259,6 +260,23 @@ public function renderForm()
),
),
],
[
'type' => 'switch',
'label' => $this->getTranslator()->trans('Show subscription information when not logged in', [], 'Modules.Quickpaysubscription.Admin'),
'name' => 'QUICKPAY_SUBSCRIPTION_SHOW_WHEN_NOT_LOGGED_IN',
'values' => array(
array(
'id' => 'show_not_logged_in_on',
'value' => 1,
'label' => $this->l('Enabled'),
),
array(
'id' => 'show_not_logged_in_off',
'value' => 0,
'label' => $this->l('Disabled'),
),
),
],
],
'submit' => [
'title' => $this->getTranslator()->trans('Save', [], 'Admin.Actions'),
Expand Down Expand Up @@ -338,6 +356,7 @@ public function getConfigFieldsValues($type = 'general')
'QUICKPAY_SUBSCRIPTION_STATUS' => Tools::getValue('QUICKPAY_SUBSCRIPTION_STATUS', Configuration::get('QUICKPAY_SUBSCRIPTION_STATUS')),
'QUICKPAY_SUBSCRIPTION_USER_CANCEL' => Tools::getValue('QUICKPAY_SUBSCRIPTION_USER_CANCEL', Configuration::get('QUICKPAY_SUBSCRIPTION_USER_CANCEL')),
'QUICKPAY_SUBSCRIPTION_DISABLE_PAYMENTS' => Tools::getValue('QUICKPAY_SUBSCRIPTION_DISABLE_PAYMENTS', Configuration::get('QUICKPAY_SUBSCRIPTION_DISABLE_PAYMENTS')),
'QUICKPAY_SUBSCRIPTION_SHOW_WHEN_NOT_LOGGED_IN' => Tools::getValue('QUICKPAY_SUBSCRIPTION_SHOW_WHEN_NOT_LOGGED_IN', Configuration::get('QUICKPAY_SUBSCRIPTION_SHOW_WHEN_NOT_LOGGED_IN')),
];
}
}
Expand All @@ -360,7 +379,8 @@ public function postProcess($type = 'general')
$keys = [
'QUICKPAY_SUBSCRIPTION_STATUS',
'QUICKPAY_SUBSCRIPTION_USER_CANCEL',
'QUICKPAY_SUBSCRIPTION_DISABLE_PAYMENTS'
'QUICKPAY_SUBSCRIPTION_DISABLE_PAYMENTS',
'QUICKPAY_SUBSCRIPTION_SHOW_WHEN_NOT_LOGGED_IN',
];
break;
}
Expand Down Expand Up @@ -564,8 +584,7 @@ public function hookActionProductDelete()
*/
public function hookActionObjectProductInCartDeleteAfter($params)
{
if (!Configuration::get('QUICKPAY_SUBSCRIPTION_STATUS') ||
!$this->context->customer->isLogged()) {
if (!Configuration::get('QUICKPAY_SUBSCRIPTION_STATUS')) {
return false;
}

Expand All @@ -586,8 +605,7 @@ public function hookActionObjectProductInCartDeleteAfter($params)
*/
public function hookActionCartUpdateQuantityBefore($params)
{
if (!Configuration::get('QUICKPAY_SUBSCRIPTION_STATUS') ||
!$this->context->customer->isLogged()) {
if (!Configuration::get('QUICKPAY_SUBSCRIPTION_STATUS')) {
return false;
}

Expand Down Expand Up @@ -618,14 +636,20 @@ public function hookActionCartUpdateQuantityBefore($params)
*/
public function hookDisplayProductPriceBlock($params)
{
if (!Configuration::get('QUICKPAY_SUBSCRIPTION_STATUS') || !$this->context->customer->isLogged()) {
if (!Configuration::get('QUICKPAY_SUBSCRIPTION_STATUS')) {
return false;
}

if (!Context::getContext()->customer->isLogged()) {
if (!Configuration::get('QUICKPAY_SUBSCRIPTION_SHOW_WHEN_NOT_LOGGED_IN')) {
return false;
}
}

$idProduct = (int) Tools::getValue('id_product');
$idProductAttribute = (int) Tools::getValue('id_product_attribute', 0);

if (!$idProduct || !Configuration::get('QUICKPAY_SUBSCRIPTION_STATUS')) {
if (!$idProduct) {
return false;
}

Expand Down Expand Up @@ -658,7 +682,7 @@ public function hookDisplayProductPriceBlock($params)
*/
public function hookActionFrontControllerSetMedia($params)
{
if (!Configuration::get('QUICKPAY_SUBSCRIPTION_STATUS') || !$this->context->customer->isLogged()) {
if (!Configuration::get('QUICKPAY_SUBSCRIPTION_STATUS')) {
return false;
}

Expand All @@ -673,7 +697,7 @@ public function hookActionFrontControllerSetMedia($params)
'modules/'.$this->name.'/views/js/script.js',
[
'priority' => 300,
'attribute' => 'async',
'attribute' => 'defer',
]
);

Expand Down Expand Up @@ -713,8 +737,7 @@ public function hookDisplayShoppingCartFooter()
{
if (!Module::isEnabled('quickpay') ||
!Configuration::get('QUICKPAY_SUBSCRIPTION_STATUS') ||
!in_array($this->context->controller->php_self, ['cart', 'order']) ||
!$this->context->customer->isLogged()) {
!in_array($this->context->controller->php_self, ['cart', 'order'])) {
return false;
}

Expand Down Expand Up @@ -1063,4 +1086,24 @@ public function addLog($message, $severity = 1, $error_code = null, $object_type
{
Logger::addLog($message, $severity, $error_code, $object_type, $object_id);
}

/**
* Update the subscription cart after successful login
*
* @throws PrestaShopException
* @throws PrestaShopDatabaseException
*/
public function hookActionAuthentication($params)
{

$subscriptionCartId = QuickpaySubscriptionCart::getIdByCartId(Context::getContext()->cart->id);
if (!$subscriptionCartId) {
return false;
}

$subscriptionCart = new QuickpaySubscriptionCart($subscriptionCartId);
$subscriptionCart->id_customer = Context::getContext()->customer->id;

$subscriptionCart->save();
}
}
16 changes: 16 additions & 0 deletions src/QuickpaySubscriptionCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,22 @@ public static function cartHasSubscriptionProduct($idCart = 0)
return $status;
}

/**
* Get subscription cart ID by Prestashop cart ID
*
* @param $cartId
* @return int
*/
public static function getIdByCartId($cartId)
{
$sql = new DbQuery();
$sql->select(self::$definition['primary']);
$sql->from(self::$definition['table']);
$sql->where('`id_cart` = ' . $cartId);

return (int) \Db::getInstance()->getValue($sql);
}

/**
* Get the selected plan and frequency for the cart
*
Expand Down
10 changes: 10 additions & 0 deletions upgrade/upgrade-1.0.1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

if (!defined('_PS_VERSION_')) {
exit;
}

function upgrade_module_1_0_1($module)
{
return $module->registerHook('actionAuthentication');
}
117 changes: 85 additions & 32 deletions views/templates/hook/cart.tpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<div class="quickpay-subscription-cart-status" style="display: none">
<div class="col-md-12 alert-danger text-black py-1 my-1">
{l s='The cart contains both subsciption and not subscription products or the subscription product\'s frequency are difference. Please remove one of type to continue' mod='quickpaysubscription'}
<div class="col-md-12 alert-danger text-black py-1 my-1 general" style="display: none">
{l s='The cart contains both subscription and not subscription products or the subscription product\'s frequency are difference. Please remove one of type to continue' mod='quickpaysubscription'}
</div>
<div class="col-md-12 alert-danger text-black py-1 my-1 login" style="display: none">
{l s='The cart contains subscription product(s). Please log in first to continue' mod='quickpaysubscription'}
</div>
</div>

Expand All @@ -10,7 +13,7 @@
$.ajax({
url: quickpaysubscription_ajax_url,
cache: false,
async: false,
async: true,
type: 'POST',
data: {
ajax: true,
Expand All @@ -20,43 +23,93 @@
success: function (response) {
response = JSON.parse(response)
if (response['status'] === true) {
document.querySelector('.checkout.cart-detailed-actions a').classList.add('disabled')
document.querySelector('.quickpay-subscription-cart-status').style.display = 'block'
if (typeof response['error'] !== 'undefined') {
if (document.querySelector('.checkout.cart-detailed-actions a') != null) {
document.querySelector('.checkout.cart-detailed-actions a').classList.add('disabled')
}
if (document.querySelector('.checkout.cart-detailed-actions .mobilepay-checkout') != null) {
document.querySelector('.checkout.cart-detailed-actions .mobilepay-checkout').classList.add('disabled')
}
document.querySelector('.quickpay-subscription-cart-status').style.display = 'block'
document.querySelector('.quickpay-subscription-cart-status .login').style.display = 'block'
} else {
if (document.querySelector('.checkout.cart-detailed-actions a') != null) {
document.querySelector('.checkout.cart-detailed-actions a').classList.add('disabled')
}
if (document.querySelector('.checkout.cart-detailed-actions .mobilepay-checkout') != null) {
document.querySelector('.checkout.cart-detailed-actions .mobilepay-checkout').classList.add('disabled')
}
document.querySelector('.quickpay-subscription-cart-status').style.display = 'block'
document.querySelector('.quickpay-subscription-cart-status .general').style.display = 'block'
}
} else {
document.querySelector('.checkout.cart-detailed-actions a').classList.remove('disabled')
if (document.querySelector('.checkout.cart-detailed-actions a') != null) {
document.querySelector('.checkout.cart-detailed-actions a').classList.remove('disabled')
}
if (document.querySelector('.checkout.cart-detailed-actions .mobilepay-checkout') != null) {
document.querySelector('.checkout.cart-detailed-actions .mobilepay-checkout').classList.remove('disabled')
}
document.querySelector('.quickpay-subscription-cart-status').style.display = 'none'
document.querySelector('.quickpay-subscription-cart-status .general').style.display = 'none'
document.querySelector('.quickpay-subscription-cart-status .login').style.display = 'none'
}
}
})
}, 250);
prestashop.on('updateCart',
function (e) {
window.setTimeout(function () {
$.ajax({
url: quickpaysubscription_ajax_url,
cache: false,
async: false,
type: 'POST',
data: {
ajax: true,
action: 'check',
token: quickpaysubscription_token
},
success: function (response) {
response = JSON.parse(response)
if (response['status'] === true) {
document.querySelector('.checkout.cart-detailed-actions a').classList.add('disabled')
document.querySelector('.quickpay-subscription-cart-status').style.display = 'block'
} else {
document.querySelector('.checkout.cart-detailed-actions a').classList.remove('disabled')
document.querySelector('.quickpay-subscription-cart-status').style.display = 'none'
}
}
})
}, 250);
});
function (e) {
window.setTimeout(function () {
$.ajax({
url: quickpaysubscription_ajax_url,
cache: false,
async: true,
type: 'POST',
data: {
ajax: true,
action: 'check',
token: quickpaysubscription_token
},
success: function (response) {
response = JSON.parse(response)
window.setTimeout(function () {
if (response['status'] === true) {
if (typeof response['error'] !== 'undefined') {
if (document.querySelector('.checkout.cart-detailed-actions a') != null) {
document.querySelector('.checkout.cart-detailed-actions a').classList.add('disabled')
}
if (document.querySelector('.checkout.cart-detailed-actions .mobilepay-checkout') != null) {
document.querySelector('.checkout.cart-detailed-actions .mobilepay-checkout').classList.add('disabled')
}
document.querySelector('.quickpay-subscription-cart-status').style.display = 'block'
document.querySelector('.quickpay-subscription-cart-status .login').style.display = 'block'
} else {
if (document.querySelector('.checkout.cart-detailed-actions a') != null) {
document.querySelector('.checkout.cart-detailed-actions a').classList.add('disabled')
}
if (document.querySelector('.checkout.cart-detailed-actions .mobilepay-checkout') != null) {
document.querySelector('.checkout.cart-detailed-actions .mobilepay-checkout').classList.add('disabled')
}
document.querySelector('.quickpay-subscription-cart-status').style.display = 'block'
document.querySelector('.quickpay-subscription-cart-status .general').style.display = 'block'
}
} else {
if (document.querySelector('.checkout.cart-detailed-actions a') != null) {
document.querySelector('.checkout.cart-detailed-actions a').classList.remove('disabled')
}
if (document.querySelector('.checkout.cart-detailed-actions .mobilepay-checkout') != null) {
document.querySelector('.checkout.cart-detailed-actions .mobilepay-checkout').classList.remove('disabled')
}
document.querySelector('.quickpay-subscription-cart-status').style.display = 'none'
document.querySelector('.quickpay-subscription-cart-status .general').style.display = 'none'
document.querySelector('.quickpay-subscription-cart-status .login').style.display = 'none'
}
}, 150);
}
})
}, 250);
});
})
</script>

Expand Down

0 comments on commit b8a8abe

Please sign in to comment.