Artikel einrichten, der automatisch im Warenkorb liegt

  • Hallo,

    ich betreibe einen Shop mit Waren und da ich selbst ein kleines Buch zu den Waren geschrieben habe (Waren reale Güter, das Buch als Pdf), möchte ich jedem Käufer automatisch diese PDF zum Download in den Warenkorb legen.

    Wie man digitale Güter einpflegt, weiss ich.

    Ich habe aber noch keine Möglichkeit gefunden, den Warenkorb so zu bearbeiten dass der Artikel XY automatisch im Warenkorb liegt. Der Preis des Artikels ist natürlich 0 EUR.

    Gibt es da ein Plugin?

    • 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

  • Dazu gibt es einen Beitrag bei WooThemes http://docs.woothemes.com/document/autom…-cart-on-visit/

    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.

  • Ne klappte doch nicht... habe folgenden Code eingefülgt:

    /*
    * goes in theme functions.php or a custom plugin
    **/
    // add item to cart on visit depending on cart total value
    add_action( 'init', 'add_product_to_cart' );
    function add_product_to_cart() {
    if ( ! is_admin() ) {
    global $woocommerce;
    $product_id = 2831;
    $found = false;
    $cart_total = 30;

    if( $woocommerce->cart->total >= $cart_total ) {
    //check if product already in cart
    if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
    $_product = $values['data'];
    if ( $_product->id == $product_id )
    $found = true;
    }
    // if product not found, add it
    if ( ! $found )
    $woocommerce->cart->add_to_cart( $product_id );
    } else {
    // if no products in cart, add it
    $woocommerce->cart->add_to_cart( $product_id );
    }
    }
    }
    }

    -------------------

    Habe dabei natürlich den Bestellwert auf 0 gesetzt und die Produkt ID geändert.

    Die komplette Function PHP sah dann so aus:

    <?php
    /**
    * storefront engine room
    *
    * @package storefront
    */

    /**
    * Initialize all the things.
    */
    require get_template_directory() . '/inc/init.php';

    /**
    * Note: Do not add any custom code here. Please use a child theme so that your customizations aren't lost during updates.
    * http://codex.wordpress.org/Child_Themes
    */
    //anzahl verstecken start
    add_filter( 'woocommerce_subcategory_count_html', 'woo_remove_category_products_count' );

    function woo_remove_category_products_count() {
    return;
    }
    //anzahl verstecken ende
    /*
    * goes in theme functions.php or a custom plugin
    **/
    // add item to cart on visit depending on cart total value
    add_action( 'init', 'add_product_to_cart' );
    function add_product_to_cart() {
    if ( ! is_admin() ) {
    global $woocommerce;
    $product_id = 2831;
    $found = false;
    $cart_total = 0;

    if( $woocommerce->cart->total >= $cart_total ) {
    //check if product already in cart
    if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
    $_product = $values['data'];
    if ( $_product->id == $product_id )
    $found = true;
    }
    // if product not found, add it
    if ( ! $found )
    $woocommerce->cart->add_to_cart( $product_id );
    } else {
    // if no products in cart, add it
    $woocommerce->cart->add_to_cart( $product_id );
    }
    }
    }
    }

    -----------------

    Ergebnis: Seite wurde nicht mehr geladen, es kam die Meldung "Fehler 500 interner Serverfehler". Sowohl auf dem Handy als auch am PC im Firefox und IE.
    Wo ist da der Fehler?

  • Auf Anhieb fallen mir folgende Zeilen auf:

    PHP
    if ( ! $found )
    $woocommerce->cart->add_to_cart( $product_id );


    Schreibe die mal wie folgt:

    PHP
    if ( ! $found ) {
    $woocommerce->cart->add_to_cart( $product_id );
    }

    Das Geheimnis des Könnens liegt im Wollen!
    [size=12]Bitte keine Hilfegesuche per PN (auch keine "privaten" Links). Das ist ein öffentliches Forum.[/SIZE]

  • Geht immer noch nicht, der code sieht nun folgendermaßen aus:

    //anzahl verstecken start
    add_filter( 'woocommerce_subcategory_count_html', 'woo_remove_category_products_count' );
    function woo_remove_category_products_count() {
    return;
    }
    //anzahl verstecken ende
    /*
    * goes in theme functions.php or a custom plugin
    **/
    // add item to cart on visit depending on cart total value
    add_action( 'init', 'add_product_to_cart' );
    function add_product_to_cart() {
    if ( ! is_admin() ) {
    global $woocommerce;
    $product_id = 340;
    $found = false;
    $cart_total = 0;
    if( $woocommerce->cart->total >= $cart_total ) {
    //check if product already in cart
    if ( sizeof( $woocommerce->cart->get_cart() ) > 0) {
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
    $_product = $values['data'];
    if ( $_product->id == $product_id )
    $found = true;
    }
    // if product not found, add it
    if ( ! $found ) {
    $woocommerce->cart->add_to_cart( $product_id );
    } else {
    // if no products in cart, add it
    $woocommerce->cart->add_to_cart( $product_id );
    }
    }
    }
    }

  • Du hast eine } vergessen.

    Das Geheimnis des Könnens liegt im Wollen!
    [size=12]Bitte keine Hilfegesuche per PN (auch keine "privaten" Links). Das ist ein öffentliches Forum.[/SIZE]

  • DA STECKT IRGENDWO DER TEUFEL DRIN.... es geht nicht. hier der Code:

    <?php
    /**
    * storefront engine room
    *
    * @package storefront
    */

    /**
    * Initialize all the things.
    */
    require get_template_directory() . '/inc/init.php';

    /**
    * Note: Do not add any custom code here. Please use a child theme so that your customizations aren't lost during updates.
    * http://codex.wordpress.org/Child_Themes
    */
    //anzahl verstecken start
    add_filter( 'woocommerce_subcategory_count_html', 'woo_remove_category_products_count' );
    function woo_remove_category_products_count() {
    return;
    }
    //anzahl verstecken ende BIE HIERHIN; WENN ICH DAS UNTERE LÖSCHE GEHTS, ALSO MUSS DER FEHLER UNTEN LIEGEN!!!
    /*
    * goes in theme functions.php or a custom plugin
    **/
    // add item to cart on visit depending on cart total value
    add_action( 'init', 'add_product_to_cart' );
    function add_product_to_cart() {
    if ( ! is_admin() ) {
    global $woocommerce;
    $product_id = 340;
    $found = false;
    $cart_total = 0;
    if( $woocommerce->cart->total >= $cart_total ) {
    //check if product already in cart
    if ( sizeof( $woocommerce->cart->get_cart() ) > 0) {
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
    $_product = $values['data'];
    if ( $_product->id == $product_id )
    $found = true;
    }
    // if product not found, add it
    if ( ! $found ) {
    $woocommerce->cart->add_to_cart( $product_id );
    }
    } else {
    // if no products in cart, add it
    $woocommerce->cart->add_to_cart( $product_id );
    }
    }
    }
    }

  • oder kommt der code nicht damit zurecht, dass ich eingetragen haben, dass der Artikel bei 0 reingelegt wird und paar Zeilen später heisst es >0 ?

  • Hier der richtige Code, Lösung gefunden:


    /*
    * goes in theme functions.php or a custom plugin
    **/
    // add item to cart on visit depending on cart total value
    add_action( 'init', 'add_product_to_cart' );
    function add_product_to_cart() {
    if ( ! is_admin() ) {
    global $woocommerce;
    $product_id = 479;
    $found = false;
    $cart_total = 200;
    if( $woocommerce->cart->total >= $cart_total ) {
    //check if product already in cart
    if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
    $_product = $values['data'];
    if ( $_product->id == $product_id )
    $found = true;
    }
    // if product not found, add it
    if ( ! $found )
    $woocommerce->cart->add_to_cart( $product_id );
    }} else { [COLOR=#ff0000]Hier am Anfang die zusätzliche Klammer und dafür am Ende eine löschen, damit die Klammerzahl stimmt!!![/COLOR]
    // if no products in cart, add it
    $woocommerce->cart->add_to_cart( $product_id );
    }
    }
    }

    Jetzt wird der Artikel auch kostenlos in den Warenkorb gelegt :-)

    Einmal editiert, zuletzt von secret77 (21. August 2015 um 19:44)

  • Der Code hatte doch noch einen Fehler, hier bereinigt:

    * goes in theme functions.php or a custom plugin
    **/
    // add item to cart on visit depending on cart total value
    add_action( 'init', 'add_product_to_cart' );
    function add_product_to_cart() {
    if ( ! is_admin() ) {
    global $woocommerce;
    $product_id = 479; [COLOR=#FF0000]EURE WOOCOMMERCE ARTIKEL ID[/COLOR]
    $found = false;
    $cart_total > 200; [COLOR=#FF0000]MINDESTBESTELLWERT WAR VORHER "=" MUSS ABER ">"[/COLOR]
    if( $woocommerce->cart->total >= $cart_total ) {
    //check if product already in cart
    if ( sizeof( $woocommerce->cart->get_cart() ) > 200 ) {
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
    $_product = $values['data'];
    if ( $_product->id == $product_id )
    $found = true;
    }
    // if product not found, add it
    if ( ! $found ){
    $woocommerce->cart->add_to_cart( $product_id );
    }} else {
    // if no products in cart, add it
    $woocommerce->cart->add_to_cart( $product_id );
    }
    }
    }
    }


    Jetzt gibt's aber das nächste Problem, dafür öffne ich nen neuen Thread

Jetzt mitmachen!

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