自定义 WooCommerce 短代码中的库存状态颜色徽章

huangapple go评论72阅读模式
英文:

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" 部分:

自定义 WooCommerce 短代码中的库存状态颜色徽章

我尝试使用 background-color 来修改 的颜色,但它只改变了文本的颜色,我希望整个按钮都能改变颜色。

英文:

In WooCommerce, I use the following code in my Theme Functions, functions.php:

function webis_stoc( $atts ){
   global $product;
   echo &quot;&lt;div class=&#39;stoc&#39;&gt;&quot;;
   $stoc=$product-&gt;stock_status;
   switch($stoc){
	   case &quot;onbackorder&quot;: echo &quot;&lt;span class=&#39;precomanda&#39;; style=&#39;color:orange;&#39;; &gt;Precomanda&lt;/span&gt;&quot;; break;
      case &quot;instock&quot;: echo &quot;&lt;span class=&#39;inStoc&#39;; style=&#39;color:green;&#39; &gt;in stoc&lt;/span&gt;&quot;; break;
      case &quot;outofstock&quot;: echo &quot;&lt;span class=&#39;farastoc&#39;; style=&#39;color:red;&#39; &gt;Stoc epuizat&lt;/span&gt;&quot;; break;
      default : echo $stoc; break;
   }
   echo &quot;&lt;/div&gt;&quot;;
}
add_shortcode  (&#39;webis_stoc&#39;,&#39;webis_stoc&#39; ); 

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:

自定义 WooCommerce 短代码中的库存状态颜色徽章

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  (&#39;webis_stoc&#39;, &#39;webis_stoc&#39; ); 
function webis_stoc( $atts ){
    global $product;
    
    if( ! $product ) return; // exit if $product variable is not defined
    
    $stoc = $product-&gt;get_stock_status(); // Get product stock status
    
    $html = &#39;&lt;div class=&quot;stoc&quot;&gt;&#39;;
   
    switch( $stoc ){
        case &#39;onbackorder&#39;: // 
            $html .= &#39;&lt;span class=&quot;precomanda&quot;; style=&quot;color:orange;&quot;&gt;Precomanda&lt;/span&gt;&#39;; 
            break;
        case &#39;outofstock&#39;: 
            $html .= &#39;&lt;span class=&quot;farastoc&quot;; style=&quot;color:red;&quot; &gt;Stoc epuizat&lt;/span&gt;&#39;; 
            break;
        default: // &#39;instock&#39; (default fallback)
            $html .= &#39;&lt;span class=&quot;inStoc&quot;; style=&quot;color:green;&quot; &gt;in stoc&lt;/span&gt;&#39;; 
            break;
    }
    return $html . &#39;&lt;/div&gt;&#39;; // For shortcodes, always return, never echo...
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

huangapple
  • 本文由 发表于 2023年6月13日 17:26:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76463456.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定