Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在电子商务签出中基于分类术语限制支付网关_Php_Wordpress_Woocommerce_Checkout_Payment Method - Fatal编程技术网

Php 在电子商务签出中基于分类术语限制支付网关

Php 在电子商务签出中基于分类术语限制支付网关,php,wordpress,woocommerce,checkout,payment-method,Php,Wordpress,Woocommerce,Checkout,Payment Method,在我的WooCommerce商店中,仅当产品具有类别ID为“266”的特定产品类别时,我才想限制并显示支付网关(支票)。现在我有了这个片段,但它的作用正好相反——它禁用了特定产品类别的签出上的网关: add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_unset_gateway_by_category' ); function bbloomer_unset_gateway_by_category( $availab

在我的WooCommerce商店中,仅当产品具有类别ID为“266”的特定产品类别时,我才想限制并显示支付网关(支票)。现在我有了这个片段,但它的作用正好相反——它禁用了特定产品类别的签出上的网关:

add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_unset_gateway_by_category' );
  
function bbloomer_unset_gateway_by_category( $available_gateways ) {
    if ( is_admin() ) return $available_gateways;
    if ( ! is_checkout() ) return $available_gateways;
    $unset = false;
    $category_ids = array( 8, 37 );
    foreach ( WC()->cart->get_cart_contents() 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;
}

您能在代码中更改您的条件吗

if ( $unset == true ){
     $index = 0;
    foreach($available_gateways as $single){
        if($single != "cheque"){
            unset($available_gateways[$index]);
        }   
        $index++;
    }
 }
使用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() && ! is_wc_endpoint_url() ) {
        // Here define your product categories
        $product_categories = array( 't-shirts', 'posters' ); // Can be term names, term slugs or term ids
        $taxonomy = 'product_cat'; // For WooCommerce product category terms (or 'product_tag' for WooCommerce product tag terms)

        $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, $taxonomy, $item['product_id'] ) ) {
                $hide_payment = true;
            }
        }
        
        if ( $hide_payment ) {
            unset( $available_gateways[$payment_method] );
        }
    }
    return $available_gateways;
}
代码进入活动子主题(或活动主题)的functions.php文件。测试和工作


处理产品标签 只需将代码中的分类法
'product\u cat'
替换为
'product\u tag'


也瞄准母产品类别 如果您也需要针对母产品类别,则必须使用此选项:

// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
    $parent_term_ids = $categories_ids = array(); // Initializing
    $taxonomy        = 'product_cat';
    $product_id      = $product_id == 0 ? get_the_id() : $product_id;

    if( is_string( $categories ) ) {
        $categories = (array) $categories; // Convert string to array
    }

    // Convert categories term names and slugs to categories term ids
    foreach ( $categories as $category ){
        $result = (array) term_exists( $category, $taxonomy );
        if ( ! empty( $result ) ) {
            $categories_ids[] = reset($result);
        }
    }

    // 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() && ! is_wc_endpoint_url() ) {
        // Here define your product categories
        $product_categories = array( 't-shirts', 'posters' ); // Can be term names, term slugs or term ids
        $taxonomy = 'product_cat'; // For WooCommerce product category terms (or 'product_tag' for WooCommerce product tag terms)

        $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;
}
代码进入活动子主题(或活动主题)的functions.php文件。测试和工作