Skip to content

Commit 275466c

Browse files
Add additional helper methods to the trait HasEmailPreviewValuesTrait (#753)
* Add additional helper methods to the trait * Update changelog * Pass in null * Minor inline docs tweaks. --------- Co-authored-by: Drew Jaynes <[email protected]>
1 parent 8a082a2 commit 275466c

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

woocommerce/Emails/Traits/HasEmailPreviewValuesTrait.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace SkyVerge\WooCommerce\PluginFramework\v5_15_6\Emails\Traits;
44

5+
use Automattic\WooCommerce\Internal\Admin\EmailPreview\EmailPreview;
6+
use WC_Product;
7+
58
/**
69
* Adds improved support for the experimental "Email Improvements" feature, by populating any missing properties
710
* with default/placeholder values.
@@ -14,4 +17,41 @@ trait HasEmailPreviewValuesTrait
1417
* Sets "Preview" values on the email object.
1518
*/
1619
abstract public function setPreviewValues() : void;
20+
21+
/**
22+
* Gets a random, real product from the site.
23+
*
24+
* For use in rendering examples for email templates.
25+
* @return WC_Product|null
26+
*/
27+
protected function getRandomProduct() : ?WC_Product
28+
{
29+
$products = array_values(wc_get_products([
30+
'post_type' => 'product',
31+
'posts_per_page' => 1,
32+
'orderby' => 'rand',
33+
]));
34+
35+
if (isset($products[0]) && $products[0] instanceof WC_Product) {
36+
return $products[0];
37+
}
38+
39+
return null;
40+
}
41+
42+
/**
43+
* Gets a dummy (fake) product.
44+
*
45+
* For use in rendering examples for email templates.
46+
* @return WC_Product
47+
*/
48+
protected function getDummyProduct() : WC_Product
49+
{
50+
if (class_exists(EmailPreview::class) && method_exists(EmailPreview::class, 'get_dummy_product_when_not_set')) {
51+
return EmailPreview::instance()->get_dummy_product_when_not_set(null);
52+
} else {
53+
// we should really never end up here!
54+
return new WC_Product();
55+
}
56+
}
1757
}

woocommerce/changelog.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*** SkyVerge WooCommerce Plugin Framework Changelog ***
22

3-
2025.nn.nn - version 5.15.6
3+
2025.nn.nn - version 5.15.6-dev.1
4+
* New: Added additional helper methods to the trait `HasEmailPreviewValuesTrait`
45

56
2025.03.05 - version 5.15.5
67
* New: Add a trait to assist in compatibility with new WooCommerce email preview feature

0 commit comments

Comments
 (0)