Home WordpressWordPress cơ bản Thêm tính năng triết khấu trong WooCommerce

Thêm tính năng triết khấu trong WooCommerce

by admincp

Trong hướng dẫn này, chúng ta sẽ tìm hiểu cách áp dụng giảm giá hàng loạt trong Woocommerce. Có nhiều cách để giảm giá cho khách hàng. Trong bài này, chúng tôi sẽ làm điều này với cách đơn giản nhất có thể. Một trong số cách đơn giản là áp dụng chiết khấu hàng loạt tùy thuộc vào tổng số giỏ hàng. VD giảm giá 5% cho các đơn hàng từ 500k trở lên và 10% cho các đơn hàng từ 1000k trở lên.

Để thực hiện bạn thêm các dòng mã sau vào cuối tệp functions.php vào giao diện WordPress của bạn:

add_action('woocommerce_cart_calculate_fees', 'tl_apply_bulk_discount');
function tl_apply_bulk_discount() {
  global $woocommerce;
  $excluded_amount = $discount_percent = 0;
  $working_total   = $woocommerce->cart->cart_contents_total;
  $excluded_categories = array(
    217, # Training
    223, # Starter Kits
  );
 
  # Only apply manual discount if no coupons are applied
  if (!$woocommerce->cart->applied_coupons) {
 
    # Find any items in cart that belong to the restricted categories
    foreach ($woocommerce->cart->cart_contents as $item) {
      $product_categories = get_the_terms($item['product_id'], 'product_cat');
      if (empty($product_categories) || is_wp_error($product_categories) || !$product_categories) {
        if (is_wp_error($product_categories)) {
          wp_die($product_categories->get_error_message());
        }
        else {
          $product_categories = new WP_Error('no_product_categories', "The product "".$item->post_title."" doesn't have any categories attached, thus no discounts can be calculated.", "Fatal Error");
          wp_die($product_categories);
        }
      }
      foreach ($excluded_categories as $excluded_category) {
        foreach ($product_categories as $category) {
          if ($excluded_category == $category->term_id) {
            $excluded_amount += $item['line_subtotal']; # Increase our discounted amount
            $working_total -= $item['line_subtotal'];   # Decrease our discounted amount
          }
        }
      }
    }
 
    # Logic to determine WHICH discount to apply based on subtotal
    if ($working_total >= 500 && $working_total < 1000) {
      $discount_percent = 5;
    }
    elseif ($working_total >= 1000) {
      $discount_percent = 10;
    }
    else {
      $discount_percent = 0;
    }
 
    # Make sure cart total is eligible for discount
    if ($discount_percent > 0) {
      $discount_amount  = ( ( ($discount_percent/100) * $working_total ) * -1 );
      $woocommerce->cart->add_fee('Bulk Discount', $discount_amount);
    }
  }
}

Như vậy, khi tổng số giỏ hàng từ 500 trở lên, sẽ áp dụng chiết khấu 5% trên tổng giá trị giỏ hàng. Bạn có thể thay đổi số tiền và tỷ lệ phần trăm chiết khấu theo nhu cầu của bạn.

Chúc bạn thành công

Để nhận được bài viết mới vui lòng đăng ký kênh kiến thức WordPress từ A-Z ở Form bên dưới. Bạn cũng có thể nhận được sự trợ giúp trên TwitterFacebook

Liên hệ

Công ty chuyên Thiết kế website uy tín nhất Miền Bắc: http://vinastar.net

Hotline tư vấn: 0989 48 3456

Nguồn: Sưu tầm trên internet

You may also like

Leave a Comment