• Resolved Ugur

    (@ugurterzi)


    Do you guys have any solution to add CSS (e.g. pending-payment-class) class into the body tag when a user has an order/s on pending payment status?

Viewing 10 replies - 1 through 10 (of 10 total)
  • Hello @ugurterzi ,

    That’s interesting.

    Can you please specify on which page you want to add this body class?

    In general, you can add a body class using this WordPress filter – body_class

    To access order status, you can use this structure –

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

    Now, you can define your own method/structure to add the status in the body class.

    I hope this information helps.

    Thank you ??

    Thread Starter Ugur

    (@ugurterzi)

    Thanks, @rur165
    It’ll help me a lot. By the way, I will add this class for only my account page and its children. I hope is_page( $page = ” ) will help.

    Thread Starter Ugur

    (@ugurterzi)

    It did not work well ?? what am I missing here

    add_filter( 'body_class', 'custom_class' );
    function custom_class( $order_status ) {
    	if( is_page( 5674198 ) ) {
    		$order = wc_get_order( $order_id );
    		$order_status = $order->get_status();
        }
        return $order->get_status();
    }
    • This reply was modified 4 years, 1 month ago by Ugur.
    Thread Starter Ugur

    (@ugurterzi)

    I was very hopeful about this snippet but it didn’t happen, what am I missing here?

    add_filter( 'body_class', 'order_class');
    
    function order_class($orderclasses) {
    
    $order = wc_get_order( $order_id );
    $order_status  = $order->get_status();
    
        if( is_page( 5674198 ))  {
            $orderclasses[] = $order_status;
        }
        return $orderclasses;
    }
    Thread Starter Ugur

    (@ugurterzi)

    It does the trick

    
    add_filter( 'body_class', 'order_class');
    
    function order_class( $orderclasses ) {
    
        $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_page( 30 ))  {
            $orderclasses[] = $order_status;
        }
        return $orderclasses;
    }

    Hello @ugurterzi ,

    Okay, the challenge is here is to get the order ID, and then using that order ID we can get the whole order object. Without the order ID, we are unable to get anything.

    I have tried to get the order status on the body class when I am visiting an order details page. Because otherwise, we can not say which order status to pick. So, this is my solution to get the current order’s status –

    add_filter( 'body_class', 'order_class');
    
    function order_class($orderclasses) {
    
    global $wp;
    
    if (is_wc_endpoint_url( 'view-order' )) {
    	$order = wc_get_order( $wp->query_vars['view-order'] );
    	$order_status  = $order->get_status();
    	$orderclasses[] = $order_status;  
    }
    
        return $orderclasses;
    }

    I hope this information helps.

    Thank you ??

    Thread Starter Ugur

    (@ugurterzi)

    Thank you @rur165

    The usage of the is_wc_endpoint_url would help me on the next move thanks for the help.
    I fixed my problem via the last order with the above structure that I have added.

    Cheers.

    Thread Starter Ugur

    (@ugurterzi)

    Both your solution @rur165 and mine cause a fatal error when the user has no order or/and not logged in. Do you have a solution for that? Thanks

    Thread Starter Ugur

    (@ugurterzi)

    I fixed it according to my needs such as:

    
    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, 'uye' ) ) {
    
        $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;
    	}
    }
    

    Hello @ugurterzi ,

    I am glad to see that you have solved the problem.

    For me, the codes were working. It’s possible that the membership extension that you were using caused the problem. Thanks for sharing the updated code. I am sure someone else will be able to use this on their site.

    Regards,
    Rashed

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘How to add order status class to the body tag?’ is closed to new replies.