我已经在这里找到了这段代码,它适用于我的 80/90%(见下文)。
当购物车中有类别 ID 349 的产品时,此代码将 60 欧元添加到我的购物车中。当我在购物车为空时将该类别的产品添加到我的购物车时,它工作正常。但是,当我的购物车中已经有来自不同类别的产品,然后我添加类别为 349 的产品时,它不会增加 60 欧元的额外费用。这怎么可能?
function woo_add_cart_fee() {
$category_ID = '349';
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) {
// 349 is the ID of the category for which we want to remove the payment gateway
if($term->term_id == $category_ID){
$excost = 60;
}
}
$woocommerce->cart->add_fee('Extra bezorgkosten kunstgras', $excost, $taxable = false, $tax_class = '');
}
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );