t_caches( $results );
}
return $results;
}
/**
* Get the list of classes to apply to this block.
*
* @return string space-separated list of classes.
*/
protected function get_container_classes() {
$classes = array(
'wc-block-grid',
"wp-block-{$this->block_name}",
"wc-block-{$this->block_name}",
"has-{$this->attributes['columns']}-columns",
);
if ( $this->attributes['rows'] > 1 ) {
$classes[] = 'has-multiple-rows';
}
if ( isset( $this->attributes['align'] ) ) {
$classes[] = "align{$this->attributes['align']}";
}
if ( ! empty( $this->attributes['alignButtons'] ) ) {
$classes[] = 'has-aligned-buttons';
}
if ( ! empty( $this->attributes['className'] ) ) {
$classes[] = $this->attributes['className'];
}
return implode( ' ', $classes );
}
/**
* Render a single products.
*
* @param int $id Product ID.
* @return string Rendered product output.
*/
protected function render_product( $id ) {
$product = wc_get_product( $id );
if ( ! $product ) {
return '';
}
$data = (object) array(
'permalink' => esc_url( $product->get_permalink() ),
'image' => $this->get_image_html( $product ),
'title' => $this->get_title_html( $product ),
'rating' => $this->get_rating_html( $product ),
'price' => $this->get_price_html( $product ),
'badge' => $this->get_sale_badge_html( $product ),
'button' => $this->get_button_html( $product ),
);
return apply_filters(
'woocommerce_blocks_product_grid_item_html',
"
permalink}\" class=\"wc-block-grid__product-link\">
{$data->image}
{$data->title}
{$data->badge}
{$data->price}
{$data->rating}
{$data->button}
",
$data,
$product
);
}
/**
* Get the product image.
*
* @param \WC_Product $product Product.
* @return string
*/
protected function get_image_html( $product ) {
return '' . $product->get_image( 'woocommerce_thumbnail' ) . '
';
}
/**
* Get the product title.
*
* @param \WC_Product $product Product.
* @return string
*/
protected function get_title_html( $product ) {
if ( empty( $this->attributes['contentVisibility']['title'] ) ) {
return '';
}
return '' . $product->get_title() . '
';
}
/**
* Render the rating icons.
*
* @param WC_Product $product Product.
* @return string Rendered product output.
*/
protected function get_rating_html( $product ) {
if ( empty( $this->attributes['contentVisibility']['rating'] ) ) {
return '';
}
$rating_count = $product->get_rating_count();
$review_count = $product->get_review_count();
$average = $product->get_average_rating();
if ( $rating_count > 0 ) {
return sprintf(
'%s
',
wc_get_rating_html( $average, $rating_count )
);
}
return '';
}
/**
* Get the price.
*
* @param \WC_Product $product Product.
* @return string Rendered product output.
*/
protected function get_price_html( $product ) {
if ( empty( $this->attributes['contentVisibility']['price'] ) ) {
return '';
}
return sprintf(
'%s
',
$product->get_price_html()
);
}
/**
* Get the sale badge.
*
* @param \WC_Product $product Product.
* @return string Rendered product output.
*/
protected function get_sale_badge_html( $product ) {
if ( empty( $this->attributes['contentVisibility']['price'] ) ) {
return '';
}
if ( ! $product->is_on_sale() ) {
return;
}
return '
' . esc_html__( 'Sale', 'woocommerce' ) . '
' . esc_html__( 'Product on sale', 'woocommerce' ) . '
';
}
/**
* Get the button.
*
* @param \WC_Product $product Product.
* @return string Rendered product output.
*/
protected function get_button_html( $product ) {
if ( empty( $this->attributes['contentVisibility']['button'] ) ) {
return '';
}
return '' . $this->get_add_to_cart( $product ) . '
';
}
/**
* Get the "add to cart" button.
*
* @param \WC_Product $product Product.
* @return string Rendered product output.
*/
protected function get_add_to_cart( $product ) {
$attributes = array(
'aria-label' => $product->add_to_cart_description(),
'data-quantity' => '1',
'data-product_id' => $product->get_id(),
'data-product_sku' => $product->get_sku(),
'rel' => 'nofollow',
'class' => 'wp-block-button__link add_to_cart_button',
);
if ( $product->supports( 'ajax_add_to_cart' ) ) {
$attributes['class'] .= ' ajax_add_to_cart';
}
return sprintf(
'%s',
esc_url( $product->add_to_cart_url() ),
wc_implode_html_attributes( $attributes ),
esc_html( $product->add_to_cart_text() )
);
}
}