0

How do I change not using ObjectManager directly? and use Dependency injection? what about my SQL query? My code is work, but seems not in good Magento style, anyone can help to correct me? I am appreciate if anyone can help

<?php


declare(strict_types=1);

namespace ABC\Fixpayment\Cron;

use Magento\Email\Model\BackendTemplate;
use Magento\Framework\Mail\Template\TransportBuilder;
use Psr\Log\LoggerInterface;

class Check
{   

    private $logger;

    private $transportBuilder;

    public function __construct(
        LoggerInterface $logger,
        BackendTemplate $emailTemplate,
        TransportBuilder $transportBuilder
    )
    {
        $this->logger = $logger;
        $this->transportBuilder = $transportBuilder;
        $this->emailTemplate = $emailTemplate;
    }

    public function execute()
    {
        //Cron job - get sales_order_payment information by below query.

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
         $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
         $connection = $resource->getConnection();
         $tableName = $resource->getTableName('sales_order_payment');
         $attribute_information = "SELECT sales_order.increment_id, sales_order_payment.additional_information 
            FROM sales_order LEFT JOIN sales_order_payment ON sales_order.entity_id = sales_order_payment.parent_id 
            WHERE sales_order_payment.method = 'fixpayment' AND sales_order.updated_at >= NOW() - INTERVAL 30 MINUTE";

        $results = $connection->fetchAll($attribute_information);


// Fetch sql query result

    foreach ($results as $result) {
            $increment_id = $result['increment_id'];
            $additional_information = $result['additional_information'];            
            $additional_information_arr = json_decode($additional_information); 
            $address_verification_json = $additional_information_arr->shipping_address_verification; 
            $address_verification_arr = json_decode($address_verification_json);
            $postal_code = $address_verification_arr->postal_code;
        
        //Grab Postal Code, if score lower than 1, email to customer support
        
        if ($postal_code < 1) {
            
    
            try {
            $templateVars = [
                            'order_number' => $increment_id,
                            'postal_code_score' => $postal_code,
                            'fixOrderId' => $additional_information_arr->fixOrderId
            ];
            $emails = ['receiver@gmail.com'];
            $email_template = $this->emailTemplate->load('fix_temp', 'template_code');
            $email_template_id = $email_template->getTemplateId();  
            $transport = $this->transportBuilder->setTemplateIdentifier($email_template_id)
            ->setTemplateOptions( [ 'area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => 1 ] )
            ->setTemplateVars( $templateVars )
            ->setFrom( [ "name" => "Magento Fix Order", "email" => "sender@gmail.com" ] )
            ->addTo($emails)
            ->setReplyTo('sender@gmail.com')
            ->getTransport();
            $transport->sendMessage(); 
            } catch (Exception $e) {
                $this->logger->error($e);
            }
            
        }  
     
    }

    }

}
1
<?php


declare(strict_types=1);

namespace ABC\Fixpayment\Cron;

use Magento\Email\Model\BackendTemplate;
use Magento\Framework\Mail\Template\TransportBuilder;
use Psr\Log\LoggerInterface;

class Check
{

    private $logger;

    private $transportBuilder;

    private $_resourceConnection;

    public function __construct(
        LoggerInterface $logger,
        BackendTemplate $emailTemplate,
        TransportBuilder $transportBuilder,
        \Magento\Framework\App\ResourceConnection $resourceConnection
    )
    {
        $this->logger = $logger;
        $this->transportBuilder = $transportBuilder;
        $this->emailTemplate = $emailTemplate;
        $this->_resourceConnection = $resourceConnection;
    }

    public function execute()
    {
        //Cron job - get sales_order_payment information by below query.
        $connection = $this->_resourceConnection->getConnection();
        $tableName = $this->_resourceConnection->getTableName('sales_order_payment');
        $attribute_information = "SELECT sales_order.increment_id, sales_order_payment.additional_information 
            FROM sales_order LEFT JOIN sales_order_payment ON sales_order.entity_id = sales_order_payment.parent_id 
            WHERE sales_order_payment.method = 'fixpayment' AND sales_order.updated_at >= NOW() - INTERVAL 30 MINUTE";

        $results = $connection->fetchAll($attribute_information);


// Fetch sql query result

        foreach ($results as $result) {
            $increment_id = $result['increment_id'];
            $additional_information = $result['additional_information'];
            $additional_information_arr = json_decode($additional_information);
            $address_verification_json = $additional_information_arr->shipping_address_verification;
            $address_verification_arr = json_decode($address_verification_json);
            $postal_code = $address_verification_arr->postal_code;

            //Grab Postal Code, if score lower than 1, email to customer support

            if ($postal_code < 1) {


                try {
                    $templateVars = [
                        'order_number' => $increment_id,
                        'postal_code_score' => $postal_code,
                        'fixOrderId' => $additional_information_arr->fixOrderId
                    ];
                    $emails = ['receiver@gmail.com'];
                    $email_template = $this->emailTemplate->load('fix_temp', 'template_code');
                    $email_template_id = $email_template->getTemplateId();
                    $transport = $this->transportBuilder->setTemplateIdentifier($email_template_id)
                        ->setTemplateOptions(['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => 1])
                        ->setTemplateVars($templateVars)
                        ->setFrom(["name" => "Magento Fix Order", "email" => "sender@gmail.com"])
                        ->addTo($emails)
                        ->setReplyTo('sender@gmail.com')
                        ->getTransport();
                    $transport->sendMessage();
                } catch (Exception $e) {
                    $this->logger->error($e);
                }

            }

        }

    }

}

Change your code to this and then run di:compile command

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.