1. Herzlich willkommen bei WPDE.org, dem grössten und ältesten deutschsprachigen Community-Forum rund um das Thema WordPress. Du musst angemeldet oder registriert sein, um Beiträge verfassen zu können.
    Information ausblenden

Artikel einrichten, der automatisch im Warenkorb liegt

Dieses Thema im Forum "Onlineshop mit WordPress" wurde erstellt von secret77, 14. August 2015.

  1. secret77

    secret77 Active Member

    Registriert seit:
    4. August 2015
    Beiträge:
    28
    Zustimmungen:
    0
    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?
     
  2. mensmaximus

    mensmaximus Well-Known Member

    Registriert seit:
    24. Juli 2014
    Beiträge:
    8.857
    Zustimmungen:
    437
  3. secret77

    secret77 Active Member

    Registriert seit:
    4. August 2015
    Beiträge:
    28
    Zustimmungen:
    0
    Prima und vielen Dank, hat funktioniert!
     
  4. secret77

    secret77 Active Member

    Registriert seit:
    4. August 2015
    Beiträge:
    28
    Zustimmungen:
    0
    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?
     
  5. SirEctor

    SirEctor Well-Known Member
    Ehrenmitglied

    Registriert seit:
    28. Oktober 2008
    Beiträge:
    12.361
    Zustimmungen:
    427
    Auf Anhieb fallen mir folgende Zeilen auf:
    PHP:
    Entschuldige, aber du musst dich registrieren oder anmelden um den Inhalt sehen zu können!
    Schreibe die mal wie folgt:
    PHP:
    Entschuldige, aber du musst dich registrieren oder anmelden um den Inhalt sehen zu können!
     
  6. secret77

    secret77 Active Member

    Registriert seit:
    4. August 2015
    Beiträge:
    28
    Zustimmungen:
    0
    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 );
    }
    }
    }
    }
     
  7. SirEctor

    SirEctor Well-Known Member
    Ehrenmitglied

    Registriert seit:
    28. Oktober 2008
    Beiträge:
    12.361
    Zustimmungen:
    427
    Du hast eine } vergessen.
    PHP:
    Entschuldige, aber du musst dich registrieren oder anmelden um den Inhalt sehen zu können!
     
  8. secret77

    secret77 Active Member

    Registriert seit:
    4. August 2015
    Beiträge:
    28
    Zustimmungen:
    0
    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 );
    }
    }
    }
    }
     
  9. gericoach

    gericoach Well-Known Member

    Registriert seit:
    12. August 2008
    Beiträge:
    7.193
    Zustimmungen:
    0
    prüfe mal die klammern, jede klammer die dur öffnest muss geschlossen werden. was du da postest sieht auf den Ersten chaotisch aus...
     
  10. secret77

    secret77 Active Member

    Registriert seit:
    4. August 2015
    Beiträge:
    28
    Zustimmungen:
    0
    2 mal nachgezählt passt
     
  11. secret77

    secret77 Active Member

    Registriert seit:
    4. August 2015
    Beiträge:
    28
    Zustimmungen:
    0
    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 ?
     
  12. secret77

    secret77 Active Member

    Registriert seit:
    4. August 2015
    Beiträge:
    28
    Zustimmungen:
    0
    Habs mal mit Artikel xy ab Wert 50 versucht, gleicher fehler... an dem Code stimmt was nicht.
     
  13. secret77

    secret77 Active Member

    Registriert seit:
    4. August 2015
    Beiträge:
    28
    Zustimmungen:
    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 { Hier am Anfang die zusätzliche Klammer und dafür am Ende eine löschen, damit die Klammerzahl stimmt!!!
    // if no products in cart, add it
    $woocommerce->cart->add_to_cart( $product_id );
    }
    }
    }

    Jetzt wird der Artikel auch kostenlos in den Warenkorb gelegt :)
     
    #13 secret77, 21. August 2015
    Zuletzt bearbeitet: 21. August 2015
  14. secret77

    secret77 Active Member

    Registriert seit:
    4. August 2015
    Beiträge:
    28
    Zustimmungen:
    0
    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; EURE WOOCOMMERCE ARTIKEL ID
    $found = false;
    $cart_total > 200; MINDESTBESTELLWERT WAR VORHER "=" MUSS ABER ">"
    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
     
  1. Diese Seite verwendet Cookies, um Inhalte zu personalisieren, diese deiner Erfahrung anzupassen und dich nach der Registrierung angemeldet zu halten.
    Wenn du dich weiterhin auf dieser Seite aufhältst, akzeptierst du unseren Einsatz von Cookies.
    Information ausblenden
  1. Diese Seite verwendet Cookies, um Inhalte zu personalisieren, diese deiner Erfahrung anzupassen und dich nach der Registrierung angemeldet zu halten.
    Wenn du dich weiterhin auf dieser Seite aufhältst, akzeptierst du unseren Einsatz von Cookies.
    Information ausblenden