英文:
Stock status color badge in a custom shortcode for WooCommerce
问题
In WooCommerce,在我的主题功能 functions.php 中,我使用以下代码:
function webis_stoc( $atts ){
global $product;
echo "<div class='stoc'>";
$stoc=$product->stock_status;
switch($stoc){
case "onbackorder": echo "<span class='precomanda' style='color:orange;'>Precomanda</span>"; break;
case "instock": echo "<span class='inStoc' style='color:green;'>in stoc</span>"; break;
case "outofstock": echo "<span class='farastoc' style='color:red;'>Stoc epuizat</span>"; break;
default : echo $stoc; break;
}
echo "</div>";
}
add_shortcode ('webis_stoc','webis_stoc' );
我在这里遇到的问题是,每种情况(3种情况)的 CSS 都没有显示出来,它总是绿色的,即使是在订购缺货或缺货的情况下,它仍然是绿色的。如何解决这个问题?
这里是应该是橙色的 "onbackorder" 部分:
我尝试使用 background-color 来修改 的颜色,但它只改变了文本的颜色,我希望整个按钮都能改变颜色。
英文:
In WooCommerce, I use the following code in my Theme Functions, functions.php:
function webis_stoc( $atts ){
global $product;
echo "<div class='stoc'>";
$stoc=$product->stock_status;
switch($stoc){
case "onbackorder": echo "<span class='precomanda'; style='color:orange;'; >Precomanda</span>"; break;
case "instock": echo "<span class='inStoc'; style='color:green;' >in stoc</span>"; break;
case "outofstock": echo "<span class='farastoc'; style='color:red;' >Stoc epuizat</span>"; break;
default : echo $stoc; break;
}
echo "</div>";
}
add_shortcode ('webis_stoc','webis_stoc' );
The Issue I am having here is that the CSS for each case (3 cases) is not displaying, It's always green, no red or orange, even if it's in on backorder or out of stock, it's still green.<br>
How can I solve this?
Here is the "onbackorder" that is supposed to be orange:
I tried to modify the colors with background-color <span>, but it only changed the color of the text, and I want the full button to recolor.
答案1
得分: 0
你的代码中存在一些错误、问题和缺失。
请尝试以下修订后的代码(已加注释):
add_shortcode('webis_stoc', 'webis_stoc');
function webis_stoc($atts){
global $product;
if( ! $product ) return; // 如果$product变量未定义,则退出
$stoc = $product->get_stock_status(); // 获取产品库存状态
$html = '<div class="stoc">';
switch($stoc){
case 'onbackorder': // 预定
$html .= '<span class="precomanda" style="color:orange;">Precomanda</span>';
break;
case 'outofstock':
$html .= '<span class="farastoc" style="color:red;">Stoc epuizat</span>';
break;
default: // 'instock'(默认回退)
$html .= '<span class="inStoc" style="color:green;">in stoc</span>';
break;
}
return $html . '</div>'; // 对于短代码,始终返回,不要使用echo...
}
将此代码放入活动子主题(或活动主题)的functions.php文件中。已测试并有效。
英文:
There are some mistakes, errors and missing things in your code.
Try the following revisited code instead (commented):
add_shortcode ('webis_stoc', 'webis_stoc' );
function webis_stoc( $atts ){
global $product;
if( ! $product ) return; // exit if $product variable is not defined
$stoc = $product->get_stock_status(); // Get product stock status
$html = '<div class="stoc">';
switch( $stoc ){
case 'onbackorder': //
$html .= '<span class="precomanda"; style="color:orange;">Precomanda</span>';
break;
case 'outofstock':
$html .= '<span class="farastoc"; style="color:red;" >Stoc epuizat</span>';
break;
default: // 'instock' (default fallback)
$html .= '<span class="inStoc"; style="color:green;" >in stoc</span>';
break;
}
return $html . '</div>'; // For shortcodes, always return, never echo...
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论