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

 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;

}







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;

}

Comments

Popular posts from this blog

Add Css And Js Files on Specific Page In laravel

Xpendy Pending

Laravel Join with two coloums