Customer Locale Added For All Mail

This commit is contained in:
Devansh 2020-08-06 16:31:05 +05:30
parent 4b80b19171
commit c6646677f2
2 changed files with 55 additions and 50 deletions

View File

@ -10,29 +10,11 @@ use Webkul\Admin\Mail\NewInvoiceNotification;
use Webkul\Admin\Mail\CancelOrderNotification;
use Webkul\Admin\Mail\NewShipmentNotification;
use Webkul\Admin\Mail\OrderCommentNotification;
use Webkul\Sales\Repositories\InvoiceRepository;
use Webkul\Admin\Mail\CancelOrderAdminNotification;
use Webkul\Admin\Mail\NewInventorySourceNotification;
class Order
{
/**
* InvoiceRepository object
*
* @var \Webkul\Sales\Repositories\InvoiceRepository
*/
protected $invoiceRepository;
/**
* Create a new repository instance using reflection api.
*
* @param \Webkul\Sales\Repositories\InvoiceRepository
*/
public function __construct(InvoiceRepository $invoiceRepository)
{
$this->invoiceRepository = $invoiceRepository;
}
/**
* Send new order Mail to the customer and admin
*
@ -41,17 +23,19 @@ class Order
*/
public function sendNewOrderMail($order)
{
$customerLocale = $this->getLocale($order);
try {
/* email to customer */
$configKey = 'emails.general.notifications.emails.general.notifications.new-order';
if (core()->getConfigData($configKey))
Mail::queue(new NewOrderNotification($order));
if (core()->getConfigData($configKey)) {
$this->prepareMail($customerLocale, new NewOrderNotification($order));
}
/* email to admin */
$configKey = 'emails.general.notifications.emails.general.notifications.new-admin';
if (core()->getConfigData($configKey)) {
app()->setLocale(env('APP_LOCALE'));
Mail::queue(new NewAdminNotification($order));
$this->prepareMail(env('APP_LOCALE'), new NewAdminNotification($order));
}
} catch (\Exception $e) {
report($e);
@ -66,18 +50,17 @@ class Order
*/
public function sendNewInvoiceMail($invoice)
{
$invoiceLocale = $this->invoiceRepository->getLocaleOfTheInvoice($invoice);
$customerLocale = $this->getLocale($invoice);
try {
if ($invoice->email_sent) {
return;
}
/* email to customer */
$configKey = 'emails.general.notifications.emails.general.notifications.new-invoice';
if (core()->getConfigData($configKey)) {
app()->setLocale($invoiceLocale);
Mail::queue(new NewInvoiceNotification($invoice));
$this->prepareMail($customerLocale, new NewInvoiceNotification($invoice));
}
} catch (\Exception $e) {
report($e);
@ -92,11 +75,13 @@ class Order
*/
public function sendNewRefundMail($refund)
{
try {
$configKey = 'emails.general.notifications.emails.general.notifications.new-refund';
$customerLocale = $this->getLocale($refund);
try {
/* email to customer */
$configKey = 'emails.general.notifications.emails.general.notifications.new-refund';
if (core()->getConfigData($configKey)) {
Mail::queue(new NewRefundNotification($refund));
$this->prepareMail($customerLocale, new NewRefundNotification($refund));
}
} catch (\Exception $e) {
report($e);
@ -111,21 +96,23 @@ class Order
*/
public function sendNewShipmentMail($shipment)
{
$customerLocale = $this->getLocale($shipment);
try {
if ($shipment->email_sent) {
return;
}
/* email to customer */
$configKey = 'emails.general.notifications.emails.general.notifications.new-shipment';
if (core()->getConfigData($configKey)) {
Mail::queue(new NewShipmentNotification($shipment));
$this->prepareMail($customerLocale, new NewShipmentNotification($shipment));
}
/* email to admin */
$configKey = 'emails.general.notifications.emails.general.notifications.new-inventory-source';
if (core()->getConfigData($configKey)) {
Mail::queue(new NewInventorySourceNotification($shipment));
$this->prepareMail(env('APP_LOCALE'), new NewInventorySourceNotification($shipment));
}
} catch (\Exception $e) {
report($e);
@ -138,18 +125,19 @@ class Order
*/
public function sendCancelOrderMail($order)
{
$customerLocale = $this->getLocale($order);
try {
/* email to customer */
$configKey = 'emails.general.notifications.emails.general.notifications.cancel-order';
if (core()->getConfigData($configKey)) {
Mail::queue(new CancelOrderNotification($order));
$this->prepareMail($customerLocale, new CancelOrderNotification($order));
}
/* email to admin */
$configKey = 'emails.general.notifications.emails.general.notifications.new-admin';
if (core()->getConfigData($configKey)) {
app()->setLocale(env('APP_LOCALE'));
Mail::queue(new CancelOrderAdminNotification($order));
$this->prepareMail(env('APP_LOCALE'), new CancelOrderAdminNotification($order));
}
} catch (\Exception $e) {
report($e);
@ -162,14 +150,44 @@ class Order
*/
public function sendOrderCommentMail($comment)
{
$customerLocale = $this->getLocale($comment);
if (! $comment->customer_notified) {
return;
}
try {
Mail::queue(new OrderCommentNotification($comment));
/* email to customer */
$this->prepareMail($customerLocale, new OrderCommentNotification($comment));
} catch (\Exception $e) {
report($e);
}
}
/**
* Get the locale of the customer if somehow item name changes then the english locale will pe provided.
*
* @param object \Webkul\Sales\Contracts\Order|\Webkul\Sales\Contracts\Invoice|\Webkul\Sales\Contracts\Refund|\Webkul\Sales\Contracts\Shipment|\Webkul\Sales\Contracts\OrderComment
* @return string
*/
private function getLocale($object)
{
if ($object instanceof \Webkul\Sales\Contracts\OrderComment) {
$object = $object->order;
}
$objectFirstItem = $object->items->first();
$objectFlatProduct = $objectFirstItem->product->product_flats->where('name', $objectFirstItem->name)->first();
return $objectFlatProduct ? $objectFlatProduct->locale : 'en';
}
/**
* Prepare Mail.
* @return void
*/
private function prepareMail($locale, $notification)
{
app()->setLocale($locale);
Mail::queue($notification);
}
}

View File

@ -256,17 +256,4 @@ class InvoiceRepository extends Repository
return $invoice;
}
/**
* Get the locale of the invoice if somehow item name changes then the english locale will pe provided.
*
* @param \Webkul\Sales\Contracts\Invoice $invoice
* @return string
*/
public function getLocaleOfTheInvoice($invoice)
{
$invoiceFirstItem = $invoice->items->first();
$invoiceFlatProduct = $invoiceFirstItem->product->product_flats->where('name', $invoiceFirstItem->name)->first();
return $invoiceFlatProduct ? $invoiceFlatProduct->locale : 'en';
}
}