1
以下代码来自wordpress + woocommerce安装。它应该检查购物车中的每件物品,如果该物品属于特定的产品类别(类别ID 79 & 130增加15欧元费用,类别80,136,139增加50欧元费用)相应地向类别添加额外费用。为简单起见,我会给他们打电话类别组15€和类别组50€。PHP的foreach代码没有产生效果结果
当一个项目被添加到购物车时,属于类别组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 = '');
}
}
}
}