根据产品类别向 woocommerce 添加费用

问题描述 投票:0回答:1

我有一家商店的类别使用“虚拟”产品类型以避免运输。但是,每个包含该类别产品的订单都需要收取服务费。

通过各种搜索,我找到了不同的方式来全局添加费用,或根据数量添加费用,并尝试使用它来通过类别进行定位,但似乎无法正确完成。

function woo_add_cart_fee() {
global $woocommerce;
//Getting Cart Contents. 
$cart = $woocommerce->cart->get_cart();
//find product category
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
    $_product = $values['data'];
        $terms = get_the_terms( $_product->id, 'product_cat' );
    }
  if ($_categoryid == 23){
      $excost = 6;
  } 
  elseif ($_categoryid == 14){
      $excost = 0;
  }     

$woocommerce->cart->add_fee('Service Charge', $excost, $taxable = false, $tax_class = '');
}   
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
php wordpress woocommerce cart
1个回答
0
投票

原始答案(2014年)

©dothedew(用户编辑问题并在当时的问题中回答)

<?php
// Add Service Fee to Category
function woo_add_cart_fee() {

    $category_ID = '23';
    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) {
            // 23 is the ID of the category for which we want to remove the payment gateway
            if($term->term_id == $category_ID){
                $excost = 6;
            }
        }
        $woocommerce->cart->add_fee('Service Charge', $excost, $taxable = false, $tax_class = '');
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );

我的答案(2023年)

我使用 WooCommerce 3.x 语法对代码进行了一些更新:

  • 我没有使用全局变量 $woocommerce,而是使用 WC() 来获取 WooCommerce 实例。这是访问 WooCommerce 中的购物车和其他对象的更现代的方式。
  • 我已经改变了我们使用 WC()->cart->get_cart() 而不是 $woocommerce->cart->cart_contents 循环遍历购物车项目的方式。
  • 我正在使用 $category_slug 变量而不是 $category_ID。这是一种更加用户友好的识别类别的方式,并且比 ID 更不容易更改。
  • 我已经移动
    add_fee
    在循环外

答案:

<?php
/**
 * Add service fee to a specific category
 */
function woo_add_cart_fee() {
    // Define the category slug and service fee amount
    $category_slug      = 'category-slug';

    $product_count = 0;

    // Loop through each item in the cart
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        // Get the product ID and its categories
        $product_id          = $cart_item['product_id'];
        $product_categories  = get_the_terms( $product_id, 'product_cat' );

        // Check if the product belongs to the specified category
        if ( ! empty( $product_categories ) ) {
            foreach ( $product_categories as $category ) {
                if ( $category_slug === $category->slug ) {
                    $product_count++;
                }
            }
        }
    }

    if ( $product_count > 0 ) {
        $service_fee = 6 * $product_count;
        // Add the service fee to the cart
        WC()->cart->add_fee( __( 'Service Charge', 'woocommerce' ), $service_fee, false );
    }
}
// Add the fee calculation function to the cart
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
© www.soinside.com 2019 - 2024. All rights reserved.