我想向 Woocommerce 添加额外费用。如果在购物车中我们的产品很少具有“consigne”属性,则只有一个 product_ID 的费用正在退回。如何纠正问题?(对不起我不完美的英语:-D)
function woo_add_cart_fee(){
global $woocommerce;
if(is_admin() && !defined('DOING_AJAX'))
return;
//Initialisation of costs
$consigne = 0.5;
//cart
$cart = $woocommerce->cart->get_cart();
foreach($cart as $product)
{
$the_product = get_product($product['product_id']);
//fee - products
$nb_products = $product['quantity'];
$activation_cost += ((float) $the_product->get_attribute('Consigne')) * $nb_products;
}
//intergration fees
if( $activation_cost >= 0 )
{
$woocommerce->cart->add_fee("Consigne", $consigne*$nb_products, true, '');
}
}
add_action('woocommerce_cart_calculate_fees', 'woo_add_cart_fee');