Zusätzliches, kostenloses Warenkorbgeschenk – Snippet

Möchtest Du bei deinem WooCommerce Shop ein kostenloses Geschenk abhängig von der Warenkorb-Summe realisieren? Standardmäßig kann das WooCommerce für WordPress leider nicht, aber mit etwas Code ist dieses Feature schnell implementiert.

In diesem Beitrag zeige ich Dir, wie Du bis zu 4 Geschenke definierst, je nach der Summe des Warenkorbs. Das beste daran: Die Artikel werden natürlich entfernt, sollte sich die Summe des Warenkorbs ändern.

PS: Ist Dir das zu kompliziert, dann kann ich Dir diesen Code in deiner Installation gerne hinterlegen. Buche einfach mein Produkt „WordPress Unterstützung“ oder kontaktiere mich direkt und ich helfe Dir.

Kostenloses Geschenk abhängig von der Summe des Warenkorbs #

Dieses Snippet ermöglicht es Dir für 4 unterschiedliche Gesamtsummen des Warenkorbs, unterschiedliche kostenlose Produkte in den Warenkorb hinzuzufügen. Beachte, dass alle zusätzlichen Artikel als kostenlos definiert werden müssen. Diese können dann durch diesen Code nicht mehr als „normale“ Produkte hinzugefügt werden.

Gewünschtes Endergebnis #

  • Kostenloses Produkt mit der ID 5808 für Warenkorbsummen zwischen EUR 100,00 und 199,99
  • Kostenloses Produkt mit der ID 5183 für Warenkorbsummen zwischen EUR 200,00 und 299,99
  • Kostenloses Produkt mit der ID 4211 für Warenkorbsummen zwischen EUR 300,00 und 499,99
  • Kostenloses Produkt mit der ID 4121 für Warenkorbsummen ab EUR 500,00

Wird einer der erwähnten Artikel (5808, 5183, 4211 oder 4124) in den Warenkorb gelegt wird, ohne dass der benötigte Einkaufswert erreicht wird, ist es nicht möglich, dieses in den Warenkorb zu legen.

Der Code #

Der nachstehende Code muss in Deine functions.php innerhalb von WordPress hinterlegt werden.
Achtung: Ich übernehme keine Haftung, sollte der Code nicht wie gewünscht funktionieren oder ein unerwartetes Verhalten deiner Installation, deines Shops oder anderer Plugins verursachen. Jegliche Verwendung erfolgt auf eigenes Risiko!

Folgende Parameter musst Du anpassen:

$free_product_id_1
$free_product_id_2
$free_product_id_3
$free_product_id_4

Sowie die Preisspanne bei welcher die Geschenke vergeben werden:

$min_subtotal_free_product_1
$min_subtotal_free_product_2
$min_subtotal_free_product_3
$min_subtotal_free_product_4


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

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

    // Free product productIDs
    $free_product_id_1 = 5808;
    $free_product_id_2 = 5183;
    $free_product_id_3 = 4211;
    $free_product_id_4 = 4121;
    
    // Minimum subtotal needed for free products
    $min_subtotal_free_product_1 = 100;
    $min_subtotal_free_product_2 = 200;
    $min_subtotal_free_product_3 = 300;
    $min_subtotal_free_product_4 = 500;

    // Initializing
    $cart_subtotal = 0;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        // When free product is is cart
        if ( $free_product_id_1 == $cart_item['product_id'] ) {
            $free_key_1 = $cart_item_key;
            $free_qty_1 = $cart_item['quantity'];
            // Optionally set the price to zero
            $cart_item['data']->set_price(0);
        } elseif ( $free_product_id_2 == $cart_item['product_id'] ) {
            $free_key_2 = $cart_item_key;
            $free_qty_2 = $cart_item['quantity'];
            // Optionally set the price to zero
            $cart_item['data']->set_price(0);
		} elseif ( $free_product_id_3 == $cart_item['product_id'] ) {
            $free_key_3 = $cart_item_key;
            $free_qty_3 = $cart_item['quantity'];
            // Optionally set the price to zero
            $cart_item['data']->set_price(0);
		} elseif ( $free_product_id_4 == $cart_item['product_id'] ) {
            $free_key_4 = $cart_item_key;
            $free_qty_4 = $cart_item['quantity'];
            // Optionally set the price to zero
            $cart_item['data']->set_price(0);
		} else {
            // NOT empty
            if ( ! empty ( $cart_item['line_total'] ) ) {
                $cart_subtotal += $cart_item['line_total'];
            }

            // NOT empty
            if ( ! empty ( $cart_item['line_tax'] ) ) {
                $cart_subtotal += $cart_item['line_tax'];
            }
        }
    }
    
    // If subtotal is less than first subtotal
    if ( $cart_subtotal < $min_subtotal_free_product_1 ) {
        // Free product 1 is already in cart, remove it
        if ( isset( $free_key_1 ) ) {
            $cart->remove_cart_item( $free_key_1 );
        }
        
        // Free product 2 is already in cart, remove it
        if ( isset( $free_key_2 ) ) {
            $cart->remove_cart_item( $free_key_2 );
        }
		
		// Free product 3 is already in cart, remove it
        if ( isset( $free_key_3 ) ) {
            $cart->remove_cart_item( $free_key_3 );
        }
		
		// Free product 4 is already in cart, remove it
        if ( isset( $free_key_4 ) ) {
            $cart->remove_cart_item( $free_key_4 );
        }
    }
    // If subtotal is between first and second subtotal
    elseif ( $cart_subtotal >= $min_subtotal_free_product_1 && $cart_subtotal < $min_subtotal_free_product_2 ) {
        // Free product 1 is not already in cart, add it
        if ( ! isset( $free_key_1 ) ) {
            $cart->add_to_cart( $free_product_id_1 );
        }
        
        // Free product 2 is in cart, remove it
        if ( isset( $free_key_2 ) ) {
            $cart->remove_cart_item( $free_key_2 );
        }
		
		 // Free product 3 is in cart, remove it
        if ( isset( $free_key_3 ) ) {
            $cart->remove_cart_item( $free_key_3 );
        }
		
		 // Free product 4 is in cart, remove it
        if ( isset( $free_key_4 ) ) {
            $cart->remove_cart_item( $free_key_4 );
        }
    }
    // If subtotal is between second and third subtotal
	elseif ( $cart_subtotal >= $min_subtotal_free_product_2 && $cart_subtotal < $min_subtotal_free_product_3 ) {
    
        // Free product 1 is already in cart, remove it
        if ( isset( $free_key_1 ) ) {
            $cart->remove_cart_item( $free_key_1 );
        }
        
        // Free product 2 is not already in cart, add it
        if ( ! isset( $free_key_2 ) ) {
            $cart->add_to_cart( $free_product_id_2 );
        }
		
		 // Free product 3 is already in cart, remove it
        if ( isset( $free_key_3 ) ) {
            $cart->remove_cart_item( $free_key_3 );
        }
		 // Free product 4 is already in cart, remove it
        if ( isset( $free_key_4 ) ) {
            $cart->remove_cart_item( $free_key_4 );
        }
    } 
	
// If subtotal is between third and fourth subtotal
	elseif ( $cart_subtotal >= $min_subtotal_free_product_3 && $cart_subtotal < $min_subtotal_free_product_4 ) {
    
        // Free product 1 is already in cart, remove it
        if ( isset( $free_key_1 ) ) {
            $cart->remove_cart_item( $free_key_1 );
        }
        
       // Free product 2 is already in cart, remove it
        if ( isset( $free_key_2 ) ) {
            $cart->remove_cart_item( $free_key_2 );
        }
		
		// Free product 3 is not already in cart, add it
        if ( ! isset( $free_key_3 ) ) {
            $cart->add_to_cart( $free_product_id_3 );
        }
		
		 // Free product 4 is already in cart, remove it
        if ( isset( $free_key_4 ) ) {
            $cart->remove_cart_item( $free_key_4 );
        }
    } 

	
	 // If subtotal greater than or equal to fourth subtotal
	elseif ( $cart_subtotal >= $min_subtotal_free_product_4 ) {
    
        // Free product 1 is already in cart, remove it
        if ( isset( $free_key_1 ) ) {
            $cart->remove_cart_item( $free_key_1 );
        }
        
        // Free product 2 is already in cart, remove it
        if ( isset( $free_key_2 ) ) {
            $cart->remove_cart_item( $free_key_2 );
        }
		
		 // Free product 3 is already in cart, remove it
        if ( isset( $free_key_3 ) ) {
            $cart->remove_cart_item( $free_key_3 );
        }
		// Free product 4 is not already in cart, add it
        if ( ! isset( $free_key_4 ) ) {
            $cart->add_to_cart( $free_product_id_4 );
        }
    } 

    // Keep free product 1 quantity to 1.
    if ( isset( $free_qty_1 ) && $free_qty_1 > 1 ) {
        $cart->set_quantity( $free_key_1, 1 );
    }
    
    // Keep free product 2 quantity to 1.
    if ( isset( $free_qty_2 ) && $free_qty_2 > 1 ) {
        $cart->set_quantity( $free_key_2, 1 );
    }
	
	// Keep free product 3 quantity to 1.
    if ( isset( $free_qty_3 ) && $free_qty_3 > 1 ) {
        $cart->set_quantity( $free_key_3, 1 );
    }
	// Keep free product 4 quantity to 1.
    if ( isset( $free_qty_4 ) && $free_qty_4 > 1 ) {
        $cart->set_quantity( $free_key_4, 1 );
    }
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );

Als Vorlage für dieses Snippet diente das Snippet von @7uc1f3r „2 different gifts depending on cart amount

Was das hilfreich?