小编Ale*_*sov的帖子

根据 WooCommerce 中的购物车金额添加特定的免费礼物

我想添加某种免费礼物,具体取决于 WooCommerce 中的购物车金额。

比方说:

  • 低于 1500 - 无赠品
  • 介于或等于 1500 - 1999 - 添加免费产品 (1)
  • 大于或等于 2000 - 添加另一个免费产品 (2),删除免费产品 (1)

基于在WooCommerce 答案代码中为最小购物车数量添加免费赠品产品,如果我添加 1 个元素,该代码将起作用,但如果我添加更多元素,它将停止工作。

这是我的代码尝试:

// Add free gifted product for specific cart subtotal
add_action('woocommerce_before_calculate_totals', 'check_free_gifted_product');
function check_free_gifted_product($cart)
{
  if (is_admin() && !defined('DOING_AJAX'))
    return;

  // Settings
  $free_product_id   = 158;
  $targeted_subtotal = 1500;
  $targeted_subtotal_max = 2000;

  $cart_subtotal     = 0; // Initializing

  // Loop through cart items (first loop)
  foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
    // When free …
Run Code Online (Sandbox Code Playgroud)

php wordpress product cart woocommerce

4
推荐指数
1
解决办法
2019
查看次数

标签 统计

cart ×1

php ×1

product ×1

woocommerce ×1

wordpress ×1