Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PO-187]("Add setting for age of orders to be picked in CancelPendingOrder cron.") #507

Merged
merged 8 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 46 additions & 14 deletions Cron/CancelPendingOrders.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ class CancelPendingOrders {
*/
protected $debug;

protected $isCancelPendingOrderAgeEnabled;

protected $pendingOrderAge;

/**
* CancelOrder constructor.
* @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
Expand Down Expand Up @@ -87,6 +91,8 @@ public function __construct(
$this->logger = $logger;
$this->isCancelPendingOrderCronEnabled = $this->config->isCancelPendingOrderCronEnabled();
$this->pendingOrderTimeout = ($this->config->getPendingOrderTimeout() > 0) ? $this->config->getPendingOrderTimeout() : 30;
$this->isCancelPendingOrderAgeEnabled = $this->config->isCancelPendingOrderAgeEnabled();
$this->pendingOrderAge = ($this->config->getPendingOrderAge() > 0) ? $this->config->getPendingOrderAge() : 43200;
rohitcbr marked this conversation as resolved.
Show resolved Hide resolved
$this->isCancelResetCartCronEnabled = $this->config->isCancelResetCartOrderCronEnabled();
$this->resetCartOrderTimeout = ($this->config->getResetCartOrderTimeout() > 0) ? $this->config->getResetCartOrderTimeout() : 30;
$this->debug = $debug;
Expand All @@ -101,24 +107,50 @@ public function execute()
$this->logger->info("Cronjob: Cancel Pending Order Cron started.");
$dateTimeCheck = date('Y-m-d H:i:s', strtotime('-' . $this->pendingOrderTimeout . ' minutes'));
$sortOrder = $this->sortOrderBuilder->setField('entity_id')->setDirection('DESC')->create();
$searchCriteria = $this->searchCriteriaBuilder
->addFilter(
'updated_at',
$dateTimeCheck,
'lt'
)->addFilter(
'status',
static::STATUS_PENDING,
'eq'
)->setSortOrders(
[$sortOrder]
)->create();

if ($this->isCancelPendingOrderAgeEnabled === true
&& $this->pendingOrderAge > $this->pendingOrderTimeout)
{
$this->logger->info("Cronjob: PendingOrderAge Enabled.");
rohitcbr marked this conversation as resolved.
Show resolved Hide resolved
$pendingOrderAgeCheck = date('Y-m-d H:i:s', strtotime('-' . $this->pendingOrderAge . ' minutes'));
$searchCriteria = $this->searchCriteriaBuilder
rohitcbr marked this conversation as resolved.
Show resolved Hide resolved
->addFilter(
'updated_at',
$dateTimeCheck,
'lt'
)->addFilter(
'updated_at',
$pendingOrderAgeCheck,
'gt'
)->addFilter(
'status',
static::STATUS_PENDING,
'eq'
)->setSortOrders(
[$sortOrder]
)->create();
}
else
{
$searchCriteria = $this->searchCriteriaBuilder
->addFilter(
'updated_at',
$dateTimeCheck,
'lt'
)->addFilter(
'status',
static::STATUS_PENDING,
'eq'
)->setSortOrders(
[$sortOrder]
)->create();
}

$orders = $this->orderRepository->getList($searchCriteria);
foreach ($orders->getItems() as $order)
{
if ($order->getPayment()->getMethod() === 'razorpay') {
$this->debug->log("Cronjob: Magento Order Id = " . $order->getIncrementId() . " picked for cancelation.");
$this->debug->log("Cronjob: Magento Order Id = " . $order->getIncrementId() . " picked for cancellation.");

$this->cancelOrder($order);
}
Expand Down Expand Up @@ -159,7 +191,7 @@ public function execute()
foreach ($orders->getItems() as $order)
{
if ($order->getPayment()->getMethod() === 'razorpay') {
$this->debug->log("Cronjob: Magento Order Id = " . $order->getIncrementId() . " picked for cancelation in reset cart cron.");
$this->debug->log("Cronjob: Magento Order Id = " . $order->getIncrementId() . " picked for cancellation in reset cart cron.");

$this->cancelOrder($order);
}
Expand Down
12 changes: 12 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Config
const WEBHOOK_SECRET = 'webhook_secret';
const ENABLE_PENDING_ORDERS_CRON = 'enable_pending_orders_cron';
const PENDING_ORDER_TIMEOUT = 'pending_orders_timeout';
const ENABLE_PENDING_ORDERS_AGE = 'enable_pending_orders_age';
const PENDING_ORDER_AGE = 'pending_orders_age';
const ENABLE_RESET_CART_CRON = 'enable_reset_cart_cron';
const RESET_CART_ORDERS_TIMEOUT = 'reset_cart_orders_timeout';
const DISABLE_UPGRADE_NOTICE = 'disable_upgrade_notice';
Expand Down Expand Up @@ -88,6 +90,16 @@ public function getPendingOrderTimeout()
return (int) $this->getConfigData(self::PENDING_ORDER_TIMEOUT);
}

public function isCancelPendingOrderAgeEnabled()
{
return (bool) (int) $this->getConfigData(self::ENABLE_PENDING_ORDERS_AGE, $this->storeId);
}

public function getPendingOrderAge()
{
return (int) $this->getConfigData(self::PENDING_ORDER_AGE);
}

public function isCancelResetCartOrderCronEnabled()
{
return (bool) (int) $this->getConfigData(self::ENABLE_RESET_CART_CRON, $this->storeId);
Expand Down
63 changes: 59 additions & 4 deletions Model/FormFieldTrack.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,54 @@ protected function _getElementHtml(AbstractElement $element)
url: '". $baseUrl ."razorpay/payment/FormDataAnalytics',
type: 'POST',
dataType: 'json',
data: {
event: 'Form Field Validation Error',
properties: validationData
data: {
event: 'Form Field Validation Error',
properties: validationData
}
})
}
})
}
else if (elementId == 'payment_us_razorpay_pending_orders_age')
{
element.blur(function(){
let elementVal = String(element.val())
let pendingOrderTimeoutElement = $('#' + 'payment_us_razorpay_pending_orders_timeout')
let pendingOrderTimeoutValue = String(pendingOrderTimeoutElement.val())

// Validations
let checkRequiredEntryBool = checkRequiredEntry(elementVal)
let checkIfValidDigitsBool = checkIfValidDigits(elementVal)
let checkIfNonNegativeBool = checkIfNonNegative(elementVal)
let checkIfInNumberRangeBool = checkIfInNumberRange(elementVal, 30, 525600)
let checkIfPendingOrderAgeGreaterThanTimeoutBool = checkIfPendingOrderAgeGreaterThanTimeout(elementVal, pendingOrderTimeoutValue)

if (
!checkRequiredEntryBool ||
!checkIfValidDigitsBool ||
!checkIfNonNegativeBool ||
!checkIfInNumberRangeBool ||
!checkIfPendingOrderAgeGreaterThanTimeoutBool
){
let validationData = {
'store_name' : storeName,
'field_name' : fieldName,
'field_type' : fieldType,
'required-entry' : checkRequiredEntryBool,
'validate-digits' : checkIfValidDigitsBool,
'validate-not-negative-number' : checkIfNonNegativeBool,
'digits-range-30-525600' : checkIfInNumberRangeBool,
'check-if-pending-order-age-greater-than-timeout' : checkIfPendingOrderAgeGreaterThanTimeoutBool
}

// Send event
$.ajax({
url: '". $baseUrl ."razorpay/payment/FormDataAnalytics',
type: 'POST',
dataType: 'json',
data: {
event: 'Form Field Validation Error',
properties: validationData
}
})
}
Expand All @@ -156,6 +201,7 @@ protected function _getElementHtml(AbstractElement $element)
'payment_us_razorpay_order_status' : $('#payment_us_razorpay_order_status').val(),
'payment_us_razorpay_auto_invoice' : $('#payment_us_razorpay_auto_invoice').val(),
'payment_us_razorpay_pending_orders_timeout' : $('#payment_us_razorpay_pending_orders_timeout').val(),
'payment_us_razorpay_pending_orders_age' : $('#payment_us_razorpay_pending_orders_age').val(),
}
let resultMapObject = new Map()

Expand All @@ -167,7 +213,8 @@ protected function _getElementHtml(AbstractElement $element)
row == 'payment_us_razorpay_key_secret' ||
row == 'payment_us_razorpay_order_status' ||
row == 'payment_us_razorpay_auto_invoice' ||
row == 'payment_us_razorpay_pending_orders_timeout'
row == 'payment_us_razorpay_pending_orders_timeout' ||
row == 'payment_us_razorpay_pending_orders_age'
)
{
// Check for empty fields
Expand Down Expand Up @@ -242,6 +289,14 @@ function checkIfInNumberRange(field, x, y)

return (fieldNum >= x && fieldNum <=y)? true : false;
}
function checkIfPendingOrderAgeGreaterThanTimeout(pendingOrderAge, pendingOrderTimeout)
{
let pendingOrderAgeInt = parseInt(pendingOrderAge);

let pendingOrderTimeoutInt = parseInt(pendingOrderTimeout);

return (pendingOrderAgeInt > pendingOrderTimeoutInt)? true : false;
}
";
}
}
26 changes: 24 additions & 2 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,36 @@
<validate>required-entry validate-digits validate-not-negative-number validate-digits-range digits-range-20-43200</validate>
</field>

<field id="enable_reset_cart_cron" translate="label" type="select" sortOrder="6" showInDefault="1" showInWebsite="0" showInStore="0">
<field id="enable_pending_orders_age" translate="label" type="select" sortOrder="6" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Pending Orders Age Enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<frontend_model>Razorpay\Magento\Model\FormFieldTrack</frontend_model>
<config_path>payment/razorpay/enable_pending_orders_age</config_path>
<depends>
<field id="enable_pending_orders_cron">1</field>
</depends>
</field>

<field id="pending_orders_age" translate="label" type="text" sortOrder="6" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Pending Orders Age</label>
<comment>Set age in minutes, (default: 43200 minutes). Age can be updated between range of 30 to 525600 minutes. This will enable cron for moving pending orders to Cancel status for given age. Please make sure to put age value more than timeout value in minutes.</comment>
<config_path>payment/razorpay/pending_orders_age</config_path>
<depends>
<field id="enable_pending_orders_cron">1</field>
<field id="enable_pending_orders_age">1</field>
</depends>
<frontend_model>Razorpay\Magento\Model\FormFieldTrack</frontend_model>
<validate>required-entry validate-digits validate-not-negative-number validate-digits-range digits-range-30-525600</validate>
</field>

<field id="enable_reset_cart_cron" translate="label" type="select" sortOrder="7" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Reset cart cron enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<frontend_model>Razorpay\Magento\Model\EnableResetCartOrdersCron</frontend_model>
<config_path>payment/razorpay/enable_reset_cart_cron</config_path>
</field>

<field id="reset_cart_orders_timeout" translate="label" type="text" sortOrder="6" showInDefault="1" showInWebsite="0" showInStore="0">
<field id="reset_cart_orders_timeout" translate="label" type="text" sortOrder="8" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Reset cart timeout</label>
<comment>Set timeout in minutes (default : 30 minutes). Timeout can be updated between the range of 20 minutes and 43200 minutes (30 days). This will enable cron for resetting the cart from new to canceled status for a given timeout.</comment>
<config_path>payment/razorpay/reset_cart_orders_timeout</config_path>
Expand Down
2 changes: 2 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
<webhook_triggered_at>0</webhook_triggered_at>
<enable_pending_orders_cron>1</enable_pending_orders_cron>
<pending_orders_timeout>30</pending_orders_timeout>
<enable_pending_orders_age>0</enable_pending_orders_age>
rohitcbr marked this conversation as resolved.
Show resolved Hide resolved
<pending_orders_age>43200</pending_orders_age>
<enable_reset_cart_cron>0</enable_reset_cart_cron>
<reset_cart_orders_timeout>30</reset_cart_orders_timeout>
<enable_custom_paid_order_status>0</enable_custom_paid_order_status>
Expand Down