Customizr Pro 编辑菜单问题与一些 Woocommerce 自定义代码
Customizr Pro editing menu issue with some Woocommerce custom code
我试图询问 Customizr 支持我的代码是什么 运行,但他们基本上说他们不支持第三方插件,例如 Woocommerce
我需要根据人们在网站上购买的商品来限制支付类型。
例如,支票付款类型仅适用于购买课程的人。
这是执行此操作的代码:
<?php
add_filter( 'woocommerce_available_payment_gateways',
'threshold_unset_gateway_by_category' );
function threshold_unset_gateway_by_category( $available_gateways ) {
global $woocommerce;
$unset = false;
$category_ids = array( 22, 21, 25, 20);
foreach ( $woocommerce->cart->get_cart() as $key => $values ) {
$terms = get_the_terms( $values['product_id'], 'product_cat' );
foreach ( $terms as $term ) {
if ( in_array( $term->term_id, $category_ids ) ) {
$unset = true;
break;
}
}
}
if ( $unset == true ) unset( $available_gateways['cheque'] );
return $available_gateways;
}
我已经深入研究了 Customizr 文件,但找不到任何冲突。 Wordpress 文件可能有点复杂,所以我可能找错了树。
这个答案并不是为了解决您与 Customizr 的问题,我只是以一种更加轻便、紧凑和灵活的方式重新访问了您的代码。
我还在add_filter()
中添加了一个优先级和参数个数,这有时会解决一些奇怪的问题…
所以在这段代码中,我改用 WP has_term()
条件函数:
add_filter( 'woocommerce_available_payment_gateways', 'categories_unset_cheque_gateway', 99, 1 );
function categories_unset_cheque_gateway( $available_gateways ){
// Not in backend (admin)
if( is_admin() )
return $available_gateways;
// BELOW define your categories in the array (can be IDs, slugs or names)
$categories = array( 20, 21, 22, 25 );
// Loop through cart items checking for specific product categories
foreach ( WC()->cart->get_cart() as $cart_item )
if( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ){
unset( $available_gateways['cheque'] );
break; // Stop the loop
}
return $available_gateways;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。
已测试并有效 (未使用 Customizr Pro 进行测试)。
所以这个问题的解决方案比我预期的要简单得多。感谢@LoicTheAztec 的提示,我利用 Loic 的代码进行更好的优化。
解决方法是检查用户是否为 admin
if ( is_admin() ) {
return $available_gateways;
}
然后把剩下的代码放到else里。
这是完整的解决方案:
add_filter( 'woocommerce_available_payment_gateways', 'categories_unset_cheque_gateway', 99, 1 );
function categories_unset_cheque_gateway( $gateways ){
if (is_admin()){
return $gateways;
}
// BELOW define your categories in the array (can be IDs, slugs or names)
else{
$categories = array( 15 );
// Loop through cart items checking for specific product categories
foreach ( WC()->cart->get_cart() as $cart_item )
if( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ){
unset( $gateways['cheque'] );
break; // Stop the loop
}
return $gateways;
}
}
我试图询问 Customizr 支持我的代码是什么 运行,但他们基本上说他们不支持第三方插件,例如 Woocommerce
我需要根据人们在网站上购买的商品来限制支付类型。 例如,支票付款类型仅适用于购买课程的人。
这是执行此操作的代码:
<?php
add_filter( 'woocommerce_available_payment_gateways',
'threshold_unset_gateway_by_category' );
function threshold_unset_gateway_by_category( $available_gateways ) {
global $woocommerce;
$unset = false;
$category_ids = array( 22, 21, 25, 20);
foreach ( $woocommerce->cart->get_cart() as $key => $values ) {
$terms = get_the_terms( $values['product_id'], 'product_cat' );
foreach ( $terms as $term ) {
if ( in_array( $term->term_id, $category_ids ) ) {
$unset = true;
break;
}
}
}
if ( $unset == true ) unset( $available_gateways['cheque'] );
return $available_gateways;
}
我已经深入研究了 Customizr 文件,但找不到任何冲突。 Wordpress 文件可能有点复杂,所以我可能找错了树。
这个答案并不是为了解决您与 Customizr 的问题,我只是以一种更加轻便、紧凑和灵活的方式重新访问了您的代码。
我还在add_filter()
中添加了一个优先级和参数个数,这有时会解决一些奇怪的问题…
所以在这段代码中,我改用 WP has_term()
条件函数:
add_filter( 'woocommerce_available_payment_gateways', 'categories_unset_cheque_gateway', 99, 1 );
function categories_unset_cheque_gateway( $available_gateways ){
// Not in backend (admin)
if( is_admin() )
return $available_gateways;
// BELOW define your categories in the array (can be IDs, slugs or names)
$categories = array( 20, 21, 22, 25 );
// Loop through cart items checking for specific product categories
foreach ( WC()->cart->get_cart() as $cart_item )
if( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ){
unset( $available_gateways['cheque'] );
break; // Stop the loop
}
return $available_gateways;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。
已测试并有效 (未使用 Customizr Pro 进行测试)。
所以这个问题的解决方案比我预期的要简单得多。感谢@LoicTheAztec 的提示,我利用 Loic 的代码进行更好的优化。 解决方法是检查用户是否为 admin
if ( is_admin() ) {
return $available_gateways;
}
然后把剩下的代码放到else里。 这是完整的解决方案:
add_filter( 'woocommerce_available_payment_gateways', 'categories_unset_cheque_gateway', 99, 1 );
function categories_unset_cheque_gateway( $gateways ){
if (is_admin()){
return $gateways;
}
// BELOW define your categories in the array (can be IDs, slugs or names)
else{
$categories = array( 15 );
// Loop through cart items checking for specific product categories
foreach ( WC()->cart->get_cart() as $cart_item )
if( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ){
unset( $gateways['cheque'] );
break; // Stop the loop
}
return $gateways;
}
}