Jump to content

refund using CQRS design pattern


Recommended Posts

Hi all,

I'm trying to make a refund base on the docs provided for prestashop 1.7.7. 

$command = new IssuePartialRefundCommand (
      $orderId,
      $refund_information['refund_items'],
      $refund_information['shipping_amount'],
      (bool)$refund_information['restock'],
      (bool)$refund_information['credit_slip'],
      (bool)$refund_information['voucher'],
      (int)$refund_information['voucher_refund_type'], 
);

$commandBus = $this->get('prestashop.core.command_bus');
$commandBus->handle($command);

But I does not work, it seems that $commandBus is null. Is there anything more I have to configure to load the command_bus ?

 

Edited by mr_david (see edit history)
Link to comment
Share on other sites

  • mr_david changed the title to refund using CQRS design pattern
  • 3 weeks later...

Where did you call it ?

Because there are 3 service containers on PrestaShop :

  1. BO Symfony (with CommandBus) only available on Symfony based controllers
  2. BO Legacy (no CommandBus) available on all old controllers (module configuration page & ModuleAdminController)
  3. FO (no CommandBus) (FrontController or ModuleFrontController)

So you can only use it in a Symfony controller

Link to comment
Share on other sites

In my case my file is in the directory of my module (controllers/admin/refunds.php), and that file is like this

<?php

include('../../../../config/config.inc.php');
include('../../../../init.php');

use PrestaShop\PrestaShop\Core\CommandBus\CommandBusInterface;
use PrestaShop\PrestaShop\Core\Domain\Order\Command\IssueStandardRefundCommand;

class Refunds extends Module
{
  public function createRefundTotal()
  {
    $order         = new Order($_GET['order_id']);
    $order_details = OrderDetail::getList((int)$_GET['order_id']);

    $refunds            = [];
    $refundShippingCost = ($_POST['shipping'] == 'true') ? true : false;
    $orderId            = $_GET['order_id'];
    $generateCreditSlip = true;
    $generateVoucher    = true;
    $voucherRefundType  = '1';

    for ($i = 0; $i < count($order_details); $i++)
    {
      $quantity = $order_details[$i]['product_quantity'] - $order_details[$i]['product_quantity_refunded'];

      if($quantity > 0)
      {
        $refunds[(int)$order_details[$i]['id_order_detail']] =
        [
          'quantity' => (int)$quantity
        ];
      }
    }

    $command = new IssueStandardRefundCommand(
      $orderId,
      $refunds,
      $refundShippingCost,
      $generateCreditSlip,
      $generateVoucher,
      $voucherRefundType
    );

    $this->commandBus->handle($command);
  }
}

This way it doesn't work and it gives me the following error

Notice: Undefined property: Refunds::$commandBus

Obviously the error is because the commandBus does not exist, but how can I use it ?, or if there is another way to generate the return of the order

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...