以下代码来自wordpress + woocommerce安装。它应该检查购物车中的每个项目,如果该项目属于特定的产品类别(类别ID 79和130添加15€费用,类别80,136,139添加50€费用)相应的类别增加额外费用。为简单起见,我将其称为 类别组15€ 和 类别组50€ 。
如果将一件商品添加到购物车,属于15€类别组或类别组50€,则会按预期添加额外费用。
如果将两件商品添加到购物车,属于不同的类别组,则会按预期添加额外费用。
但如果购物车中添加了多个商品,属于同一类别组,则每个类别组只会添加一个费用,这是不可取的。添加的每个项目都应该产生相关的类别组费用。例如。 2类别组15€应增加2项额外费用,3项类别组50€应增加3项额外费用等等。
代码中是否有错误或......?
function woo_add_cart_fee() {
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) {
// 79 is the ID of the category for which we want to remove the payment gateway
if($term->term_id == '79' || $term->term_id == '130'){
$excost = 15;
$woocommerce->cart->add_fee('Εκτύπωση με απλό μελάνι', $excost, $taxable = false, $tax_class = '');
} else if ($term->term_id == '80' || $term->term_id == '139' || $term->term_id == '136'){
$excost = 50;
$woocommerce->cart->add_fee('Μακέτα & εκτύπωση (απλό μελάνι)', $excost, $taxable = false, $tax_class = '');
}
}
}
}
答案 0 :(得分:0)
您需要使每个费用名称都是唯一的。
$ name - (字符串)required - 费用的唯一名称。 无法添加同名的多项费用。
这是一种方法:
$woocommerce->cart->add_fee('Μακέτα & εκτύπωση (απλό μελάνι) ' . $i++, $excost, $taxable = false, $tax_class = '');