fix: order confirmation email shows wrong payment method and incomplete address
The email template had "Carte de crédit" hardcoded regardless of the
actual payment method. The address blocks were also missing the
``address2`` and ``name`` fields.
- Add ``mode_paiement``, ``livraison_nom``, ``facturation_nom``,
``livraison_adresse2``, ``facturation_adresse2`` to
``ConfirmationCommande`` Mailable
- Migration to replace hardcoded payment label with
``{{mode_paiement}}`` and add ``address2`` fields in DB template
- Migration to add ``name`` fields before each address block
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Mail;
|
||||
|
||||
use App\Models\Core\Mail\MailTemplate;
|
||||
use App\Repositories\Shop\Orders;
|
||||
use App\Repositories\Shop\Traits\MailCustomers;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
@@ -32,29 +33,44 @@ class ConfirmationCommande extends TemplateMailable
|
||||
|
||||
public $facturation_ville;
|
||||
|
||||
public $livraison_nom;
|
||||
|
||||
public $livraison_adresse;
|
||||
|
||||
public $livraison_adresse2;
|
||||
|
||||
public $livraison_cp;
|
||||
|
||||
public $livraison_ville;
|
||||
|
||||
public $facturation_nom;
|
||||
|
||||
public $facturation_adresse2;
|
||||
|
||||
public $mode_paiement;
|
||||
|
||||
protected static $templateModelClass = MailTemplate::class;
|
||||
|
||||
public function __construct($order)
|
||||
{
|
||||
$facturation_address = $order->invoice->address;
|
||||
$facturation_address = $order->invoice->address;
|
||||
$delivery_address = $order->delivery_address;
|
||||
$this->prenom = $order->customer->first_name;
|
||||
$this->nom = $order->customer->last_name;
|
||||
$this->facturation_nom = $facturation_address->name;
|
||||
$this->facturation_adresse = $facturation_address->address;
|
||||
$this->facturation_adresse2 = $facturation_address->address2;
|
||||
$this->facturation_cp = $facturation_address->zipcode;
|
||||
$this->facturation_ville = $facturation_address->city;
|
||||
$this->livraison_nom = $delivery_address->name;
|
||||
$this->livraison_adresse = $delivery_address->address;
|
||||
$this->livraison_adresse2 = $delivery_address->address2;
|
||||
$this->livraison_cp = $delivery_address->zipcode;
|
||||
$this->livraison_ville = $delivery_address->city;
|
||||
$this->societe = $order->customer->company;
|
||||
$this->email = $order->customer->email;
|
||||
$this->numero_commande = $order->ref;
|
||||
$this->date_commande = $order->created_at;
|
||||
$this->mode_paiement = Orders::getPaymentType($order->payment_type);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
$this->transformTemplate(function ($html) {
|
||||
// Replace hardcoded "Carte de crédit" with the template variable
|
||||
$html = str_replace(
|
||||
'Carte de crédit',
|
||||
'{{mode_paiement}}',
|
||||
$html
|
||||
);
|
||||
|
||||
// Add address2 to delivery address
|
||||
$html = str_replace(
|
||||
'{{livraison_adresse}}<br />{{livraison_cp}} {{livraison_ville}}',
|
||||
'{{livraison_adresse}}{{#livraison_adresse2}}<br />{{livraison_adresse2}}{{/livraison_adresse2}}<br />{{livraison_cp}} {{livraison_ville}}',
|
||||
$html
|
||||
);
|
||||
|
||||
// Add address2 to billing address
|
||||
$html = str_replace(
|
||||
'{{facturation_adresse}}<br />{{facturation_cp}} {{facturation_ville}}',
|
||||
'{{facturation_adresse}}{{#facturation_adresse2}}<br />{{facturation_adresse2}}{{/facturation_adresse2}}<br />{{facturation_cp}} {{facturation_ville}}',
|
||||
$html
|
||||
);
|
||||
|
||||
return $html;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
$this->transformTemplate(function ($html) {
|
||||
$html = str_replace(
|
||||
'{{mode_paiement}}',
|
||||
'Carte de crédit',
|
||||
$html
|
||||
);
|
||||
|
||||
$html = str_replace(
|
||||
'{{livraison_adresse}}{{#livraison_adresse2}}<br />{{livraison_adresse2}}{{/livraison_adresse2}}<br />{{livraison_cp}} {{livraison_ville}}',
|
||||
'{{livraison_adresse}}<br />{{livraison_cp}} {{livraison_ville}}',
|
||||
$html
|
||||
);
|
||||
|
||||
$html = str_replace(
|
||||
'{{facturation_adresse}}{{#facturation_adresse2}}<br />{{facturation_adresse2}}{{/facturation_adresse2}}<br />{{facturation_cp}} {{facturation_ville}}',
|
||||
'{{facturation_adresse}}<br />{{facturation_cp}} {{facturation_ville}}',
|
||||
$html
|
||||
);
|
||||
|
||||
return $html;
|
||||
});
|
||||
}
|
||||
|
||||
private function transformTemplate(callable $transform): void
|
||||
{
|
||||
$template = DB::table('mail_templates')
|
||||
->where('mailable', 'App\\Mail\\ConfirmationCommande')
|
||||
->first();
|
||||
|
||||
if (! $template) {
|
||||
return;
|
||||
}
|
||||
|
||||
$translations = json_decode($template->html_template, true);
|
||||
|
||||
foreach ($translations as $lang => $html) {
|
||||
$translations[$lang] = $transform($html);
|
||||
}
|
||||
|
||||
DB::table('mail_templates')
|
||||
->where('id', $template->id)
|
||||
->update(['html_template' => json_encode($translations, JSON_UNESCAPED_UNICODE)]);
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
$this->transformTemplate(function ($html) {
|
||||
// Add name before delivery address
|
||||
$html = str_replace(
|
||||
'{{livraison_adresse}}',
|
||||
'{{#livraison_nom}}{{livraison_nom}}<br />{{/livraison_nom}}{{livraison_adresse}}',
|
||||
$html
|
||||
);
|
||||
|
||||
// Add name before billing address
|
||||
$html = str_replace(
|
||||
'{{facturation_adresse}}',
|
||||
'{{#facturation_nom}}{{facturation_nom}}<br />{{/facturation_nom}}{{facturation_adresse}}',
|
||||
$html
|
||||
);
|
||||
|
||||
return $html;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
$this->transformTemplate(function ($html) {
|
||||
$html = str_replace(
|
||||
'{{#livraison_nom}}{{livraison_nom}}<br />{{/livraison_nom}}{{livraison_adresse}}',
|
||||
'{{livraison_adresse}}',
|
||||
$html
|
||||
);
|
||||
|
||||
$html = str_replace(
|
||||
'{{#facturation_nom}}{{facturation_nom}}<br />{{/facturation_nom}}{{facturation_adresse}}',
|
||||
'{{facturation_adresse}}',
|
||||
$html
|
||||
);
|
||||
|
||||
return $html;
|
||||
});
|
||||
}
|
||||
|
||||
private function transformTemplate(callable $transform): void
|
||||
{
|
||||
$template = DB::table('mail_templates')
|
||||
->where('mailable', 'App\\Mail\\ConfirmationCommande')
|
||||
->first();
|
||||
|
||||
if (! $template) {
|
||||
return;
|
||||
}
|
||||
|
||||
$translations = json_decode($template->html_template, true);
|
||||
|
||||
foreach ($translations as $lang => $html) {
|
||||
$translations[$lang] = $transform($html);
|
||||
}
|
||||
|
||||
DB::table('mail_templates')
|
||||
->where('id', $template->id)
|
||||
->update(['html_template' => json_encode($translations, JSON_UNESCAPED_UNICODE)]);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user