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

lndjwyie  于 2023-03-22  发布在  WordPress
关注(0)|答案(1)|浏览(172)

因此,我有一个商店的类别,使用“虚拟”的产品类型,以避免航运。然而,有一个服务费,需要添加到每个订单,其中包括该类别的产品。
通过各种搜索,我发现了不同的方式来全球增加费用,或根据数量增加费用,并试图使用它通过类别的目标,但似乎不能得到它的权利。任何帮助将不胜感激。

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

好吧,因为这是我的第一篇文章,所以我暂时不能回答我自己的问题。我终于用下面的片段来解决这个问题,但是如果你发现问题,请随时改进:

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

显然,您可以根据需要更改类别ID和成本。

v9tzhpje

v9tzhpje1#

原始答案(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' );

相关问题