有条件地显示 Woocommerce 结帐字段
Conditionally display Woocommerce checkout field
在 Wordpress 网站上,我目前正在尝试设置一个 Woocommerce 和 Learnpress plugins. I Use Woocommerce Checkout Add-ons 商业插件,它允许创建一些额外的结帐字段。
我想有条件地显示一些结帐字段,仅当结帐购物车中存在某些 Learnpress 课程时。
结帐插件中的代码如下:
<?php if ( $add_on_fields ) : ?>
<div id="wc_checkout_add_ons">
<?php
foreach ( $add_on_fields as $key => $field ) :
woocommerce_form_field( $key, $field, WC()->checkout()->get_value(
$key ) );
endforeach;
?>
</div>
<?php endif; ?>
于是开始走这条路:
<div id="wc_checkout_add_ons">
<?php
$course_category = get_the_terms( $post->ID, 'course_category' );
echo $course_category;
if ($course_category == 'weekend-intensives') {
foreach ( $add_on_fields as $key => $field ) :
woocommerce_form_field( $key, $field, WC()->checkout()->get_value( $key ) );
endforeach;
} else {
echo ('Proceed with checkout');
}
?>
</div>
现在我什至没有得到 $course_category
的初始回显所以我知道我已经错了...
我需要弄清楚获取 checkout/cart 中课程的 learnpress 课程类别 的代码是什么。
我知道它远不止于此,而且我可能还有很长的路要走,但我愿意在一些帮助下完成它。
如有任何帮助,我们将不胜感激。
您可以将 has_term()
特定的 WordPress 条件函数用于自定义分类法和自定义分类法 "Product categories" 或者您的情况 "Course categories"可以轻松使用。
1) 您需要检查购物车商品,查看是否有购物车商品属于 'weekend-intensives' 课程类别,这必须在代码开头完成。这样你就可以改变现有的条件 if ( $add_on_fields ) :
…
2) 您需要确保 课程类别 分类法适用于 WooCommerce 产品自定义 post 类型。否则,您将无法检查任何课程类别 购物车项目。
所以正确的做法是:
<?php
## ---- Custom code start here ---- ##
$is_in_cart = false;
// Checking cart items for specific "Course category"
foreach( WC->cart->get_cart() as $cart_item ){
// Here your Course category
if( has_term( 'weekend-intensives', 'course_category', $cart_item['product_id'] ){
$is_in_cart = true; // Found
break; // Stop the loop
}
}
// adding our condition in existing code if statement
if ( $add_on_fields && $is_in_cart ) :
## ---- Custom code Ends here ---- ##
// if ( $add_on_fields ) :
?>
<div id="wc_checkout_add_ons">
<?php
foreach ( $add_on_fields as $key => $field ) :
woocommerce_form_field( $key, $field, WC()->checkout()->get_value( $key ) );
endforeach;
?>
</div>
<?php endif; ?>
这未经测试,但应该可以(如果课程类别适用于 WC 产品)
更新
检查产品 ID 是否适用于 "weekend-intensives" 课程类别:
- 代码进入您活跃的子主题(或主题)的 function.php 文件。
- 在其中设置正确的产品 ID 以供检查和保存。
- 进入档案页面或产品页面查看结果(保存后)。
代码:
// Checking for "weekend-intensives" course category in shop and product pages
add_action( 'woocommerce_before_main_content', function(){
// Only for admin user role
if( ! current_user_can('edit_products')) return;
// ==> BELOW set your product ID to check
$product_id = 43;
// Output the raw cart object data
if( has_term( 'weekend-intensives', 'course_category', $product_id ){
echo '<pre>YES! This product works with "weekend-intensives" course category</pre>';
} else {
echo '<pre>This product DOES NOT WORK with "weekend-intensives" course category</pre>';
}
}, 987 );
在 Wordpress 网站上,我目前正在尝试设置一个 Woocommerce 和 Learnpress plugins. I Use Woocommerce Checkout Add-ons 商业插件,它允许创建一些额外的结帐字段。
我想有条件地显示一些结帐字段,仅当结帐购物车中存在某些 Learnpress 课程时。
结帐插件中的代码如下:
<?php if ( $add_on_fields ) : ?>
<div id="wc_checkout_add_ons">
<?php
foreach ( $add_on_fields as $key => $field ) :
woocommerce_form_field( $key, $field, WC()->checkout()->get_value(
$key ) );
endforeach;
?>
</div>
<?php endif; ?>
于是开始走这条路:
<div id="wc_checkout_add_ons">
<?php
$course_category = get_the_terms( $post->ID, 'course_category' );
echo $course_category;
if ($course_category == 'weekend-intensives') {
foreach ( $add_on_fields as $key => $field ) :
woocommerce_form_field( $key, $field, WC()->checkout()->get_value( $key ) );
endforeach;
} else {
echo ('Proceed with checkout');
}
?>
</div>
现在我什至没有得到 $course_category
的初始回显所以我知道我已经错了...
我需要弄清楚获取 checkout/cart 中课程的 learnpress 课程类别 的代码是什么。
我知道它远不止于此,而且我可能还有很长的路要走,但我愿意在一些帮助下完成它。
如有任何帮助,我们将不胜感激。
您可以将 has_term()
特定的 WordPress 条件函数用于自定义分类法和自定义分类法 "Product categories" 或者您的情况 "Course categories"可以轻松使用。
1) 您需要检查购物车商品,查看是否有购物车商品属于 'weekend-intensives' 课程类别,这必须在代码开头完成。这样你就可以改变现有的条件 if ( $add_on_fields ) :
…
2) 您需要确保 课程类别 分类法适用于 WooCommerce 产品自定义 post 类型。否则,您将无法检查任何课程类别 购物车项目。
所以正确的做法是:
<?php
## ---- Custom code start here ---- ##
$is_in_cart = false;
// Checking cart items for specific "Course category"
foreach( WC->cart->get_cart() as $cart_item ){
// Here your Course category
if( has_term( 'weekend-intensives', 'course_category', $cart_item['product_id'] ){
$is_in_cart = true; // Found
break; // Stop the loop
}
}
// adding our condition in existing code if statement
if ( $add_on_fields && $is_in_cart ) :
## ---- Custom code Ends here ---- ##
// if ( $add_on_fields ) :
?>
<div id="wc_checkout_add_ons">
<?php
foreach ( $add_on_fields as $key => $field ) :
woocommerce_form_field( $key, $field, WC()->checkout()->get_value( $key ) );
endforeach;
?>
</div>
<?php endif; ?>
这未经测试,但应该可以(如果课程类别适用于 WC 产品)
更新
检查产品 ID 是否适用于 "weekend-intensives" 课程类别:
- 代码进入您活跃的子主题(或主题)的 function.php 文件。
- 在其中设置正确的产品 ID 以供检查和保存。
- 进入档案页面或产品页面查看结果(保存后)。
代码:
// Checking for "weekend-intensives" course category in shop and product pages
add_action( 'woocommerce_before_main_content', function(){
// Only for admin user role
if( ! current_user_can('edit_products')) return;
// ==> BELOW set your product ID to check
$product_id = 43;
// Output the raw cart object data
if( has_term( 'weekend-intensives', 'course_category', $product_id ){
echo '<pre>YES! This product works with "weekend-intensives" course category</pre>';
} else {
echo '<pre>This product DOES NOT WORK with "weekend-intensives" course category</pre>';
}
}, 987 );