简体   繁体   English

Woocommerce中基于状态和产品类别的累进购物车项目费用

[英]Progressive cart item fee based on state and on product category in Woocommerce

Recently I tryied to use 2 sniped codes in one for this Woocommerce project where I need to set a fee for a specific product category (term ID: 19) and and a specific country state (Florida 'FL')最近,我尝试在这个 Woocommerce 项目中使用 2 个狙击代码,我需要为特定产品类别(术语 ID:19)和特定国家/地区(佛罗里达州“FL”)设置费用

Additionally I should need to multiply this fee rate by the items from this product category (19).此外,我需要将此费率乘以该产品类别 (19) 中的项目。

This is my actual code:这是我的实际代码:

add_action('woocommerce_cart_calculate_fees','woocommerce_custom_surcharge'); 
function woocommerce_custom_surcharge() {
    $category_ID = '19'; 

    global $woocommerce;


    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $state  = array('FL');


    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) 
    {
        // 19 is the ID of the category for which we want to remove the payment gateway
        if($term->term_id == $category_ID){
    $surcharge  = 1;

    if ( in_array( WC()->customer->shipping_state, $state && ) ) {
        $woocommerce->cart->add_fee( 'State Tire Fee', $surcharge, true, '' );
    }
}

How can I set a progressive fee based on items from a specific category and customers from a specific state?如何根据特定类别的商品和特定州的客户设置累进费用?

Any help will be appreciated.任何帮助将不胜感激。

There are some mistakes and your code is a bit outdated...有一些错误,您的代码有点过时...
The code below will set a progressive fee based on cart item quantity from a specific product category and for a specific state only.下面的代码将根据特定产品类别和特定状态的购物车项目数量设置累进费用。

add_action( 'woocommerce_cart_calculate_fees', 'wc_custom_surcharge', 20, 1 );
function wc_custom_surcharge( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return; // Exit

    ## Your Settings (below) ##

    $categories      = array(19);
    $targeted_states = array('FL');
    $base_rate       = 1;

    $user_state = WC()->customer->get_shipping_state();
    $user_state = empty($user_state) ? WC()->customer->get_billing_state() : $user_state;
    $surcharge  = 0; // Initializing

    // If user is not from florida we exit
    if ( ! in_array( $user_state, $targeted_states ) )
        return; // Exit

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        if ( has_term( $categories, 'product_cat', $cart_item['product_id'] )  ){
            // calculating fee based on the defined rate and on item quatinty
            $surcharge += $cart_item['quantity'] * $base_rate;
        }
    }

    // Applying the surcharge
    if ( $surcharge > 0 ) {
        $cart->add_fee( __("State Tire Fee", "woocommerce"), $surcharge, true );
    }
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的function.php文件中。 Tested and works.测试和工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 基于 Woocommerce 中特定产品类别购物车项目数量的购物车费用 - Cart fee based on specific product category cart item quantity in Woocommerce WooCommerce 结账费用基于购物车商品数量(不包括类别) - WooCommerce checkout fee based on cart item count with category excluded WooCommerce 产品类别和城市运费 - WooCommerce Product Category and City Based Shipping Fee 根据类别为每个产品向 WooCommerce 添加费用 - Add a fee to WooCommerce per product, based on category 根据 WooCommerce 产品类别禁用特定的购物车项目数量字段 - Disable specific cart item quantity fields based on WooCommerce product category 根据Woocommerce中的产品类别将最大商品数量添加到购物车 - Max item quantity added to cart based on product category in Woocommerce 在WooCommerce中根据产品类别和产品长度申请费用 - Apply fee based on product category and product length in WooCommerce 根据类别,为每种产品向WooCommerce添加百分比费用 - Add a percent fee to WooCommerce per product, based on category 根据产品类别和运输方式添加费用 WooCommerce - Add fee based on product category and shipping method WooCommerce 基于 Woocommerce 中购物车项目数的有条件累进百分比折扣 - Conditional progressive percentage discount based on cart item count in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM