Я пытаюсь деактивировать PayPal для всех подкатегорий с помощью заданного сегмента категории для WooCommerce. В настоящее время мой следующий код просто деактивирует способ оплаты для одной категории. Можно ли деактивировать для всех подкатегорий?
<?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 можно создавать динамические веб-сайты и веб-приложения.
Обновление 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 активной дочерней темы (или активной темы). Проверено и работает.
Для других с той же проблемой. Код @LoicTheAztec был расширен частью кода, чтобы проверить, является ли сайт с одним продуктом частью категории:
* 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;
}