Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions woocommerce/Emails/Traits/HasEmailPreviewValuesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

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

use Automattic\WooCommerce\Internal\Admin\EmailPreview\EmailPreview;
use WC_Product;

/**
* Adds improved support for the experimental "Email Improvements" feature, by populating any missing properties
* with default/placeholder values.
Expand All @@ -14,4 +17,41 @@ trait HasEmailPreviewValuesTrait
* Sets "Preview" values on the email object.
*/
abstract public function setPreviewValues() : void;

/**
* Gets a random, real product from the site.
*
* For use in rendering examples for email templates.
* @return WC_Product|null
*/
protected function getRandomProduct() : ?WC_Product
{
$products = array_values(wc_get_products([
'post_type' => 'product',
'posts_per_page' => 1,
'orderby' => 'rand',
]));

if (isset($products[0]) && $products[0] instanceof WC_Product) {
return $products[0];
}

return null;
}

/**
* Gets a dummy (fake) product.
*
* For use in rendering examples for email templates.
* @return WC_Product
*/
protected function getDummyProduct() : WC_Product
{
if (class_exists(EmailPreview::class) && method_exists(EmailPreview::class, 'get_dummy_product_when_not_set')) {
return EmailPreview::instance()->get_dummy_product_when_not_set(null);
} else {
// we should really never end up here!
return new WC_Product();
}
}
}
3 changes: 2 additions & 1 deletion woocommerce/changelog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*** SkyVerge WooCommerce Plugin Framework Changelog ***

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

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