在我的wooocommerce商店中,仅当产品具有类别ID为“ 266”的特定产品类别时,我才想限制并显示付款网关(支票)。现在,我有了这个代码段,但是却相反:它在结帐时针对特定产品类别禁用了网关:
Position
答案 0 :(得分:1)
使用has_term()
WordPress条件函数可以简化代码,使其更有效,方法如下:
add_filter( 'woocommerce_available_payment_gateways', 'filter_available_payment_gateways' );
function filter_available_payment_gateways( $available_gateways ) {
// Only on checkout page
if ( is_checkout() ) {
// Here define your product categories
$product_categories = array( 't-shirts', 'posters' ); // Can be term names, term slugs or term ids
$payment_method = 'cheque'; // Here define your payment method id to be removed
$hide_payment = false;
// Loop through cart items
foreach ( WC()->cart->get_cart_contents() as $item ) {
if ( ! has_term( $product_categories, 'product_cat', $item['product_id'] ) ) {
$hide_payment = true;
}
}
if ( $hide_payment ) {
unset( $available_gateways[$payment_method] );
}
}
return $available_gateways;
}
代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。
如果您还需要定位父产品类别,那么您将不得不使用它:
// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
// Initializing
$parent_term_ids = $categories_ids = array();
$taxonomy = 'product_cat';
if( is_string( $categories ) ) {
$categories = (array) $categories;
}
$product_id = $product_id > 0 ? get_the_id() : $product_id;
// Convert categories term names and slugs to categories term ids
foreach ( $categories as $category ){
if( is_numeric( $category ) ) {
$categories_ids[] = (int) $category;
} elseif ( term_exists( sanitize_title( $category ), $taxonomy ) ) {
$categories_ids[] = get_term_by( 'slug', sanitize_title( $category ), $taxonomy )->term_id;
}
}
// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
if( $term->parent > 0 ){
$parent_term_ids[] = $term->parent; // Set the parent product category
$parent_term_ids[] = $term->term_id; // (and the child)
} else {
$parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
}
}
return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}
add_filter( 'woocommerce_available_payment_gateways', 'filter_available_payment_gateways' );
function filter_available_payment_gateways( $available_gateways ) {
// Only on checkout page
if ( is_checkout() ) {
// Here define your product categories
$product_categories = array( 't-shirts', 'posters' ); // Can be term names, term slugs or term ids
$payment_method = 'cheque'; // Here define your payment method id to be removed
$hide_payment = false;
// Loop through cart items
foreach ( WC()->cart->get_cart_contents() as $item ) {
if ( ! has_product_categories( $product_categories, $item['product_id'] ) ) {
$hide_payment = true;
}
}
if ( $hide_payment ) {
unset( $available_gateways[$payment_method] );
}
}
return $available_gateways;
}
代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。
答案 1 :(得分:0)
您能否在代码中更改条件
if ( $unset == true ){
$index = 0;
foreach($available_gateways as $single){
if($single != "cheque"){
unset($available_gateways[$index]);
}
$index++;
}
}