WooCommerce 根据特定产品的购买数量收取额外费用
WooCommerce additional fees based on purchased quantity for specific products
这是我目前拥有的代码:
add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');
function add_custom_fees( $cart ){
if (is_admin() && !defined('DOING_AJAX')) {
return;
}
$pizza_ice = 0;
$pizza_materials = 0;
$gravy_ice = 0;
$gravy_materials = 0;
$pizza_burger = 293;
$gravy = 462;
$pizza_in_cart = false;
$gravy_in_cart = false;
foreach( $cart->get_cart() as $item ){
$product_in_cart = $item['product_id'];
if ( $product_in_cart === $pizza_burger) {
$pizza_in_cart = true;
}
elseif ( $product_in_cart === $gravy) {
$gravy_in_cart = true;
}
}
if ( $pizza_in_cart != false ) {
$pizza_ice += $item['quantity'] * 27;
$pizza_materials += $item['quantity'] * 9.99;
$cart->add_fee( 'Dry Ice - Pizza Burger', $pizza_ice);
$cart->add_fee( 'Shipping Materials - Pizza Burger', $pizza_materials);
}
elseif ( $gravy_in_cart != false ) {
$gravy_ice += $item['quantity'] * 27;
$gravy_materials += $item['quantity'] * 6.99;
$cart->add_fee( 'Dry Ice - Frozen Gravy', $gravy_ice);
$cart->add_fee( 'Shipping Materials - Frozen Gravy', $gravy_materials);
}
elseif ( $pizza_in_cart != false || $gravy_in_cart != false ) {
$pizza_ice += $item['quantity'] * 27;
$pizza_materials += $item['quantity'] * 9.99;
$cart->add_fee( 'Dry Ice - Pizza Burger', $pizza_ice);
$cart->add_fee( 'Shipping Materials - Pizza Burger', $pizza_materials);
$gravy_ice += $item['quantity'] * 27;
$gravy_materials += $item['quantity'] * 6.99;
$cart->add_fee( 'Dry Ice - Frozen Gravy', $gravy_ice);
$cart->add_fee( 'Shipping Materials - Frozen Gravy', $gravy_materials);
}
}
我想做的是:
如果 Pizza Burger 产品在购物车中,则应为每个数量添加干冰费和材料费。
如果购物车中有 Frozen Gravy 产品,则应为每个数量添加干冰费和材料费。
如果 Pizza Burger 和 Frozen Gravy 产品都在购物车中,则应为每个数量添加干冰费用和材料费用。
我做错了什么?
当此代码段处于活动状态时,它仅适用于 Gravy。更改比萨汉堡的数量对新费用没有影响。
您的代码中存在一些错误,请尝试以下操作:
add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');
function add_custom_fees( $cart ){
if (is_admin() && !defined('DOING_AJAX'))
return;
## -- product Ids -- ##
$pizza_id = 293;
$gravy_id = 462;
## -- dry Ice & Materials fee amounts -- ##
$pizza_ice = $gravy_ice = 27;
$pizza_mat = 9.99;
$gravy_mat = 6.99;
$gravy_qty = $pizza_qty = 0; // Initializing
// Loop through cart items
foreach( $cart->get_cart() as $item ){
if ( $item['product_id'] == $pizza_id ) {
$pizza_qty += $item['quantity'];
}
elseif ( $item['product_id'] == $gravy_id ) {
$gravy_qty += $item['quantity'];
}
}
if ( $pizza_qty > 0 ) {
$cart->add_fee( 'Dry Ice - Pizza Burger', $pizza_qty * $pizza_ice );
$cart->add_fee( 'Shipping Materials - Pizza Burger', $pizza_qty * $pizza_mat );
}
if ( $gravy_qty > 0 ) {
$cart->add_fee( 'Dry Ice - Frozen Gravy', $gravy_qty * $gravy_ice );
$cart->add_fee( 'Shipping Materials - Frozen Gravy', $gravy_qty * $gravy_mat );
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。它应该有效。
这是我目前拥有的代码:
add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');
function add_custom_fees( $cart ){
if (is_admin() && !defined('DOING_AJAX')) {
return;
}
$pizza_ice = 0;
$pizza_materials = 0;
$gravy_ice = 0;
$gravy_materials = 0;
$pizza_burger = 293;
$gravy = 462;
$pizza_in_cart = false;
$gravy_in_cart = false;
foreach( $cart->get_cart() as $item ){
$product_in_cart = $item['product_id'];
if ( $product_in_cart === $pizza_burger) {
$pizza_in_cart = true;
}
elseif ( $product_in_cart === $gravy) {
$gravy_in_cart = true;
}
}
if ( $pizza_in_cart != false ) {
$pizza_ice += $item['quantity'] * 27;
$pizza_materials += $item['quantity'] * 9.99;
$cart->add_fee( 'Dry Ice - Pizza Burger', $pizza_ice);
$cart->add_fee( 'Shipping Materials - Pizza Burger', $pizza_materials);
}
elseif ( $gravy_in_cart != false ) {
$gravy_ice += $item['quantity'] * 27;
$gravy_materials += $item['quantity'] * 6.99;
$cart->add_fee( 'Dry Ice - Frozen Gravy', $gravy_ice);
$cart->add_fee( 'Shipping Materials - Frozen Gravy', $gravy_materials);
}
elseif ( $pizza_in_cart != false || $gravy_in_cart != false ) {
$pizza_ice += $item['quantity'] * 27;
$pizza_materials += $item['quantity'] * 9.99;
$cart->add_fee( 'Dry Ice - Pizza Burger', $pizza_ice);
$cart->add_fee( 'Shipping Materials - Pizza Burger', $pizza_materials);
$gravy_ice += $item['quantity'] * 27;
$gravy_materials += $item['quantity'] * 6.99;
$cart->add_fee( 'Dry Ice - Frozen Gravy', $gravy_ice);
$cart->add_fee( 'Shipping Materials - Frozen Gravy', $gravy_materials);
}
}
我想做的是:
如果 Pizza Burger 产品在购物车中,则应为每个数量添加干冰费和材料费。
如果购物车中有 Frozen Gravy 产品,则应为每个数量添加干冰费和材料费。
如果 Pizza Burger 和 Frozen Gravy 产品都在购物车中,则应为每个数量添加干冰费用和材料费用。
我做错了什么?
当此代码段处于活动状态时,它仅适用于 Gravy。更改比萨汉堡的数量对新费用没有影响。
您的代码中存在一些错误,请尝试以下操作:
add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');
function add_custom_fees( $cart ){
if (is_admin() && !defined('DOING_AJAX'))
return;
## -- product Ids -- ##
$pizza_id = 293;
$gravy_id = 462;
## -- dry Ice & Materials fee amounts -- ##
$pizza_ice = $gravy_ice = 27;
$pizza_mat = 9.99;
$gravy_mat = 6.99;
$gravy_qty = $pizza_qty = 0; // Initializing
// Loop through cart items
foreach( $cart->get_cart() as $item ){
if ( $item['product_id'] == $pizza_id ) {
$pizza_qty += $item['quantity'];
}
elseif ( $item['product_id'] == $gravy_id ) {
$gravy_qty += $item['quantity'];
}
}
if ( $pizza_qty > 0 ) {
$cart->add_fee( 'Dry Ice - Pizza Burger', $pizza_qty * $pizza_ice );
$cart->add_fee( 'Shipping Materials - Pizza Burger', $pizza_qty * $pizza_mat );
}
if ( $gravy_qty > 0 ) {
$cart->add_fee( 'Dry Ice - Frozen Gravy', $gravy_qty * $gravy_ice );
$cart->add_fee( 'Shipping Materials - Frozen Gravy', $gravy_qty * $gravy_mat );
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。它应该有效。