【发布时间】:2021-03-30 18:03:38
【问题描述】:
我正在尝试通过 WooCommerce 的给定类别 slug 为所有子类别停用 PayPal。目前,我的以下代码只是停用了一个类别的付款方式。是否可以为所有子类别停用?
<?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' );
【问题讨论】:
标签: php wordpress woocommerce payment-gateway taxonomy-terms