doue8385 2018-06-05 07:44
浏览 62
已采纳

当特定产品类别在Woocommerce购物车中时,有条件地应用CSS样式

Before I start my question I would like to let you know that I am a graphic designer, not a developer. I am helping out a buddy with their website so please forgive my ignorance and lack of knowledge.

I am building a site in Wordpress with a WooCommerce shop. I am fairly comfortable with CSS and HTML but have virtually no php knowledge and I am very new to WooCommerce.

I've got a very specific thing I'd like to achieve but I don't really know where to start.

Essentially I'd like to apply a conditional css style once a product of a particular category is placed in the cart (and reversed if the product is removed). Specifically I'd like to change the colour of an icon that sits in a widget and will be displayed on the header. So the effect is that when a visitor adds a 'gift' to their cart, the 'gift' icon will 'light up' to prompt the visitor to next add a 'card' and so on.

I found the following which looks at first glance like it might get me close, but I don't know how to implement it, so any help would be very appreciated:

  1. // set our flag to be false until we find a product in that category
  2. $cat_check = false;
  3. // check each cart item for our category
  4. foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
  5. $product = $cart_item['data'];
  6. // replace 'membership' with your category's slug
  7. if ( has_term( 'membership', 'product_cat', $product->id ) ) {
  8. $cat_check = true;
  9. // break because we only need one "true" to matter here
  10. break;
  11. }
  12. }
  13. // if a product in the cart is in our category, do something
  14. if ( $cat_check ) {
  15. // we have the category, do what we want
  16. }

Thanks in advance!

展开全部

  • 写回答

2条回答 默认 最新

  • draxu26480 2018-06-05 08:20
    关注

    Try the following slightly different revisited code instead in a hooked function, that will allow you to add some inline CSS styles when a specific product category is found in cart items:

    1. add_action( 'wp_head' , 'custom_conditional_css_styles' );
    2. function custom_conditional_css_styles(){
    3. if( WC()->cart->is_empty() ) return; // We exit is cart is empty
    4. // HERE your product category
    5. $product_category = 'membership';
    6. $found = false;
    7. // Loop through cart items checking for our product category
    8. foreach ( WC()->cart->get_cart() as $cart_item ) {
    9. if ( has_term( 'membership', 'product_cat', $cart_item['product_id'] ) ) {
    10. $found = true;
    11. break; // Found we stop the loop
    12. }
    13. }
    14. if ( ! $found ) return; // If the product category is noy found we exit
    15. // Here come your CSS styles
    16. ?>
    17. <style>
    18. .my-class { color:red;}
    19. </style>
    20. <?php
    21. }

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    When using has_term() with cart items, never use $cart_item['data']->get_id() but always $cart_item['product_id'] instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部