* Fix Product Categories List with align attribute

* Add support to align attribute to the Product Categories List block
This commit is contained in:
Albert Juhé Lluveras 2020-06-16 13:18:04 +02:00 committed by GitHub
parent 582ba8797f
commit b0ef232271
2 changed files with 37 additions and 1 deletions

View File

@ -35,6 +35,13 @@ registerBlockType( 'woocommerce/product-categories', {
},
},
attributes: {
/**
* Alignment of the block.
*/
align: {
type: 'string',
},
/**
* Whether to show the product count in each category.
*/

View File

@ -43,6 +43,7 @@ class ProductCategories extends AbstractDynamicBlock {
return array_merge(
parent::get_attributes(),
array(
'align' => $this->get_schema_align(),
'className' => $this->get_schema_string(),
'hasCount' => $this->get_schema_boolean( true ),
'hasImage' => $this->get_schema_boolean( false ),
@ -84,13 +85,41 @@ class ProductCategories extends AbstractDynamicBlock {
}
}
$output = '<div class="wc-block-product-categories ' . esc_attr( $attributes['className'] ) . ' ' . ( $attributes['isDropdown'] ? 'is-dropdown' : 'is-list' ) . '">';
$classes = $this->get_container_classes( $attributes );
$output = '<div class="' . esc_attr( $classes ) . '">';
$output .= ! empty( $attributes['isDropdown'] ) ? $this->renderDropdown( $categories, $attributes, $uid ) : $this->renderList( $categories, $attributes, $uid );
$output .= '</div>';
return $output;
}
/**
* Get the list of classes to apply to this block.
*
* @param array $attributes Block attributes. Default empty array.
* @return string space-separated list of classes.
*/
protected function get_container_classes( $attributes = array() ) {
$classes = array( 'wc-block-product-categories' );
if ( isset( $attributes['align'] ) ) {
$classes[] = "align{$attributes['align']}";
}
if ( ! empty( $attributes['className'] ) ) {
$classes[] = $attributes['className'];
}
if ( $attributes['isDropdown'] ) {
$classes[] = 'is-dropdown';
} else {
$classes[] = 'is-list';
}
return implode( ' ', $classes );
}
/**
* Get categories (terms) from the db.
*