如何获取没有运输类别的产品

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

How to get a product with no shipping class

问题

我在stackoverflow上找到了一段代码,稍微修改了一下,以便为每个产品显示运输类别及其相关费用,这部分是'硬编码'的,因为我只有2个运输类别,具体取决于产品大小,只有一种运输方式。

我可以显示类别并'回显'与该类别相关的价格,对于具有运输类别的产品,但是如果没有类别,我不知道如何获取没有类别产品的'字符串值'或任何其他可以针对它们的东西。

以下是代码,也许有人可以帮助,(我已经尝试过'',null),我对PHP真的很陌生...

add_action('woocommerce_single_product_summary', 'display_product_shipping_class', 15 );
function display_product_shipping_class(){
    global $product;
    
    $shipping_class = $product->get_shipping_class();
	if( ! empty($shipping_class) ) {
        $term = get_term_by( 'slug', $shipping_class, 'product_shipping_class' );
    
        if( is_a($term, 'WP_Term') ){
            echo '<p class="product-shipping-class">' . $term->name . '</p>';
			$myVar1 = "pavolum";
			$myVar2 = "pvolum";
			$myVar3 = "";
			if( $shipping_class==$myVar1){ 
				echo '<p class="tarif">Livraison à Tarif=10dt</p>';
			}
			if( $shipping_class==$myVar2){ 
				echo '<p class="tarif">Livraison à Tarif=15dt</p>';
			}
			if($shipping_class== null){
				echo '<p class="tarif">Livraison à Tarif=7dt</p>';
			}
        }
    }
}
英文:

I have found a code in stackoverflow that I slighltly modified to show for each product the shipping class and its associated cost and this 'hard coded' as I only have 2 shipping classes depending on product size for just one shipping method.

I can show the class and 'echo' the price associated to this class for products who have a shipping class, however if there is no class I am getting stuck on how to get the 'string value' of no class products or anything else that can target them.

Below is the code maybe someone can help, (I have tried '', null), I am really new with PHP...

add_action(&#39;woocommerce_single_product_summary&#39;, &#39;display_product_shipping_class&#39;, 15 );
function display_product_shipping_class(){
    global $product;
    
    $shipping_class = $product-&gt;get_shipping_class();
	if( ! empty($shipping_class) ) {
        $term = get_term_by( &#39;slug&#39;, $shipping_class, &#39;product_shipping_class&#39; );
    
        if( is_a($term, &#39;WP_Term&#39;) ){
            echo &#39;&lt;p class=&quot;product-shipping-class&quot;&gt;&#39; . $term-&gt;name . &#39;&lt;/p&gt;&#39;;
			$myVar1 = &quot;pavolum&quot;;
			$myVar2 = &quot;pvolum&quot;;
			$myVar3 = &quot;&quot;;
			if( $shipping_class==$myVar1){ 
				echo &#39;&lt;p class=&quot;tarif&quot;&gt;Livraison &#224; Tarif=10dt&lt;/p&gt;&#39;;
			}
			if( $shipping_class==$myVar2){ 
				echo &#39;&lt;p class=&quot;tarif&quot;&gt;Livraison &#224; Tarif=15dt&lt;/p&gt;&#39;;
			}
			if($shipping_class== null){
				echo &#39;&lt;p class=&quot;tarif&quot;&gt;Livraison &#224; Tarif=7dt&lt;/p&gt;&#39;;
			}
        }
    }
}

答案1

得分: 3

I'll provide the translated code portion:

问题

首先,在第5行,您有以下条件来检查$shipping_class是否为空,然后继续执行:

if( ! empty($shipping_class) )

使用此条件,您永远不会达到第三种情况,其中您将比较$shipping_class$myVar1$myVar2和可能是''null的情况。


提醒可能为空的值:

以下值被视为空:

0
0.0
"0"
""
NULL
FALSE
array()

(空值来源)

可能的解决方案

一个可能的解决方案是检查$shipping_class是否为empty(),并添加一个else语句来处理它为空/未定义的情况。

带有代码示例的示例:

if( ! empty($shipping_class) ) {
   // 在这里与$myVar1和$myVar2进行比较
}
else {
  // 处理$shipping_class为空的情况
}

另一个可能的解决方案是删除对empty()的检查,并使用第二个if来处理预期工作的情况。

英文:

The problem

For a start, on line #5, you have the following condition which check if $shipping_class is different than empty before going forward:

if( ! empty($shipping_class) )

With this condition, you will never reach the third case where you compare $shipping_class with $myVar1, $myVar2 and potentially &#39;&#39; or null.


Reminder for possible empty values:

The following values evaluates to empty:

0
0.0
&quot;0&quot;
&quot;&quot;
NULL
FALSE
array()

(Source for empty())


Possible solutions

A possible solution could be to check if $shipping_class is empty() and add a else statement to manage the case where it's empty/not defined.

Example with code:

if( ! empty($shipping_class) ) {
   // Do comparison with $myVar1 and $myVar2 here
}
else {
  // Manage case where $shipping_class is empty
}

Another possible solution could be to remove the check on empty() and manage the case with your second if, which should now be working as expected.

huangapple
  • 本文由 发表于 2023年7月4日 23:15:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613969.html
匿名

发表评论

匿名网友

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

确定