Search code examples
csswordpresshook-woocommerce

How to add order status class to the body tag?


do you know how can we add CSS (let's call pending-payment-class) class into the body tag when a user has an order which is on pending payment status?

I tried this one but no luck

add_filter( 'body_class', 'order_class');

function order_class($orderclasses) {

$order = wc_get_order( $order_id );
$order_status  = $order->get_status();

    if( is_page( 30 ))  {
        $orderclasses[] = $order_status;
    }
    return $orderclasses;
}

Thanks.


Solution

  • I fixed it with this one

    add_filter( 'body_class', 'order_class');
    
    function order_class( $orderclasses ) {
    
        // bail if Memberships isn't active
        if ( ! function_exists( 'wc_memberships' ) ) {
                    return;
            }
            
        if ( wc_memberships_is_user_active_member( $user_id, 'membership-slug' ) ) {
    
        $user_id = get_current_user_id(); // The current user ID
        
        // Get the WC_Customer instance Object for the current user
        $customer = new WC_Customer( $user_id );
        
        // Get the last WC_Order Object instance from current customer
        $last_order = $customer->get_last_order();
        
        $order_id     = $last_order->get_id(); // Get the order id
        $order_data   = $last_order->get_data(); // Get the order unprotected data in an array
        $order_status = $last_order->get_status(); // Get the order status
        
        if (is_account_page()) {
            $orderclasses[] = $order_status;
            }
            return $orderclasses;
        }
    }
    

    Note: it only works for the setup that has WC Memberships and last order. You can modify it according to your needs.