簡體   English   中英

具有特定產品標簽的 WooCommerce 產品的批量動態定價

[英]Bulk dynamic pricing for WooCommerce products with specific product-tag

我正在嘗試為所有具有標簽的產品添加動態折扣:“批量折扣”我希望在客戶購買時發生折扣,例如。 5 個帶有標簽的相似或不同的產品。

我正在使用這段代碼。 這個答案。 這就是我所擁有的:

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

第一個循環計算標簽出現在單個產品或同一產品的多個項目上的次數

如果滿足條件,第二個循環應用折扣

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 產品的多個批量動態定價

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM