Mindesbestellmengen durch 3 teilbar ausser bei einem Produkt

  • ich komme da leider nicht weiter und würde mich über jede hilfe freuen...

    in meinem weinshop muss man aus versandgründen immer mindestens 3 weinflaschen bestellen. das klappt auch sehr gut mit dem u. a. code. nun habe ich aber ein produkt zusammengestllt, dass bereits aus 3 flaschen besteht. wenn man nun 3 weine und das Produktpackage bestellt sind das 4 einheiten und somit kann man das ganze im shop nicht bestellen. wie schaffe ich es nun dass das Produktpackage aus der 3er Regel rausgenommen wird?


    hier der code

    // check that cart items quantities totals are in multiples of 3
    add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
    function woocommerce_check_cart_quantities() {
    global $woocommerce;
    $multiples = 3;
    $total_products = 0;
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
    $total_products += $values['quantity'];
    }
    if ( ( $total_products % $multiples ) > 0 )
    $woocommerce->add_error( sprintf( __('Bitte beachten Sie, dass die Bestellmenge aus Versandgründen durch %s teilbar sein muss.', 'woocommerce'), $multiples ) );
    }

    // Limit cart items with a certain shipping class to be purchased in multiple only
    add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities_for_class' );
    function woocommerce_check_cart_quantities_for_class() {
    global $woocommerce;
    $multiples = 3;
    $class = 'bottle';
    $total_products = 0;
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
    $product = get_product( $values['product_id'] );
    if ( $product->get_shipping_class() == $class ) {
    $total_products += $values['quantity'];
    }
    }
    if ( ( $total_products % $multiples ) > 0 )
    $woocommerce->add_error( sprintf( __('Ihre Bestellmenge muss aus Versandgründen durch %s teilbar sein.', 'woocommerce'), $multiples ) );
    }
    ?>

    <?php

    • Anzeige

    Hallo!

    Wenn du gerade an deiner Website arbeitest oder dein aktuelles Hosting überdenkst: Wir betreiben mit NetzLiving eine Hosting-Plattform, die speziell auf Performance, Sicherheit und einfache Verwaltung ausgelegt ist.

    • ✔️ Schnelle Ladezeiten (optimiert für WordPress & Co.)
    • ✔️ Deutsche Server & DSGVO-konform
    • ✔️ Persönlicher Support (kein 0815-Ticket-System)

    Mehr erfahren

  • Das Paket hat eine post_id. Diese ID nimmst Du mit einem IF gleich zu beginn einer Funktion aus:

    Code
    global $product;
    if ( $product->id == 12345678 ) { 
     return;
    }

    WordPress ist "frei" wie in Freiheit es zu nutzen, aber nicht im Sinne von Freibier. Wer also glaubt man bekommt rund um WordPress alles kostenlos, der irrt. Hilfe ist ein Geschenk für das man sich bedankt, dafür gibt es den 'Gefällt mir' Button. Wer das nicht kann und sich selbst nicht zu helfen weiss, muss sich bezahlte Unterstützung suchen.

  • wie/ wo genau müsste ich den code einfügen?

    Das stand schon in meiner Antwort "[COLOR=#333333]gleich zu beginn einer Funktion"[/COLOR]

    WordPress ist "frei" wie in Freiheit es zu nutzen, aber nicht im Sinne von Freibier. Wer also glaubt man bekommt rund um WordPress alles kostenlos, der irrt. Hilfe ist ein Geschenk für das man sich bedankt, dafür gibt es den 'Gefällt mir' Button. Wer das nicht kann und sich selbst nicht zu helfen weiss, muss sich bezahlte Unterstützung suchen.

  • habe es nun gemacht, der warenkorb muss aber immer noch durch 3 teilbar sein...


    <?php
    // check that cart items quantities totals are in multiples of 3
    add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
    function woocommerce_check_cart_quantities() {

    global $product;
    if ( $product->id == 594 ) {
    return;
    }
    global $woocommerce;
    $multiples = 3;
    $total_products = 0;
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
    $total_products += $values['quantity'];
    }
    if ( ( $total_products % $multiples ) > 0 )
    $woocommerce->add_error( sprintf( __('Bitte beachten Sie, dass die Bestellmenge aus Versandgründen durch %s teilbar sein muss.', 'woocommerce'), $multiples ) );
    }


    // Limit cart items with a certain shipping class to be purchased in multiple only
    add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities_for_class' );
    function woocommerce_check_cart_quantities_for_class() {
    global $woocommerce;
    $multiples = 3;
    $class = 'bottle';
    $total_products = 0;
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
    $product = get_product( $values['product_id'] );
    if ( $product->get_shipping_class() == $class ) {
    $total_products += $values['quantity'];
    }
    }
    if ( ( $total_products % $multiples ) > 0 )
    $woocommerce->add_error( sprintf( __('Ihre Bestellmenge muss aus Versandgründen durch %s teilbar sein.', 'woocommerce'), $multiples ) );
    }


    ?>


    <?php


    /********** KLASIK DEFINITION *************/
    global $themeoptionsvalue, $meta_boxes;


    if ( ! isset( $content_width ) ) $content_width = 1140;
    $child_path = get_stylesheet_directory() . '/';
    //$admin_path = get_template_directory() . '/admin/';
    $includes_path = get_template_directory() . '/includes/';
    $meta_boxes = array();


    define('KLASIK_THEMENAME', 'Klasik');
    define('KLASIK_PARENTMENU_SLUG', 'klasiktheme-settings');


    /********** END KLASIK DEFINITION *************/


    // Theme Options
    require_once $includes_path . 'theme-admin.php';


    //Theme init
    require_once $includes_path . 'theme-init.php';


    //Metabox Function
    require_once $includes_path . 'theme-metaboxes.php';


    //Widgets
    require_once $includes_path . 'theme-widgets.php';


    //Sidebar
    require_once $includes_path . 'theme-sidebar.php';


    //Additional function
    require_once $includes_path . 'theme-function.php';


    //Header function
    require_once $includes_path . 'header-function.php';


    //Footer function
    require_once $includes_path . 'footer-function.php';


    //Loading jQuery
    require_once $includes_path . 'theme-scripts.php';


    //Loading Style Css
    require_once $includes_path . 'theme-styles.php';


    if(file_exists($child_path . 'includes/child-functions.php')){
    require_once $child_path . 'includes/child-functions.php';
    }


    if(file_exists($child_path . 'includes/child-metaboxes.php')){
    require_once $child_path . 'includes/child-metaboxes.php';
    }


    if(file_exists($child_path . 'includes/child-shortcodes.php')){
    require_once $child_path . 'includes/child-shortcodes.php';
    }

  • habe es nun gemacht, der warenkorb muss aber immer noch durch 3 teilbar sein

    Dann muss die Anzahl bei einem Match geändert werden. Also die Änderung wieder rückgängig machen und im loop:

    Code
    [COLOR=#333333]foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
    [/COLOR] if ( [COLOR=#2E3133][FONT=Consolas]$values[/FONT][/COLOR][COLOR=#2E3133][FONT=Consolas][[/FONT][/COLOR][COLOR=#6B291B][FONT=Consolas]'product_id'[/FONT][/COLOR][COLOR=#2E3133][FONT=Consolas]] == 594 ) {
    [/FONT][/COLOR][COLOR=#333333]  $total_products += $values['quantity'] * 3;
    [/COLOR] } else {
    [COLOR=#333333]  $total_products += $values['quantity'];
    [/COLOR] }
    [COLOR=#333333]}
    [/COLOR]

    WordPress ist "frei" wie in Freiheit es zu nutzen, aber nicht im Sinne von Freibier. Wer also glaubt man bekommt rund um WordPress alles kostenlos, der irrt. Hilfe ist ein Geschenk für das man sich bedankt, dafür gibt es den 'Gefällt mir' Button. Wer das nicht kann und sich selbst nicht zu helfen weiss, muss sich bezahlte Unterstützung suchen.

Jetzt mitmachen!

Sie haben noch kein Benutzerkonto auf unserer Seite? Registrieren Sie sich kostenlos und nehmen Sie an unserer Community teil!