Product Image: Displays with different size on the frontend (https://github.com/woocommerce/woocommerce-blocks/pull/8926)

* Add block minimal structure

* Add Single Product block icon

* Add usesContext to block.json

* Add Save function to Single Product Block registration settings

* Add InnerBlocks template to Editor for Single Product Block

* Add Product selector to Single Product block

* Add withProduct HOC to the block editor

* Add save function to the Single Product block

* Add renderOnServerSide attribute to Product image

This attribute is used to determine if the product image should be rendered on the server side or not. This is needed because the product image is not rendered on the server side by default, but we want to render it on the server side when it is inside the Single Product block.

* Add renderOnServerSide attribute to Product Category List block

* Add renderOnServerSide attribute to Product Price block

* Add renderOnServerSide attribute to Product Rating block

* Add renderOnServerSide attribute to Product Stock Indicator block

* Add renderOnServerSide attribute to Product Summary block

* Add renderOnServerSide attribute to Product Tag List block

* Add renderOnServerSide attribute to Product Title block

* Add renderOnServerSide attribute to Product Add to Cart, Sales Badge and Sku block

* Reuse editor code for Single Product block

* Add Props interface to Single Product Server Side Editor

* Add render_block_context hook to SingleProductServerSide block

* Fix Single Product Server Side block that was not appearing in the block inserter

* Fix bug where the correct product was not being displayed on the frontend

* Remove Single Product Block

* Add Product title and product summary variations

* Add Add To Cart Form and Product Meta blocks to the Single Product Block

* remove more files that belong the Single Product block

* fix php linter error

* wrap up Single Product Block

* Fix issue preventing the block from working on the Single Product Template

* Fix update context function for Single Product block

* Replace Single Product block title

* Replace global $post only for Single Product inner blocks

* Remove unnecessary var_dump statement

* Improve documentation for the Single Product block

* fix registration: add-to-cart-form and product-meta blocks

* Improve documentation for Single Product block

* Fix lint error related to imports

* Remove query monitor debug code

* Remove unnecessary `renderOnServerSide` attribute from product elements

* Fix return type for the Save function of the Product Elements blocks

* Fix return type for the Save function of the Product Title block

* Add scss file for Single Product block

* Fix PHP coding standards

* Fix php coding standards

* Remove internal dependencies section duplication

* Replace .wc-block with .wc-block-editor- styles

According to the naming convention for this project (https://github.com/woocommerce/woocommerce-blocks/blob/trunk/docs/contributors/contributing/coding-guidelines.md#naming) we should be using the `.wc-block-editor-` prefix for styles that are only used in the editor.

* Solve style for the Edit card component in the Single Product block

* Fix error preventing Block Control from being displayed

* Make the Product title to be a link by default

* Fix PHP Coding Standards

* Fix PHP Code Standards

* Fix php cs issues

* Fix issue with php cs

* Remove unnecessary comment

* Remove unused Icon import

* Remove unnecessary dot from the className attributes in the Single Product Block

* Replace metadata.name with metadata.title in the Editor file

* Ignore phpcs rule

* Add new attributes to the Attributes interface of the Price block

* Fix typescript errors on the Product Meta block

* Fix ts errors on Single Product editor

* Fix ts errors on layout-editor.tsx

* Fix ts errors in Single Product index file

* Fix ts errors on add to cart form index file

* Fix unsupported block error when using inner blocks outside the Single Product template

* Fix unsupported block error for the Single Product block when editing Posts/Pages

* Remove unnecessary JSDoc types

* Remove unnecessary APIError component

* Replace global $post only when rendering a `core/post-excerpt` block

* Fix php cs errors

* Put the Single Product block behind an experimental flag

* Fix error occurring when trying to access ancestor of an undefined block settings

* Fix Product Image appearing with different size on the Frontend

* Fix PHP Code Standards errors

* Remove JS Doc from shared-product-control.tsx

* Fix single-product-frontend.js 404 error

* Fix error 'Block names must be string' when adding Single Product block

* Add todo comment to state the temporary solution used in the Single Product block

* Set imageSizing attribute on Product Query and Single Product blocks

* Rename values for image size options in the product image block

The previous allowed values were 'cropped' and 'full-size'. The new values are 'thumbnail' and 'single' which are similar to the values used in WooCoomerce.

WooCommerce uses 'woocommerce_thumbnail', 'woocommerce_gallery_thumbnail' and 'woocommerce_single' for images sizes. The 'woocommerce_single' size is used for the Single Product page and the 'woocommerce_thumbnail' size is used for the product grid.

* Fix Related Products image to show as a thumbnail instead of the full size

* Fix PHP CS error

---------

Co-authored-by: Luigi <gigitux@gmail.com>
This commit is contained in:
Alexandre Lara 2023-04-11 11:41:34 -03:00 committed by GitHub
parent df18b279e6
commit 719fabdc57
11 changed files with 41 additions and 28 deletions

View File

@ -3,6 +3,11 @@
*/
import type { BlockAttributes } from '@wordpress/blocks';
/**
* Internal dependencies
*/
import { ImageSizing } from './types';
export const blockAttributes: BlockAttributes = {
showProductLink: {
type: 'boolean',
@ -18,7 +23,7 @@ export const blockAttributes: BlockAttributes = {
},
imageSizing: {
type: 'string',
default: 'full-size',
default: ImageSizing.SINGLE,
},
productId: {
type: 'number',

View File

@ -23,7 +23,7 @@ import type { HTMLAttributes } from 'react';
*/
import ProductSaleBadge from '../sale-badge/block';
import './style.scss';
import type { BlockAttributes } from './types';
import { BlockAttributes, ImageSizing } from './types';
const ImagePlaceholder = (): JSX.Element => {
return (
@ -81,7 +81,7 @@ type Props = BlockAttributes & HTMLAttributes< HTMLDivElement >;
export const Block = ( props: Props ): JSX.Element | null => {
const {
className,
imageSizing = 'full-size',
imageSizing = ImageSizing.SINGLE,
showProductLink = true,
showSaleBadge,
saleBadgeAlign = 'right',
@ -160,7 +160,7 @@ export const Block = ( props: Props ): JSX.Element | null => {
fallbackAlt={ product.name }
image={ image }
loaded={ ! isLoading }
showFullSize={ imageSizing !== 'cropped' }
showFullSize={ imageSizing !== ImageSizing.THUMBNAIL }
/>
</ParentComponent>
</div>

View File

@ -32,10 +32,9 @@ import {
BLOCK_ICON as icon,
BLOCK_DESCRIPTION as description,
} from './constants';
import type { BlockAttributes } from './types';
import { BlockAttributes, ImageSizing } from './types';
type SaleBadgeAlignProps = 'left' | 'center' | 'right';
type ImageSizingProps = 'full-size' | 'cropped';
const Edit = ( {
attributes,
@ -57,12 +56,6 @@ const Edit = ( {
[ setAttributes, isDescendentOfQueryLoop ]
);
useEffect( () => {
if ( isBlockThemeEnabled && attributes.imageSizing !== 'full-size' ) {
setAttributes( { imageSizing: 'full-size' } );
}
}, [ attributes.imageSizing, isBlockThemeEnabled, setAttributes ] );
return (
<div { ...blockProps }>
<InspectorControls>
@ -160,19 +153,19 @@ const Edit = ( {
}
) }
value={ imageSizing }
onChange={ ( value: ImageSizingProps ) =>
onChange={ ( value: ImageSizing ) =>
setAttributes( { imageSizing: value } )
}
>
<ToggleGroupControlOption
value="full-size"
value={ ImageSizing.SINGLE }
label={ __(
'Full Size',
'woo-gutenberg-products-block'
) }
/>
<ToggleGroupControlOption
value="cropped"
value={ ImageSizing.THUMBNAIL }
label={ __(
'Cropped',
'woo-gutenberg-products-block'

View File

@ -9,6 +9,7 @@ import { ProductResponseItem } from '@woocommerce/types';
* Internal dependencies
*/
import { Block } from '../block';
import { ImageSizing } from '../types';
jest.mock( '@woocommerce/base-hooks', () => ( {
__esModule: true,
@ -152,7 +153,7 @@ describe( 'Product Image Block', () => {
productId={ productWithImages.id }
showSaleBadge={ false }
saleBadgeAlign={ 'left' }
imageSizing={ 'full-size' }
imageSizing={ ImageSizing.SINGLE }
isDescendentOfQueryLoop={ false }
/>
</ProductDataContextProvider>
@ -186,7 +187,7 @@ describe( 'Product Image Block', () => {
productId={ productWithoutImages.id }
showSaleBadge={ false }
saleBadgeAlign={ 'left' }
imageSizing={ 'full-size' }
imageSizing={ ImageSizing.SINGLE }
isDescendentOfQueryLoop={ false }
/>
</ProductDataContextProvider>
@ -219,7 +220,7 @@ describe( 'Product Image Block', () => {
productId={ productWithImages.id }
showSaleBadge={ false }
saleBadgeAlign={ 'left' }
imageSizing={ 'full-size' }
imageSizing={ ImageSizing.SINGLE }
isDescendentOfQueryLoop={ false }
/>
</ProductDataContextProvider>
@ -249,7 +250,7 @@ describe( 'Product Image Block', () => {
productId={ productWithoutImages.id }
showSaleBadge={ false }
saleBadgeAlign={ 'left' }
imageSizing={ 'full-size' }
imageSizing={ ImageSizing.SINGLE }
isDescendentOfQueryLoop={ false }
/>
</ProductDataContextProvider>
@ -277,7 +278,7 @@ describe( 'Product Image Block', () => {
productId={ productWithoutImages.id }
showSaleBadge={ false }
saleBadgeAlign={ 'left' }
imageSizing={ 'full-size' }
imageSizing={ ImageSizing.SINGLE }
isDescendentOfQueryLoop={ false }
/>
</ProductDataContextProvider>

View File

@ -1,3 +1,8 @@
export enum ImageSizing {
SINGLE = 'single',
THUMBNAIL = 'thumbnail',
}
export interface BlockAttributes {
// The product ID.
productId: number;
@ -10,7 +15,7 @@ export interface BlockAttributes {
// How should the sale badge be aligned if displayed.
saleBadgeAlign: 'left' | 'center' | 'right';
// Size of image to use.
imageSizing: 'full-size' | 'cropped';
imageSizing: ImageSizing;
// Whether or not be a children of Query Loop Block.
isDescendentOfQueryLoop: boolean;
}

View File

@ -17,6 +17,7 @@ import { Block as ProductSaleBadge } from '../../../atomic/blocks/product-elemen
import { Block as ProductPrice } from '../../../atomic/blocks/product-elements/price/block';
import { Block as ProductButton } from '../../../atomic/blocks/product-elements/button/block';
import AddToCartButton from '../../../atomic/blocks/product-elements/add-to-cart/block';
import { ImageSizing } from '../../../atomic/blocks/product-elements/image/types';
interface CrossSellsProductProps {
product: ProductResponseItem;
@ -44,7 +45,7 @@ const CartCrossSellsProduct = ( {
productId={ product.id }
showProductLink={ false }
saleBadgeAlign={ 'left' }
imageSizing={ 'full-size' }
imageSizing={ ImageSizing.SINGLE }
isDescendentOfQueryLoop={ false }
/>
<ProductName

View File

@ -10,6 +10,7 @@ import type { InnerBlockTemplate } from '@wordpress/blocks';
import { QueryBlockAttributes } from './types';
import { VARIATION_NAME as PRODUCT_TITLE_ID } from './variations/elements/product-title';
import { VARIATION_NAME as PRODUCT_TEMPLATE_ID } from './variations/elements/product-template';
import { ImageSizing } from '../../atomic/blocks/product-elements/image/types';
/**
* Returns an object without a key.
@ -91,6 +92,7 @@ export const INNER_BLOCKS_TEMPLATE: InnerBlockTemplate[] = [
},
},
},
imageSizing: ImageSizing.THUMBNAIL,
},
],
[

View File

@ -59,6 +59,7 @@ export const INNER_BLOCKS_TEMPLATE: InnerBlockTemplate[] = [
'woocommerce/product-image',
{
productId: 0,
imageSizing: 'cropped',
},
],
[

View File

@ -7,12 +7,13 @@ import classnames from 'classnames';
* Internal dependencies
*/
import { BLOCK_NAME as PRODUCT_BUTTON_BLOCK_NAME } from '../../atomic/blocks/product-elements/button/constants';
import { ImageSizing } from '../../atomic/blocks/product-elements/image/types';
/**
* The default layout built from the default template.
*/
export const DEFAULT_PRODUCT_LIST_LAYOUT = [
[ 'woocommerce/product-image', { imageSizing: 'cropped' } ],
[ 'woocommerce/product-image', { imageSizing: ImageSizing.THUMBNAIL } ],
[ 'woocommerce/product-title' ],
[ 'woocommerce/product-price' ],
[ 'woocommerce/product-rating' ],

View File

@ -11,6 +11,7 @@ import type { InnerBlockTemplate } from '@wordpress/blocks';
import metadata from './block.json';
import { VARIATION_NAME as PRODUCT_TITLE_VARIATION_NAME } from '../product-query/variations/elements/product-title';
import { VARIATION_NAME as PRODUCT_SUMMARY_VARIATION_NAME } from '../product-query/variations/elements/product-summary';
import { ImageSizing } from '../../atomic/blocks/product-elements/image/types';
export const BLOCK_ICON = (
<Icon
@ -33,6 +34,7 @@ export const DEFAULT_INNER_BLOCKS: InnerBlockTemplate[] = [
{
showSaleBadge: false,
isDescendentOfSingleProductBlock: true,
imageSizing: ImageSizing.SINGLE,
},
],
],

View File

@ -75,7 +75,7 @@ class ProductImage extends AbstractBlock {
'showProductLink' => true,
'showSaleBadge' => true,
'saleBadgeAlign' => 'right',
'imageSizing' => 'full-size',
'imageSizing' => 'single',
'productId' => 'number',
'isDescendentOfQueryLoop' => 'false',
);
@ -141,16 +141,18 @@ class ProductImage extends AbstractBlock {
* Render Image.
*
* @param \WC_Product $product Product object.
* @param array $attributes Parsed attributes.
* @return string
*/
private function render_image( $product ) {
$image_info = wp_get_attachment_image_src( get_post_thumbnail_id( $product->get_id() ), 'woocommerce_thumbnail' );
private function render_image( $product, $attributes ) {
$image_type = 'single' == $attributes['imageSizing'] ? 'woocommerce_single' : 'woocommerce_thumbnail';
$image_info = wp_get_attachment_image_src( get_post_thumbnail_id( $product->get_id() ), $image_type );
if ( ! isset( $image_info[0] ) ) {
// The alt text is left empty on purpose, as it's considered a decorative image.
// More can be found here: https://www.w3.org/WAI/tutorials/images/decorative/.
// Github discussion for a context: https://github.com/woocommerce/woocommerce-blocks/pull/7651#discussion_r1019560494.
return sprintf( '<img src="%s" alt="" />', wc_placeholder_img_src( 'woocommerce_thumbnail' ) );
return sprintf( '<img src="%s" alt="" />', wc_placeholder_img_src( $image_type ) );
}
return sprintf(
@ -205,7 +207,7 @@ class ProductImage extends AbstractBlock {
$this->render_anchor(
$product,
$this->render_on_sale_badge( $product, $parsed_attributes ),
$this->render_image( $product ),
$this->render_image( $product, $parsed_attributes ),
$parsed_attributes
)
);