Php Customizer Pro编辑菜单问题,带有一些自定义代码
我试图向Customer support询问我的代码运行到了什么地方,但他们基本上说他们不支持第三方插件,比如Woocommerce 我需要根据人们在网站上购买的东西来限制付款类型。 例如,支票付款类型仅适用于购买课程的人 以下是执行此操作的代码:Php Customizer Pro编辑菜单问题,带有一些自定义代码,php,wordpress,woocommerce,themes,payment-gateway,Php,Wordpress,Woocommerce,Themes,Payment Gateway,我试图向Customer support询问我的代码运行到了什么地方,但他们基本上说他们不支持第三方插件,比如Woocommerce 我需要根据人们在网站上购买的东西来限制付款类型。 例如,支票付款类型仅适用于购买课程的人 以下是执行此操作的代码: <?php add_filter( 'woocommerce_available_payment_gateways', 'threshold_unset_gateway_by_category' ); function threshold_
<?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;
}
这个答案并不是为了解决您与Customizer之间的问题,我只是以一种更加轻巧、紧凑和灵活的方式重新审视了您的代码
我还在add_filter()
中添加了优先级和参数数量,这有时会解决一些奇怪的问题
因此,在这段代码中,我使用WPhas_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文件
经过测试且正常工作(未经Customizer Pro测试)。因此解决方案比我预期的要简单得多。感谢@LoicTheAztec提供的提示,我利用了Loic的代码进行了更好的优化。
修复方法是检查用户是否是管理员
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;
}
}
这有帮助,但没有解决问题,如问题中所述,为什么现在使用您的方法抑制某些支付网关会破坏您编辑菜单的能力?@dsmtorboawd抱歉,但我没有,也没有使用Customizer Pro,因此我无法测试它(因为这是一个商业主题)。我希望你能找到解决这个问题的办法。