Den Checkout für bestimmte Warenkorbartikel in Woocommerce basierend auf Produktkategorien verweigern

Lesezeit: 3 Minuten

Benutzer-Avatar
omukiguy

Basierend auf Checkout nur zulassen, wenn sich ein Produkt einer obligatorischen Kategorie im Warenkorb befindet, habe ich versucht, ein eigenes Codebeispiel zu erstellen, das einen Hinweis ausgibt und das Auschecken verhindert, wenn der Warenkorb nur Produkte in bestimmten Kategorien enthält. Es funktioniert bei der Vorbeugung und Fehlermeldung, aber beim Hinzufügen anderer Produkte verweigert es immer noch den Checkout.

/**
 * Renders a notice and prevents checkout if the cart
 * only contains products in a specific category
 */
//add_action( 'woocommerce_check_cart_items', 'brown_wc_prevent_checkout_for_category' );

function brown_wc_prevent_checkout_for_category() {

    // set the slug of the category for which we disallow checkout
    $categories = array('drinks','extra-accompaniments');

    foreach ($categories as $category => $value) {
        // get the product category
        $product_cat = get_term_by( 'slug', $category, 'product_cat' );

        // sanity check to prevent fatals if the term doesn't exist
        if ( is_wp_error( $product_cat ) ) {
            return;
        }

        // check if this category is the only thing in the cart
        if ( brown_wc_is_category_alone_in_cart( $category ) ) {

            // render a notice to explain why checkout is blocked
            wc_add_notice( sprintf( 'Please choose atleast one bento.', $category ), 'error' );
        }
    }

}

/**
 * Checks if a cart contains exclusively products in a given category
 * 
 * @param string $category the slug of the product category
 * @return bool - true if the cart only contains the given category
 */
function brown_wc_is_category_alone_in_cart( $category ) {

    //When the cart is empty, remove warning message that I have set for when the snippet takes effect
    if (!WC()->cart->is_empty()) {
        // All the code
        // check each cart item for our category
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

            // if a product is not in our category, bail out since we know the category is not alone
            if ( ! has_term( $category, 'product_cat', $cart_item['data']->id ) ) {
                return false;
            }
        }

        // if we're here, all items in the cart are in our category
        return true;

    } else {

        return false;   //  Assume you'd want false here, since the cart is empty

    }

}

Benutzer-Avatar
LoicTheAztec

Es gibt einige Fehler in Ihrem Code, die verhindern, dass er erfolgreich funktioniert. Ihr Code und Ihre Anforderungen sind ganz anders als meine vorherige Antwort.

Sie müssen die erforderliche Schaltflächen-URL und den Text für den Hinweis festlegen, z. B. den Link zur Produktkategorie „bento“ und den Namen der Schaltfläche.

Hier ist der Code:

// Conditional function that check if the non mandatory product categories are in all cart items
function has_not_mandatory_category(){
    // DEFINE HERE the your non mandatory product categories (Ids, slugs or names)
    $categories = array('drinks','extra-accompaniments');

    // Iterrating each item in cart and detecting…
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        $product_id = $cart_item['product_id']; // <== This is the right way (working for product variations too)
        if ( ! has_term( $categories, 'product_cat', $product_id ) )
            return false;
    }
    return true;
}

// Display a message and prevent checkout if the non mandatory product categories are not in all cart items
add_action( 'woocommerce_check_cart_items', 'prevent_checkout_display_notice' ); // for cart and checkout
function prevent_checkout_display_notice() {
    // DEFINE HERE the return button URL and title
    $button_url="#";
    $button_title="Go there";

    // Display message if the non mandatory product categories are not in all cart items
    if ( has_not_mandatory_category() )
        wc_add_notice(
            sprintf( __( 'Please choose at least one bento. <a class="button" href="https://stackoverflow.com/questions/48533752/%s">%s</a>', 'your_theme_domain'),
                $button_url,
                $button_title
            ), 'error'
        );
}

Der Code wird in die function.php-Datei Ihres aktiven untergeordneten Designs (oder aktiven Designs) eingefügt.

Getestet und funktioniert…

Anstatt auf nicht obligatorische Produktkategorien abzuzielen, sollten Sie das Gegenteil tun…

  • Vielen Dank. Das funktioniert sehr gut und hat einen schlankeren Code. PS; Ich habe weniger Artikel, denen ich die Kasse verweigern kann, als sie zuzulassen.

    – omukiguy

    31. Januar 2018 um 9:28 Uhr

1011300cookie-checkDen Checkout für bestimmte Warenkorbartikel in Woocommerce basierend auf Produktkategorien verweigern

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

Privacy policy