Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Blockified Single Product Template: add product-classes via wc_get_product_class #9697

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions src/BlockTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,13 @@ function( $template ) {

if ( str_contains( $template->slug, 'single-product' ) ) {
if ( ! is_admin() && ! BlockTemplateUtils::template_has_legacy_template_block( $template ) ) {
// Add the product class to the body. We should move this move this to a more appropriate place.
gigitux marked this conversation as resolved.
Show resolved Hide resolved
add_filter(
'body_class',
function( $classes ) {
return array_merge( $classes, wc_get_product_class() );
}
);

$new_content = SingleProductTemplateCompatibility::add_compatibility_layer( $template->content );
$template->content = $new_content;
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e-pw/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const config: ExtendedPlaywrightTestConfig = {
use: {
baseURL: BASE_URL,
screenshot: 'only-on-failure',
stateDir: './tests/e2e-pw/test-results/storage/',
stateDir: 'tests/e2e-pw/test-results/storage/',
trace: 'retain-on-failure',
video: 'on-first-retry',
viewport: { width: 1280, height: 720 },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* External dependencies
*/
import { test, expect } from '@woocommerce/e2e-playwright-utils';

const products = [
{
product: 'Album',
// Copy-pasted by WooCommerce Core Legacy Template.
classes: [
'product',
'type-product',
'status-publish',
'instock',
'product_cat-music',
'has-post-thumbnail',
'downloadable',
'virtual',
'purchasable',
'product-type-simple',
],
frontendPage: '/product/album/',
},
{
product: 'Hoodie',
// Copy-pasted by WooCommerce Core Legacy Template.
classes: [
'product',
'type-product',
'status-publish',
'instock',
'product_cat-hoodies',
'has-post-thumbnail',
'sale',
'shipping-taxable',
'purchasable',
'product-type-variable',
],
frontendPage: '/product/hoodie/',
},
];

for ( const { classes, product, frontendPage } of products ) {
test.describe( `The Single Product page of the ${ product }`, () =>
test( 'add product specific classes to the body', async ( {
page,
} ) => {
await page.goto( frontendPage );
const body = await page.locator( 'body' );
const bodyClasses = await body.getAttribute( 'class' );

classes.forEach( ( className ) => {
expect( bodyClasses?.split( ' ' ) ).toContain( className );
} );
} )
);
}