skip to Main Content

I try to change the color of an order-status-span-element to color red, if the order status is "Pending payment".
As soon as the status changes to "completed", the span text color should switch to green.

Here is the clean div code:

<div class="order-status">
        <span>Order Status</span>
        <?php elseif ( 'order-status' === $column_id ) : ?>
        <?php echo '<span id="order-status-value">' . esc_html( wc_get_order_status_name( $order->get_status() ) ) . '<span>'; ?>

    </div>

I tried to insert a proper if-statement, but I don’t know which function or variable I need to insert.
This is the snippet is only a part of my orders.php ->(…/my-account/orders).

<div class="order-status">
    <span>Order Status</span>
    <?php elseif ( 'order-status' === $column_id ) : ?>
    <?php echo '<span id="order-status-value">' . esc_html( wc_get_order_status_name( $order->get_status() ) ) . '<span>';
        if ( strcasecmp( wc_get_order_status_name( $order->get_status() ) == 0 ) ) :
                echo "The if statement works!";
        ?>      
                <style type="text/css">
                    #order-status-value {
                        color: green;
                    }
                </style>
        <?php 
        else: 
        ?>
                <style type="text/css">
                    #order-status-value {
                        color: red;
                    }
                </style>
    </div>
</div>

This code doesn’t work.

2

Answers


  1. Chosen as BEST ANSWER

    Here is the solution I use at the moment - this one works perfectly.

    <div class="order-status">
                <span>Order Status</span>
                <?php           
                elseif ( 'order-status' === $column_id ) :
                    $order_color_check = $order->get_status();  
                    if($order_color_check=="completed") :
                        echo '<span class="order-status-value" style="color: green;">' . esc_html( wc_get_order_status_name( $order->get_status() ) ) . '</span>';
                        
                    else :
                        echo '<span class="order-status-value" style="color: red;">' . esc_html( wc_get_order_status_name( $order->get_status() ) ) . '</span>';
                    endif; ?>
            </div>
    

  2. You could use the "style" HTML attribute to change the color :

    <?php
    
        $is_green = wc_get_order_status_name($order->get_status()) == 0;
            
        echo '<span id="order-status-value" ' 
        . ($is_green ? 'style="color:green"' : '')
        . '>' 
        . esc_html( wc_get_order_status_name( $order->get_status() ) ) 
        . '<span>';
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search