Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

php - Woocommerce : Add Body class to product page if the current product is in the cart

I found this great snippet from here https://gist.github.com/michaelbourne/a2fbebbc839a41c4204524fcd020061e

The following is the function to add a body class to the Woocomerce product page if the cart contains any product.

    <?php

// Add a body class via the body_class filter in WP
// =============================================================================
add_filter( 'body_class', 'mb_body_class_for_cart_items' );
function mb_body_class_for_cart_items( $classes ) {
    if( ! WC()->cart->is_empty() ){
        $classes[] = 'cart-has-items';
    }
    return $classes;
}

// Add a body class via javascript for AJAX enabled sites
// =============================================================================
add_action( 'wp_footer', 'mb_body_class_for_ajax_add_to_cart' );
function mb_body_class_for_ajax_add_to_cart() {
?>
  <script type="text/javascript">
    (function($){
      $('body').on( 'added_to_cart', function(){
        if( ! $(this).hasClass('cart-has-items') ){
          $(this).addClass('cart-has-items');
        }
      });
    })(jQuery);
  </script>
<?php
}

Does anyone know how to modify the above code snippet to add a class to the body of the product page when the cart contains the current product?

For example, if 'item A' is in the cart, when a visitor browses the 'item A' product page it should contain body class 'already-added'

Any help would be appreciated.

question from:https://stackoverflow.com/questions/65863248/woocommerce-add-body-class-to-product-page-if-the-current-product-is-in-the-ca

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need a function that you can use multiple times. This function can be used for both variable and simple products:

function ywp_check_product_is_in_cart( $product_id ) {
    if ( ! WC()->cart->is_empty() ) {
        foreach( WC()->cart->get_cart() as $cart_item ) {
            $cart_item_ids = array( $cart_item['product_id'], $cart_item['variation_id'] );

            if( in_array( $product_id, $cart_item_ids ) ) {
                return true;
            }
        }

        return false;
    }

    return false;
}

USAGE

add_filter( 'body_class', 'ywp_body_class_for_cart_items' );
function ywp_body_class_for_cart_items( $classes ) {
    // Check user currently is in product page
    if( ! is_singular( 'product' ) ) {
        return $classes;
    }

    if( ywp_check_product_is_in_cart( get_the_id() ) ) {
        $classes[] = 'already-added';
    }

    return $classes;
}

Code goes in functions.php of your actiive theme/child theme. Tested and works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...