如何在WooCommerce中根据购物车数量和产品属性对可变和单品应用折扣?

huangapple go评论78阅读模式
英文:

How to apply discount on variable and single products, based on cart quantity and product attribute in WooCommerce?

问题

我正在尝试根据购物车中具有特定产品属性的产品数量来应用百分比折扣。

更具体地说,我的目标是对具有属性“flaske”的至少6件产品的订单应用15%的折扣。

我已经成功地为具有设置变体属性的可变产品实现了这一目标,但似乎无法针对单个/简单产品进行操作。

我的代码如下(从Woocommerce条件定价代码中借鉴):

  1. // 基于产品数量和购物车中的属性计算折扣
  2. add_action('woocommerce_cart_calculate_fees', 'wc_cart_item_quantity_discount');
  3. function wc_cart_item_quantity_discount($cart) {
  4. if (is_admin() && !defined('DOING_AJAX'))
  5. return;
  6. // 初始化变量
  7. $min_item_amount = 6; // 最小数量
  8. $discount = $items_count = $percent = $items_subtotal = 0;
  9. $taxonomy = 'pa_variant'; // 分类法
  10. $term_slugs = array('flaske'); // 术语/术语
  11. // 循环购物车项目
  12. foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
  13. // 循环变量
  14. foreach ($cart_item['variation'] as $attribute => $term_slug) {
  15. // 仅计算大于6且具有属性的项目
  16. if ($cart_item['data']->get_price() >= $min_item_amount && $attribute === 'attribute_' . $taxonomy && in_array($term_slug, $term_slugs)) {
  17. $items_count += $cart_item['quantity'];
  18. $items_subtotal += $cart_item['line_subtotal'];
  19. }
  20. }
  21. }
  22. // 条件百分比
  23. if ($items_count >= 6) {
  24. $percent = 15;
  25. }
  26. // 折扣(应税)
  27. if ($items_count > 0) {
  28. // 计算
  29. $discount -= ($items_subtotal / 100) * $percent;
  30. $cart->add_fee(__('Mix & Match rabat - $percent%', 'woocommerce'), $discount, true);
  31. }
  32. }

我的当前代码非常适用于可变产品(变体),但似乎不影响单个产品,即使我将单个产品赋予与可变产品相同的属性也是如此。

我怀疑这与foreach循环foreach ($cart_item['variation'] as $attribute => $term_slug)有关。

如何使这个代码普遍适用,以便它也适用于具有相同属性“flaske”的单个/简单产品?

将不适用于这些产品的部分删除或注释掉,可能会导致它适用于所有产品。希望对你有所帮助。

英文:

I am trying to apply a percentage discount based on quantity of products with a specific product attribute, in cart.

More precisely my goal is to apply 15% discount on orders of minimum 6 products with the attribute flaske.

I have managed to achieve this for variable products with variation attributes set, but I can't seem to target the single/simple products.

My code so far (borrowed from Condition for Quantity and Price for Woocommerce):

  1. // Discount based on product quantity and attribute in cart
  2. add_action( 'woocommerce_cart_calculate_fees','wc_cart_item_quantity_discount' );
  3. function wc_cart_item_quantity_discount( $cart ) {
  4. if ( is_admin() && ! defined( 'DOING_AJAX' ) )
  5. return;
  6. // INITIALIZING VARIABLES
  7. $min_item_amount = 6; // Min quantity
  8. $discount = $items_count = $percent = $items_subtotal = 0;
  9. $taxonomy = 'pa_variant'; // Taxonomy
  10. $term_slugs = array('flaske'); // Term/terms
  11. // Loop through cart items
  12. foreach( $cart->get_cart() as $cart_item_key => $cart_item ) {
  13. // Loop through variation
  14. foreach( $cart_item['variation'] as $attribute => $term_slug ) {
  15. // Only counting items that are above 6 and has attribute
  16. if( $cart_item['data']->get_price() >= $min_item_amount && $attribute === 'attribute_'.$taxonomy && in_array( $term_slug, $term_slugs ) ) {
  17. $items_count += $cart_item['quantity'];
  18. $items_subtotal += $cart_item['line_subtotal'];
  19. }
  20. }
  21. }
  22. // CONDITIONAL PERCENTAGE
  23. if ($items_count >= 6 ) {
  24. $percent = 15;
  25. }
  26. // DISCOUNT (TAXABLE)
  27. if( $items_count > 0 ) {
  28. // Calculation
  29. $discount -= ($items_subtotal / 100) * $percent;
  30. $cart->add_fee( __( "Mix & Match rabat - $percent%", "woocommerce" ), $discount, true);
  31. }
  32. }

My current code works very well for variable products (the variants), but it doesn't seem to affect single products, even if I give the single products the same attributes as the variable product.

I suspect it has to do with the foreach loop foreach( $cart_item['variation'] as $attribute => $term_slug )

How can I make this work in general, so it also applies for single/simple products with same attribute flaske?

