【问题标题】:woocommerce: disable payment for certain categorieswoocommerce:禁用某些类别的付款
【发布时间】:2015-04-16 23:23:49
【问题描述】:

我需要在我的 woocommerce 网站上禁用某些类别的付款。到目前为止,我已经找到了这个代码,它只适用于 1 个类别。有谁知道如何使这项工作适用于多个类别?谢谢

<?php
/*
 * A simple filter to disable a user-specified payment gateway when a product with a user-specified category is added to the shopping cart
 *  - Note:  If multiple products are added and only one has a matching category, it will remove the payment gateway
 * Requires:
 *    payment_NAME : One of the five hardcoded Woocommerce standard types of payment gateways - paypal, cod, bacs, cheque or mijireh_checkout
 *    category_ID :   The ID of the category for which the gateway above will be removed.  Get the ID by clicking on the category under Products -> Categories and reading the "tag_ID" in the address bar
 *                             i.e. http://ubuntu.humble.lan/wp-admin/edit-tags.php?action=edit&taxonomy=product_cat&tag_ID=20&post_type=product <-- the tag_ID is 20
 * Coded by sean _ at _ techonfoot.com
 * Thanks to boxoft - http://stackoverflow.com/questions/15303031/woocommerce-get-category-for-product-page
 * Usual free code disclaimer - use at your own risk
 * This code was tested against Woocommerce 2.0.8 and WordPress 3.5.1
 */
function filter_gateways($gateways){

$payment_NAME = 'paypal'; // <--------------- change this
$category_ID = '20';  // <----------- and this

 global $woocommerce;

 foreach ($woocommerce->cart->cart_contents as $key => $values ) {
    // Get the terms, i.e. category list using the ID of the product
    $terms = get_the_terms( $values['product_id'], 'product_cat' );
    // Because a product can have multiple categories, we need to iterate through the list of the products category for a match
    foreach ($terms as $term) {
        // 20 is the ID of the category for which we want to remove the payment gateway
        if($term->term_id == $category_ID){
               unset($gateways[$payment_NAME]);
                   // If you want to remove another payment gateway, add it here i.e. unset($gateways['cod']);
                    break;
          }
        break;

【问题讨论】:

    标签: php woocommerce


    【解决方案1】:

    您可以将上面提供的代码修改为以下内容,它应该可以工作:

    function filter_gateways($gateways){
    
        global $woocommerce;
    
        foreach ($woocommerce->cart->cart_contents as $key => $values ) {
    
            // ID(s) of the category we want to remove gateways from
            $category_ids = array( 12,9 );
    
            // Get the terms, i.e. category list using the ID of the product
            $terms = get_the_terms( $values['product_id'], 'product_cat' );
    
            // Because a product can have multiple categories, we need to iterate through the list of the products category for a match
            foreach ($terms as $term) {
    
                if(in_array($term->term_id,$category_ids)){
    
                    // If you have additional items from a non-restricted category in the cart with a restricted category product the only payment option available for all cart items will be the gateway(s) that haven't been unset.
                    // WARNING: If you unset all the gateways users will get a "Sorry, it seems that there are no available payment methods for your state. Please contact us if you require assistance or wish to make alternate arrangements." when trying to checkout!
                    unset($gateways['cod']);
                    unset($gateways['bacs']);
                    unset($gateways['cheque']);
                    break;
                }
            break;
            }
    
        }
        return $gateways;
    }
    add_filter('woocommerce_available_payment_gateways','filter_gateways');
    

    注意:请记住,如果您的购物车中有来自非限制类别的其他商品和受限类别产品,则结帐期间所有购物车商品的唯一付款选项将是网关(s) 尚未取消设置。

    【讨论】:

      猜你喜欢
      • 2017-06-25
      • 1970-01-01
      • 2021-03-30
      • 1970-01-01
      • 2013-05-03
      • 1970-01-01
      • 2022-01-02
      • 2021-10-06
      • 2017-05-31
      相关资源
      最近更新 更多