本文介绍了Woocommerce中基于州和产品类别的累进购物车项目费用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

最近,我在这个Woocommerce项目中尝试使用两个代码段,其中我需要为特定的产品类别(条款ID:19)和特定的国家/地区州(佛罗里达州'FL')设置费用

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')

此外,我还需要将此费用乘以该产品类别中的商品(19).

Additionally I should need to multiply this fee rate by the items from this product category (19).

这是我的实际代码:

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?

任何帮助将不胜感激.

推荐答案

有一些错误,您的代码有些过时了...
下面的代码将根据特定产品类别中的购物车商品数量并仅针对特定状态来设置累进费用.

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 );
    }
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于Woocommerce中基于州和产品类别的累进购物车项目费用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 04:51