below is the cron job code that suppose to delete customer if they have value of 1 in deleteOrders field but it not doing that, what am i missing?
<?php
namespace Vendor\ThreeYearOrders\Cron;
use Magento\Customer\Model\Session;
class DeleteOrder
{
/**
* @var Session
*/
protected $_customerSession;
/**
* @param Session $customerSession
*/
public function __construct(Session $customerSession)
{
$this->_customerSession = $customerSession;
}
public function execute()
{
$customerId = $this->_customerSession->getCustomerId();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$resource = $objectManager->get('\Magento\Framework\App\ResourceConnection');
$log = $objectManager->get('\Psr\Log\LoggerInterface');
$connection = $resource->getConnection();
$tableName = $connection->getTableName('customer_entity');
try {
$where = [
'entity_id = ?' => $customerId,
'deleteOrders = ?' => 1
];
$connection->delete($tableName, $where);
$log->info('Customer is deleted');
} catch (\Exception $e) {
$log->error("Error deleting customer: " . $e->getMessage());
}
}
}
This is crontab.xml file below that execute and run cronjob file
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
<group id="default">
<job name="Vendor_threeyearorders_delete_orders" instance="Vendor\ThreeYearOrders\Cron\DeleteOrder" method="execute">
<schedule>* * * * *</schedule>
</job>
</group>
</config>
Setup file
<?php
namespace Vendor\ThreeYearOrders\Setup;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\DB\Ddl\Table;
class InstallSchema implements InstallSchemaInterface
{
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$installer->startSetup();
$table = $installer->getTable('customer_entity');
$installer->getConnection()->addColumn(
$table,
'deleteOrders',
[
'type' => Table::TYPE_INTEGER,
'nullable' => true,
'default' => null,
'comment' => 'Delete Orders'
]
);
$installer->endSetup();
}
}