根据Woocommerce产品类别更改“添加到购物车”按钮颜色

在Woocommerce中,我试图根据它所在的产品类别更改“添加到购物车”按钮的颜色。到目前为止,我能够更改文本,但是我无法弄清楚如何传入哈希格式的持久颜色值(例如#fffff)

// Change add to cart button text per category add_filter( 'woocommerce_product_single_add_to_cart_text', 'wps_custom_cart_button_text' ); function wps_custom_cart_button_text() { global $product; $terms = get_the_terms( $product->ID, 'product_cat' ); foreach ($terms as $term) { $product_cat = $term->slug; break; } switch($product_cat) { case 'clothing'; return __('Order your Clothes', 'your_theme_text_domain' ); default; return __( 'Order now', 'your_theme_text_domain' ); } } 

任何帮助赞赏。

这是一种根据产品类别更改按钮文本和按钮颜色的方法:

 // Change add to cart button text per category add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_single_add_to_cart_text', 20, 2 ); function custom_single_add_to_cart_text( $text_button, $product ) { global $post; $domain = 'woocommerce'; // HERE set the array of categories terms slug and corresponding colors and texts $categories_colors = array( 'clothing' => array( 'color' => 'red', 'text' => __('Order your Clothes', $domain ) ), 'posters' => array( 'color' => 'blue', 'text' => __('Order your Posters', $domain ) ), ); $color = ''; $text_button = __('Order now', $domain ); // default foreach ($categories_colors as $key => $value ) { if( has_term( $key, 'product_cat', $post->ID ) ){ $color = $value['color']; $text_button = $value['text']; break; } } if ( empty($color) ) return $text_button; // Exit if empty // Dynamic css andjquery to set the color when defined ?>     

代码位于活动子主题(或活动主题)的function.php文件中。 经过测试和工作。

您可以在代码中删除整个

bock,在活动主题styles.css文件中添加自己的css规则(基于颜色添加的css类)

解决了使用CSS修改按钮样式而不使用PHP。 调用分配的类别的类ID,然后调用woocommerce按钮选择器元素。

只需将其放入您的附加CSS或自定义CSS部分:

 /*Call the class ID of the category and the "add to cart" button assigned to that category*/ .product_cat-CATEGORYNAME.product .button { /*Button styling here*/ border-radius:5px; color:#ffff; background-color: #fffff; }