Any help and advice would be appreciated.

Other useful references:

Woocommerce percentage discount per item based on quantity

Exclude variations with 2 specific attribute terms from coupon usage in Woocommerce

答案1

得分: 1

以下是翻译好的代码部分:

  1. add_action('woocommerce_cart_calculate_fees', 'wc_cart_item_quantity_discount');
  2. function wc_cart_item_quantity_discount($cart) {
  3. if (is_admin() && !defined('DOING_AJAX'))
  4. return;
  5. // 初始化变量
  6. $discount = $items_count = $percent = $items_subtotal = 0;
  7. $taxonomy = 'pa_variant'; // 分类
  8. $term_slugs = array('flaske'); // 术语/词汇
  9. // 循环遍历购物车项目
  10. foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
  11. $product = $cart_item['data'];
  12. // 产品变体
  13. if ($product->is_type('variation')) {
  14. // 循环遍历变体属性
  15. foreach ($cart_item['variation'] as $attribute => $term_slug) {
  16. // 只计算数量大于6且具有属性的项目
  17. if ($attribute === 'attribute_' . $taxonomy && in_array($term_slug, $term_slugs)) {
  18. $items_count += $cart_item['quantity'];
  19. $items_subtotal += $cart_item['line_subtotal'];
  20. }
  21. }
  22. }
  23. // 简单产品
  24. elseif ($product->is_type('simple')) {
  25. $attributes = $product->get_attributes();
  26. if (!empty($attributes) && array_key_exists($taxonomy, $attributes)) {
  27. $terms = (array) $attributes[$taxonomy]->get_terms(); // WP_Term对象数组
  28. $slugs = array_map(function($term) { return $term->slug; }, $terms); // 仅提取术语slug
  29. if (count(array_intersect($slugs, $term_slugs)) > 0) {
  30. $items_count += $cart_item['quantity'];
  31. $items_subtotal += $cart_item['line_subtotal'];
  32. }
  33. }
  34. }
  35. }
  36. // 条件百分比
  37. if ($items_count >= 6) {
  38. $percent = 15;
  39. }
  40. // 折扣(应税)
  41. if ($items_count > 0) {
  42. // 计算
  43. $discount -= ($items_subtotal / 100) * $percent;
  44. $cart->add_fee(__("Mix & Match rabat - $percent%", "woocommerce"), $discount, true);
  45. }
  46. }

它应该可以正常工作。

英文:

You don't get the product attributes set in simple or variable products, in the same way as for product variations.

The following revisited code should also handle simple products (untested):

  1. add_action( 'woocommerce_cart_calculate_fees','wc_cart_item_quantity_discount' );
  2. function wc_cart_item_quantity_discount( $cart ) {
  3. if ( is_admin() && ! defined( 'DOING_AJAX' ) )
  4. return;
  5. // INITIALIZING VARIABLES
  6. $discount = $items_count = $percent = $items_subtotal = 0;
  7. $taxonomy = 'pa_variant'; // Taxonomy
  8. $term_slugs = array('flaske'); // Term/terms
  9. // Loop through cart items
  10. foreach( $cart->get_cart() as $cart_item_key => $cart_item ) {
  11. $product = $cart_item['data'];
  12. // Product variations
  13. if( $product->is_type('variation') ) {
  14. // Loop through variation attributes
  15. foreach ($cart_item['variation'] as $attribute => $term_slug) {
  16. // Only counting items that are above 6 and has attribute
  17. if ($attribute === 'attribute_' . $taxonomy && in_array($term_slug, $term_slugs)) {
  18. $items_count += $cart_item['quantity'];
  19. $items_subtotal += $cart_item['line_subtotal'];
  20. }
  21. }
  22. }
  23. // Simple products
  24. elseif ( $product->is_type('simple') ) {
  25. $attributes = $product->get_attributes();
  26. if( ! empty($attributes) && array_key_exists($taxonomy, $attributes) ) {
  27. $terms = (array) $attributes[$taxonomy]->get_terms(); // array of WP_Term objects
  28. $slugs = array_map(function($term) { return $term->slug; }, $terms); // Extract only the term slugs
  29. if (count( array_intersect($slugs, $term_slugs) ) > 0 ) {
  30. $items_count += $cart_item['quantity'];
  31. $items_subtotal += $cart_item['line_subtotal'];
  32. }
  33. }
  34. }
  35. }
  36. // CONDITIONAL PERCENTAGE
  37. if ($items_count >= 6) {
  38. $percent = 15;
  39. }
  40. // DISCOUNT (TAXABLE)
  41. if ($items_count > 0) {
  42. // Calculation
  43. $discount -= ($items_subtotal / 100) * $percent;
  44. $cart->add_fee(__("Mix & Match rabat - $percent%", "woocommerce"), $discount, true);
  45. }
  46. }

It should work.

huangapple
  • 本文由 发表于 2023年7月18日 01:30:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706825.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定