Bedingt festgelegte Warenkorbartikelpreise für bestimmte Produkte in WooCommerce 3

Lesezeit: 4 Minuten

Benutzer-Avatar
Mujtaba K

In WooCommerce verwende ich einen Code aus dieser Antwort:
Stellen Sie den Artikelpreis des WooCommerce-Einkaufswagens auf Null, wenn das Produkt bereits gekauft wurde

Es funktioniert großartig mit einem Produkt, aber ich würde es gerne zum Laufen bringen mit mehreren Produkten.

Der folgende Code ändert also für ein bestimmtes Produkt den Artikelpreis des Einkaufswagens in $0.00 wenn der Kunde bereits gekauft hat und wenn nicht, ist der Preis festgelegt $100 (für den ersten Kauf):

    add_action( 'woocommerce_before_calculate_totals', 'conditionally_change_cart_items_price', 10, 1 );
function conditionally_change_cart_items_price( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;


    $targeted_product_id = 1107;

    // Set Here your custom price (1st purshase)
    $custom_price = 100; // First purshase for product ID 1092

    // Detecting if customer has already bought The targeted product (1092)
    if( is_user_logged_in() ){
        $customer      = wp_get_current_user();
        $customer_id   = $customer->ID; // customer ID
        $customer_email = $customer->email; // customer email

        if( wc_customer_bought_product( $customer_email, $customer_id, $targeted_product_id) )
            $custom_price = 0; // Set to 0 for other purchases (product ID 1092)
    }

    foreach ( $cart_object->get_cart() as $cart_item ) {
        // When targeted product is in cart we change the price
        if ( $cart_item['product_id'] == $targeted_product_id ) {
            // Woocommerce 3+ compatibility
            if ( version_compare( WC_VERSION, '3.0', '<' ) )
                $cart_item['data']->price = $custom_price;
            else
                $cart_item['data']->set_price( $custom_price );
        }
    }
}

Wie könnte ich dafür sorgen, dass es für mehrere definierte Produkte funktioniert (statt für eines)?

Sie müssen ein Array mit allen Zielprodukt-IDs erstellen und dann sollten Sie prüfen, ob ein Kunde dieses Produkt bereits in der foreach-Schleife gekauft hat:

add_action( 'woocommerce_before_calculate_totals', 'conditionally_change_cart_items_price', 10, 1 );

function conditionally_change_cart_items_price( $cart_object ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // add as many ids as you need
    $targeted_product_ids = array( 1107, 1108, 1109 );

    // Set Here your custom price (1st purshase)
    $custom_price = 100; // First purshase for target product


    if ( is_user_logged_in() ) {
        $customer       = wp_get_current_user();
        $customer_id    = $customer->ID; // customer ID
        $customer_email = $customer->email; // customer email

        foreach ( $cart_object->get_cart() as $cart_item ) {
            // When targeted product is in cart we change the price
            if ( in_array( $cart_item['product_id'], $targeted_product_ids ) ) {

                // Detecting if customer has already bought The targeted product
                if ( wc_customer_bought_product( $customer_email, $customer_id, $targeted_product_id ) )
                    // Set to 0 for other purchases
                    $custom_price = 0;

                // Woocommerce 3+ compatibility
                if ( version_compare( WC_VERSION, '3.0', '<' ) )
                    $cart_item['data']->price = $custom_price;
                else
                    $cart_item['data']->set_price( $custom_price );
            }
        }
    }
}

Benutzer-Avatar
LoicTheAztec

Basierend auf: Stellen Sie den Artikelpreis des Woocommerce-Einkaufswagens auf Null, wenn das Produkt bereits gekauft wurde

Sie können es für mehrere definierte Produkt-IDs verwenden (in einem Array) Hier entlang:

// For WooCommerce version 3 and above (only)
add_action( 'woocommerce_before_calculate_totals', 'conditionally_change_cart_items_prices', 10, 1 );
function conditionally_change_cart_items_prices( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) 
        return;

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

    // Set HERE the Products to check in this array
    $targeted_product_ids = array( 37, 50, 89, 124, 327 );
    $products_bought = array();

    // Set Here your custom price (1st purshase)
    $custom_price1 = 100; // First purshase
    // Set Here your custom price (more purshases than first)
    $custom_price2 = 0; // Next purshases

    // Detecting if customer has already bought our targeted products
    if( is_user_logged_in() ){
        $customer      = wp_get_current_user();
        $customer_id   = $customer->ID; // customer ID
        $customer_email = $customer->email; // customer email

        // Checking each product in the array
        foreach( $targeted_product_ids as $product_id ){
            if( wc_customer_bought_product( $customer_email, $customer_id, $product_id) ){
                // We set all (targeted) purchased products in an array
                $products_purchased[] = $product_id;
            }
        }
    }

    // Checking cart items and changing item prices if needed
    foreach ( $cart->get_cart() as $cart_item ) {
        // When a targeted products already purchased is in cart we set price 2
        if ( in_array( $cart_item['product_id'], $products_purchased ) ) {
            $cart_item['data']->set_price( $custom_price2 );
        } 
        // When a targeted products is in cart and has not been purchased we set price 1
        elseif ( in_array( $cart_item['product_id'], $targeted_product_ids ) ) {
            $cart_item['data']->set_price( $custom_price1 );
        }
    }
}

Code geht in die function.php-Datei Ihres aktiven Child-Themes (oder Themes) oder auch in jede Plugin-Datei.

Dieser Code ist getestet und funktioniert in WooCommerce 3+

  • Wenn ich 5 Produkte aus einer bestimmten Kategorie auswähle, wird eine Gebühr von 100 $ für alle Produkte im Warenkorb erhoben. Ich möchte es auf 100 $ statt 500 $ für die erste Bestellung und dann auf 0,00 $ bei den nächsten Bestellungen begrenzen.

    – Mujtaba K

    1. Oktober 2017 um 12:37 Uhr

  • @MujtabaK 1) Sie haben nicht nach einer Produktkategorie gefragt, sondern nach einer Reihe von Produkt-IDs… 2) Es ist nicht möglich, nach einer Produktkategorie zu suchen… 3) Ihre Frage ist nicht klar genug, da Sie viele Produkte in verschiedenen Kategorien haben können Mengen, also müssen Sie in allen Fällen die Preise in einigen expliziten Beispielen erläutern.

    – LoicTheAztec

    2. Oktober 2017 um 0:58 Uhr

1011260cookie-checkBedingt festgelegte Warenkorbartikelpreise für bestimmte Produkte in WooCommerce 3

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

Privacy policy