Dynamische Massenpreise für WooCommerce-Produkte mit einem bestimmten Produkt-Tag

Lesezeit: 2 Minuten

Dynamische Massenpreise fur WooCommerce Produkte mit einem bestimmten Produkt Tag
sehr klein

Ich versuche, allen Produkten mit dem Tag “Massenrabatt” einen dynamischen Rabatt hinzuzufügen. Ich möchte, dass der Rabatt gewährt wird, wenn ein Kunde z. 5 ähnliche oder unterschiedliche Produkte mit dem Tag.

Ich arbeite mit Das Code. Und diese Antwort. Das ist, was ich habe:

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

1647361145 276 Dynamische Massenpreise fur WooCommerce Produkte mit einem bestimmten Produkt Tag
7uc1f3r

Die erste Schleife zählt, wie oft das Tag auf einem einzelnen Produkt oder auf mehreren Artikeln desselben Produkts erscheint

Die 2. Schleife wendet den Rabatt an, wenn die Bedingung erfüllt ist

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

Verwandt: Mehrere dynamische Massenpreise für WooCommerce-Produkte mit einem bestimmten Produkt-Tag

1004550cookie-checkDynamische Massenpreise für WooCommerce-Produkte mit einem bestimmten Produkt-Tag

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy