diff --git a/includes/abstracts/abstract-wc-product.php b/includes/abstracts/abstract-wc-product.php
index 50437cb570e..504fc66d466 100644
--- a/includes/abstracts/abstract-wc-product.php
+++ b/includes/abstracts/abstract-wc-product.php
@@ -1859,7 +1859,7 @@ class WC_Product extends WC_Abstract_Legacy_Product {
}
if ( ! $image && $placeholder ) {
- $image = wc_placeholder_img( $size );
+ $image = wc_placeholder_img( $size, $attr );
}
return apply_filters( 'woocommerce_product_get_image', $image, $this, $size, $attr, $placeholder, $image );
diff --git a/includes/wc-product-functions.php b/includes/wc-product-functions.php
index b264a5f22dc..a5ac34c30b9 100644
--- a/includes/wc-product-functions.php
+++ b/includes/wc-product-functions.php
@@ -291,26 +291,37 @@ function wc_placeholder_img_src( $size = 'woocommerce_thumbnail' ) {
*
* Uses wp_get_attachment_image if using an attachment ID @since 3.6.0 to handle responsiveness.
*
- * @param string $size Image size.
+ * @param string $size Image size.
+ * @param string|array $attr Optional. Attributes for the image markup. Default empty.
* @return string
*/
-function wc_placeholder_img( $size = 'woocommerce_thumbnail' ) {
+function wc_placeholder_img( $size = 'woocommerce_thumbnail', $attr = '' ) {
$dimensions = wc_get_image_size( $size );
$placeholder_image = get_option( 'woocommerce_placeholder_image', 0 );
+ $default_attr = array(
+ 'class' => 'woocommerce-placeholder wp-post-image',
+ 'alt' => __( 'Placeholder', 'woocommerce' ),
+ );
+
+ $attr = wp_parse_args( $attr, $default_attr );
+
if ( wp_attachment_is_image( $placeholder_image ) ) {
$image_html = wp_get_attachment_image(
$placeholder_image,
$size,
false,
- array(
- 'alt' => __( 'Placeholder', 'woocommerce' ),
- 'class' => 'woocommerce-placeholder wp-post-image',
- )
+ $attr
);
} else {
$image = wc_placeholder_img_src( $size );
- $image_html = '';
+ $hwstring = image_hwstring( $dimensions['width'], $dimensions['height'] );
+ $attr = array_map( 'esc_attr', $attr );
+ $image_html = rtrim( ' $value ) {
+ $image_html .= " $name=" . '"' . $value . '"';
+ }
+ $image_html .= ' />';
}
return apply_filters( 'woocommerce_placeholder_img', $image_html, $size, $dimensions );
diff --git a/tests/unit-tests/product/data.php b/tests/unit-tests/product/data.php
index 86544dfe7b9..54c907e484b 100644
--- a/tests/unit-tests/product/data.php
+++ b/tests/unit-tests/product/data.php
@@ -324,6 +324,10 @@ class WC_Tests_Product_Data extends WC_Unit_Test_Case {
$product = new WC_Product();
$this->assertContains( wc_placeholder_img_src(), $product->get_image() );
+
+ // Test custom class attribute is honoured.
+ $image = $product->get_image( 'woocommerce_thumbnail', array( 'class' => 'custom-class' ) );
+ $this->assertContains( 'class="custom-class"', $image );
}
/**
diff --git a/tests/unit-tests/product/functions.php b/tests/unit-tests/product/functions.php
index 01ea4e0b40e..14b9ec2ddca 100644
--- a/tests/unit-tests/product/functions.php
+++ b/tests/unit-tests/product/functions.php
@@ -885,6 +885,10 @@ class WC_Tests_Product_Functions extends WC_Unit_Test_Case {
*/
public function test_wc_placeholder_img() {
$this->assertTrue( (bool) strstr( wc_placeholder_img(), wc_placeholder_img_src() ) );
+
+ // Test custom class attribute is honoured.
+ $attr = array( 'class' => 'custom-class' );
+ $this->assertContains( 'class="custom-class"', wc_placeholder_img( 'woocommerce_thumbnail', $attr ) );
}
/**