gpt4 book ai didi

php - WooCommerce 根据特定产品的购买数量收取额外费用

转载 作者:行者123 更新时间:2023-12-04 08:07:21 28 4
gpt4 key购买 nike

这是我目前拥有的代码:

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);
}

}
我想做的是:
  • 如果比萨汉堡产品在购物车中,则应为每个数量添加干冰费和 Material 费。
  • 如果冷冻肉汁产品在购物车中,则应为每个数量添加干冰费和 Material 费。
  • 如果 Pizza Burger 和 Frozen Gravy 产品都在购物车中,则应为每个数量添加干冰费和 Material 费。

  • 我在这里做错了什么?
    当此代码段处于事件状态时,它仅适用于肉汁。更改比萨汉堡的数量对新费用没有影响。

    最佳答案

    您的代码中有一些错误,请尝试以下操作:

    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 文件中。它应该有效。

    关于php - WooCommerce 根据特定产品的购买数量收取额外费用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66163274/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com