Php 对给定顶级类别的所有子类别禁用特定付款方式
我正试图通过给定的WooCommerce类别slug停用所有子类别的PayPal。目前,我下面的代码只是停用了一个类别的付款方式。是否可以停用所有子类别Php 对给定顶级类别的所有子类别禁用特定付款方式,php,wordpress,woocommerce,payment-gateway,taxonomy-terms,Php,Wordpress,Woocommerce,Payment Gateway,Taxonomy Terms,我正试图通过给定的WooCommerce类别slug停用所有子类别的PayPal。目前,我下面的代码只是停用了一个类别的付款方式。是否可以停用所有子类别 <?php /** * Disable payment gateway based on category. */ function ace_disable_payment_gateway_category( $gateways ) { // Categories that'll disable the payment gat
<?php
/**
* Disable payment gateway based on category.
*/
function ace_disable_payment_gateway_category( $gateways ) {
// Categories that'll disable the payment gateway
$category_slugs = array( 'tobacco' );
$category_ids = get_terms( array( 'taxonomy' => 'product_cat', 'slug' => $category_slugs, 'fields' => 'ids' ) );
// Check each cart item for given category
foreach ( WC()->cart->get_cart() as $item ) {
$product = $item['data'];
if ( $product && array_intersect( $category_ids, $product->get_category_ids() ) ) {
unset( $gateways['ppec_paypal'] );
break;
}
}
return $gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'ace_disable_payment_gateway_category' );
更新2
要禁用顶级产品类别子类别的特定支付网关,请使用以下命令:
add_filter( 'woocommerce_available_payment_gateways', 'disable_payment_gateway_subcategory' );
function disable_payment_gateway_subcategory( $payment_gateways ) {
if ( is_admin() ) return $payment_gateways; // Not on admin
$taxonomy = 'product_cat';
$term_slug = 'tobacco'; // Main top category
$term_id = get_term_by( 'slug', $term_slug, $taxonomy )->term_id; // get term Id
$children_ids = get_term_children( $term_id, $taxonomy ); // Get all children terms Ids
array_unshift( $children_ids, $term_id ); // Adding main term Id to the array
// Check each cart item for given subcategories of a top category
foreach ( WC()->cart->get_cart() as $cart_item ) {
$term_ids = wp_get_post_terms( $cart_item['product_id'], $taxonomy, array('fields' => 'ids') );
if ( array_intersect( $children_ids, $term_ids ) ) {
unset($payment_gateways['ppec_paypal']);
break; // stop the loop
}
}
return $payment_gateways;
}
代码进入活动子主题(或活动主题)的functions.php文件。已测试并有效。适用于其他具有相同问题的用户@LOICTHAZTEC代码由代码部分扩展,以检查单个产品站点是否也是该类别的一部分:
* Disable payment gateway based on category.
*/
add_filter( 'woocommerce_available_payment_gateways', 'disable_payment_gateway_subcategory' );
function disable_payment_gateway_subcategory( $payment_gateways ) {
if ( is_admin() ) return $payment_gateways; // Not on admin
$taxonomy = 'product_cat';
$term_slug = 'tobacco'; // Main top category
$term_id = get_term_by( 'slug', $term_slug, $taxonomy )->term_id; // get term Id
$children_ids = get_term_children( $term_id, $taxonomy ); // Get all children terms Ids
array_unshift( $children_ids, $term_id ); // Adding main term Id to the array
// Check on single product site
$cateID = wp_get_post_terms( get_the_ID(), $taxonomy, array('fields' => 'ids') );
if ( array_intersect( $children_ids, $cateID ) ) {
unset($payment_gateways['ppec_paypal']);
}
// Check each cart item for given subcategories of a top category
foreach ( WC()->cart->get_cart() as $cart_item ) {
$term_ids = wp_get_post_terms( $cart_item['product_id'], $taxonomy, array('fields' => 'ids') );
if ( array_intersect( $children_ids, $term_ids ) ) {
unset($payment_gateways['ppec_paypal']);
break; // stop the loop
}
}
return $payment_gateways;
}
谢谢,但不幸的是它不起作用。将付款方式改为“bacs”也一样。我想这只适用于结帐或购物车?在单一产品页面上也应禁用此选项。单一产品页面上也会激活快速PayPal结账。对不起,我不想占用你的时间,但我对PHP没有你熟悉,通常我是Java开发人员。也许你能救我一天:)哥们!!!它现在工作得很好。非常感谢你!我会接受答案。但还有一个问题:如果我添加了一项“烟草”并去结帐,我的服务器日志会输出一个“408”错误代码,而没有任何进一步的信息——它来自您的代码。有什么想法吗?不过,谢谢你抽出时间@OzzyB。我不明白……你的意思是,如果你添加一个类别为“tobbaco”的项目,你会得到一个错误?我刚刚更新了代码,将“烟草”术语Id包含到术语Id数组中…使用代码(之前的代码也是),WooCommerce存在一些问题。我无法打开WooCommerce仪表板和客户。其他一切都没有问题。这是服务器的输出: