Skip to content

Commit

Permalink
Provide an option to disable the emailing of receipts
Browse files Browse the repository at this point in the history
Update Order.php
- add new private static send_receipt
- update the statusTransition method to allow for the disabling of receipt emails when orders are paid
Update docs & example config
Update OrderProcessor.php for missing config variables
  • Loading branch information
AntonyThorpe committed May 16, 2024
1 parent 93c3fd3 commit 35e1884
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
7 changes: 5 additions & 2 deletions docs/en/02_Customisation/Emails.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Shop emails can be customised to suit your project needs.
There are a few yaml config options that will affect which emails are sent:

```yaml
SilverShop\Model\Order:
send_receipt: false # to disable the emailing of paid invoices (default is true)

SilverShop\Checkout\OrderProcessor:
#send order confirmation when order is placed, but unpaid
send_confirmation: true
Expand All @@ -13,6 +16,7 @@ SilverShop\Checkout\OrderProcessor:
SilverShop\Checkout\OrderEmailNotifier:
bcc_confirmation_to_admin: true
bcc_receipt_to_admin: true
bcc_status_change_to_admin: true

#Specify the 'from' address to use in email correspondence
SilverShop\Extension\ShopConfigExtension:
Expand All @@ -35,7 +39,7 @@ en:
ShopEmail:
ConfirmationSubject: 'My Website Order #{OrderNo} confirmation'
ReceiptSubject: 'My Website Order #{OrderNo} receipt'
CancelSubject: 'My Website Order #{OrderNo} cancelled by member'
CancelSubject: 'My Website Order #{OrderNo} cancelled by member'
```
## Making your own Notifier
Expand All @@ -48,4 +52,3 @@ Injector:
OrderEmailNotifier:
class: MyCustomOrderEmailNotifier
```

1 change: 1 addition & 0 deletions example_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ SilverShop\Model\Order:
modifiers:
- 'SilverShop\Model\Modifiers\Shipping\Simple'
- 'SilverShop\Model\Modifiers\Tax\FlatTax'
# send_receipt: false # send paid invoice to customer (default is true)
cancel_before_payment: false
cancel_before_processing: false
cancel_before_sending: false
Expand Down
10 changes: 10 additions & 0 deletions src/Checkout/OrderProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ class OrderProcessor
use Injectable;
use Configurable;

/**
* @config
*/
private static bool $send_admin_notification = false;

/**
* @config
*/
private static bool $send_confirmation = false;

/**
* @var Order
*/
Expand Down
8 changes: 7 additions & 1 deletion src/Model/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ class Order extends DataObject
*/
private static $cancel_before_payment = true;

/**
* Email customer an invoice upon payment
* @config
*/
private static bool $send_receipt = true;

/**
* Whether or not an order can be cancelled before processing
*
Expand Down Expand Up @@ -845,7 +851,7 @@ protected function statusTransition($fromStatus, $toStatus)
//all payment is settled
$this->extend('onPaid');

if (!$this->ReceiptSent) {
if (!$this->ReceiptSent && static::config()->get('send_receipt')) {
OrderEmailNotifier::create($this)->sendReceipt();
$this->setField('ReceiptSent', DBDatetime::now()->Rfc2822());
}
Expand Down
13 changes: 13 additions & 0 deletions tests/php/Checkout/OrderEmailNotifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ public function testReceipt()
$this->assertEmailSent('[email protected]', '[email protected]');
}

public function testReceiptNoEmailSent()
{
$this->clearEmails();
Config::modify()->set(Order::class, 'send_receipt', false);
$order = $this->objFromFixture(Order::class, 'unpaid');
$order->setField('Status', 'Paid');
$order->write();
$this->assertNull(
$this->findEmail('[email protected]', '[email protected]'),
'An email is not sent when the Order class send_receipt is set to false'
);
}

public function testStatusUpdate()
{
$this->notifier->sendStatusChange('test subject');
Expand Down

0 comments on commit 35e1884

Please sign in to comment.