具有特定产品标签的 WooCommerce 产品的批量动态定价 [英] Bulk dynamic pricing for WooCommerce products with specific product-tag

查看:74
本文介绍了具有特定产品标签的 WooCommerce 产品的批量动态定价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为所有带有bulk-discount"标签的产品添加动态折扣.如果客户购买,我希望折扣发生.5 个相似或不同的带有标签的产品.

I'm trying to add dynamic discount to all products who have the tag: "bulk-discount" I want the discount to happen if a customer buys eg. 5 similar or different prducts with the tag.

我正在使用这个代码.还有这个答案.这就是我所拥有的:

I'm working with this code. And this answer. This is what i have:

add_action( 'woocommerce_before_calculate_totals', 'bbloomer_quantity_based_pricing', 9999 );
 
function bbloomer_quantity_based_pricing( $cart ) {
 
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
 
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
 
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {

    // Get an instance of the WC_Product object
    $product = $cart_item['data'];

    // Get product id
    $product_id = $cart_item['product_id'];

    if( method_exists( $product, 'set_name' ) && has_term( 'bulk-discount', 'product_tag', $product_id ) ) {
        
    // Define discount rules and thresholds
    $threshold1 = 5; // Change price if items > 4
    $discount1 = 0.05; // Reduce unit price by 5%
    $threshold2 = 10; // Change price if items > 9
    $discount2 = 0.1; // Reduce unit price by 10%
 
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
      if ( $cart_item['quantity'] >= $threshold1 && $cart_item['quantity'] < $threshold2 ) {
         $price = round( $cart_item['data']->get_price() * ( 1 - $discount1 ), 2 );
         $cart_item['data']->set_price( $price );
      } elseif ( $cart_item['quantity'] >= $threshold2 ) {
         $price = round( $cart_item['data']->get_price() * ( 1 - $discount2 ), 2 );
         $cart_item['data']->set_price( $price );
      }    
    }
    
 }

推荐答案

我想你是这个意思?

第一个循环计算标签在单个产品或同一产品的多个项目上出现的次数

The first loop counts how many times the tag appears on a single product or multiple items from the same product

如果条件满足,则第二个循环应用折扣

The 2nd loop applies the discount if the condition is met

function bbloomer_quantity_based_pricing( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;

    // count tag found
    $tag_found = 0;

    // Loop through cart items, count how many times tag occurs
    foreach ( $cart->get_cart() as $cart_item ) {
        // Get an instance of the WC_Product object
        $product = $cart_item['data'];

        // Get product id
        $product_id = $cart_item['product_id'];

        // if product has tag
        if( has_term( 'bulk-discount', 'product_tag', $product_id ) ) {

            // Get quantity from product in cart
            $quantity = $cart_item['quantity'];

            // if product quantity > 1
            if ( $quantity > 1) {
                $tag_found = $tag_found + $quantity;
            } else {
                $tag_found += 1;                
            }
        }
    }

    // Define discount rules and thresholds
    $threshold = 5; // Change price if items > 4
    $discount = 0.05; // Reduce unit price by 5%

    // if tag found >= $threshold
    if ( $tag_found >= $threshold ) {
        // Loop through cart items, add discount
        foreach ( $cart->get_cart() as $cart_item ) {
            // Get an instance of the WC_Product object
            $product = $cart_item['data'];

            // Get product id
            $product_id = $cart_item['product_id'];

            // if product has tag
            if( has_term( 'bulk-discount', 'product_tag', $product_id ) ) {
                // calculate new price
                $price = round( $cart_item['data']->get_price() * ( 1 - $discount ), 2 );

                // set new price
                $cart_item['data']->set_price( $price );
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'bbloomer_quantity_based_pricing', 10, 1 );

这篇关于具有特定产品标签的 WooCommerce 产品的批量动态定价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