Last active
January 22, 2024 16:24
-
-
Save inxilpro/0e150aaa7c740016bad0fe8c94fa6e17 to your computer and use it in GitHub Desktop.
View hooks demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Promotions | |
{ | |
public static function for(User $user) | |
{ | |
return new static($user); | |
} | |
public function __construct(protected User $user) | |
{ | |
} | |
public function registerHooks() | |
{ | |
if ($this->inCyberMondayWindow()) { | |
View::hook('mail.receipt', 'promos', function() { | |
return new HtmlString(Blade::render(<<<'BLADE' | |
<x-mail-banner> | |
Don't miss out on our <a href="#">Cyber Monday deals!</a> | |
</x-mail-banner> | |
BLADE | |
)); | |
}); | |
} | |
} | |
protected function inCyberMondayWindow() | |
{ | |
$start = Date::parse('fourth monday of november')->toImmutable(); | |
$end = $start->addWeek(); | |
return now()->between($start, $end); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<x-email> | |
<x-hook name="promos" /> | |
<p>Thank you for your purchase!</p> | |
... | |
</x-email> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ReceiptMail extends Mailable | |
{ | |
public function __construct( | |
public User $customer, | |
public Collection $items | |
) { | |
} | |
public function build() | |
{ | |
Promotions::for($this->customer)->registerHooks(); | |
} | |
public function content(): Content | |
{ | |
return new Content(view: 'mail.receipt'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment