admin管理员组文章数量:1323348
I try to give out three divs with three different colors and phrases (each for every order-status: completed(green), pending (red), processing (orange).
<div class="order-actions2">
<?php
$order_status = $order->get_status();
if($order_status=="completed") {
echo "Thank you for the payment" . ' ';
$actions = wc_get_account_orders_actions( $order );
?> <style type=text/css>.order-actions2 {background-color: green;} </style><?php
}
elseif($order_status=="pending") {
echo "Spending money is always a statement" . ' ';
$actions = wc_get_account_orders_actions( $order );
?> <style type=text/css>.order-actions2 {background-color: red;} </style><?php
}
elseif($order_status=="processing") {
echo "Spending money is always a statement" . ' ';
$actions = wc_get_account_orders_actions( $order );
?> <style type=text/css>.order-actions2 {background-color: orange;} </style><?php
}
if ( ! empty( $actions ) ) {
foreach ( $actions as $key => $action ) { // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
echo '<a href="' . esc_url( $action['url'] ) . '" class="order-action2-button ' . sanitize_html_class( $key ) . '">' . esc_html( $action['name'] ) . ' ' . 'now' . '</a>';
}
}
?>
</div>
At the moment every div shows up green. If I wrap every status in an own div, I need to open and close php all the time. This makes me unable to use elseif. Maybe there is a solution with arrays or something I don't know, yet? Thank you.
I try to give out three divs with three different colors and phrases (each for every order-status: completed(green), pending (red), processing (orange).
<div class="order-actions2">
<?php
$order_status = $order->get_status();
if($order_status=="completed") {
echo "Thank you for the payment" . ' ';
$actions = wc_get_account_orders_actions( $order );
?> <style type=text/css>.order-actions2 {background-color: green;} </style><?php
}
elseif($order_status=="pending") {
echo "Spending money is always a statement" . ' ';
$actions = wc_get_account_orders_actions( $order );
?> <style type=text/css>.order-actions2 {background-color: red;} </style><?php
}
elseif($order_status=="processing") {
echo "Spending money is always a statement" . ' ';
$actions = wc_get_account_orders_actions( $order );
?> <style type=text/css>.order-actions2 {background-color: orange;} </style><?php
}
if ( ! empty( $actions ) ) {
foreach ( $actions as $key => $action ) { // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
echo '<a href="' . esc_url( $action['url'] ) . '" class="order-action2-button ' . sanitize_html_class( $key ) . '">' . esc_html( $action['name'] ) . ' ' . 'now' . '</a>';
}
}
?>
</div>
At the moment every div shows up green. If I wrap every status in an own div, I need to open and close php all the time. This makes me unable to use elseif. Maybe there is a solution with arrays or something I don't know, yet? Thank you.
Share Improve this question edited Sep 8, 2020 at 13:59 Chrizzly asked Sep 7, 2020 at 13:57 ChrizzlyChrizzly 34 bronze badges 1- Keep in mind you need a specific question to ask that can be factually and completely answered, not an opinion or discussion topic. Can you adjust your question so it's clearly stated what the specific question is? – Tom J Nowell ♦ Commented Sep 7, 2020 at 14:46
1 Answer
Reset to default 0Your issue is a misunderstanding of how CSS/HTML works.
Take this example:
<style>p { color: red; }</style>
<p>1</p>
<style>p { color: blue; }</style>
<p>2</p>
<style>p { color: green; }</style>
<p>3</p>
Which colour is the 1st paragraph?
I suspect you would answer red, but that would be incorrect. All paragraphs are green.
The disconnect is that p { color: green; }
doesn't mean "from here onwards paragraphs are green", it means "all paragraphs on the page are green".
What's worse is these are inline style tags, which are very bad practice and completely unnecessary.
So instead, just add a HTML class that mentions the order status, and put rules in a CSS file to change the colours, e.g.
<div class="order-actions2 order-status--complete">
.order-status--complete {
background-color: green;
}
In PHP you could do this:
<div class="order-actions2 order-status--<?php echo esc_attr( $order->status ); ?>">
This removes the styling part of the code from PHP entirely, simplifying things significantly, and making it much nicer to work with.
Remember, CSS rules apply globally across the entire page. .order-actions2 { ... }
refers to all elements that have order-actions2
as a HTML class, regardless of where they appear on the page.
You might also be tempted to use the style=""
attribute, which is also extremely very bad practice. Ignore anybody who suggests this solution.
Some extra notes:
- That
$actions = ..
line is identical in every single case, just move it to the end and call it once just before the if check - There's no reason to append
. ' '
just add the space in the first string sanitize_html_class
is not a substitute foresc_attr
. We're escaping here, not sanitising, so it still has to be wrapped inesc_attr
版权声明:本文标题:php - How can I change the background color of divs dynamically (depending on an order-status in woocommerce)? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742132007a2422203.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论