我希望根据产品的属性添加一个身体类别,例如颜色,因此,如果产品颜色为红色标签。
我将使用什么钩子来做到这一点?
这可以使用 body_class
filter Hook中的自定义函数来完成此操作:
add_filter( 'body_class', function( $classes ) {
if( ! is_product() ) return $classes;
global $post;
$custom_classes = array();
$product = wc_get_product( $post->ID );
foreach( $product->get_attributes() as $taxonomy => $wc_attribute ){
if( $taxonomy == 'pa_color' )
$custom_classes = $wc_attribute->get_slugs();
return array_merge( $classes, $custom_classes );
} );
代码在您的活动子主题(或主题)的function.php文件中或任何插件文件中。
此代码在WooCommerce 3 上进行了测试。