英文:
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('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>';
}
}
}
}
答案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 ''
or null
.
Reminder for possible empty values:
The following values evaluates to empty:
0
0.0
"0"
""
NULL
FALSE
array()
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论