Fügen Sie ein Produkt-Add-On-Feld zu bestimmten Produkten auf WooCommerce hinzu

Lesezeit: 6 Minuten

Fugen Sie ein Produkt Add On Feld zu bestimmten Produkten auf WooCommerce hinzu
RompelStompel

Ich muss den Code ändern, um den Textbereich nicht auf allen meinen WooCommerce-Produkten anzuzeigen, sondern nur auf 2, dies ist auf meinem untergeordneten WordPress-Theme unter functions.php Datei.

Ich habe $product_id in geändert $product_id = 2130(meine spezifische Produkt-ID)bin mir nicht sicher, ob ich das alles ändern soll $product_id oder $_POST damit dieser Code nur auf 2 Produkten angezeigt wird

Es ist für ein bestimmtes Produkt oder Produkte, die mit einem Namen personalisiert werden. Ich habe versucht, den Code $product_id auf zahlreiche andere Erweiterungen ohne Erfolg zu ändern. Ich bin mir nicht sicher, ob ich die gesamte $product_id durch welchen Code ersetzen soll, oder sollte es bei sein $_POST und alle ersetzen.

add_action( 'woocommerce_before_add_to_cart_button', 'custom_product_add_on', 9 );  
function custom_product_add_on() {
    $value = isset( $_POST['_custom_text_add_on'] ) ? sanitize_text_field( $_POST['_custom_text_add_on'] ) : '';

    echo '<div>
        <label>Custom Text Add-On <abbr class="required" title="required">*</abbr></label>
        <p>
             <input name="_custom_text_add_on" value="' . $value . '">
        </p>
    </div>';
}

add_filter( 'woocommerce_add_to_cart_validation', 'custom_product_add_on_validation', 10, 3 );
function custom_product_add_on_validation( $passed, $product_id, $qty ){
    if( isset( $_POST['_custom_text_add_on'] ) && sanitize_text_field( 
$_POST['_custom_text_add_on'] ) == '' ) {
         wc_add_notice( 'Custom Text Add-On is a required field', 'error' );
         $passed = false;
    }
    return $passed;
}

add_filter( 'woocommerce_add_cart_item_data', 'custom_product_add_on_cart_item_data', 10, 2 );
function custom_product_add_on_cart_item_data( $cart_item, $product_id ){
    if( isset( $_POST['_custom_text_add_on'] ) ) {
        $cart_item['custom_text_add_on'] = sanitize_text_field( 
$_POST['_custom_text_add_on'] );
    }
    return $cart_item;
}

add_filter( 'woocommerce_get_item_data', 'custom_product_add_on_display_cart', 10, 2 );
function custom_product_add_on_display_cart( $_data, $cart_item ) {
    if ( isset( $cart_item['custom_text_add_on'] ) ){
        $data[] = array(
            'name' => 'Custom Text Add-On',
            'value' => sanitize_text_field( $cart_item['custom_text_add_on'] )
        );
    }
    return $data;
}

add_action( 'woocommerce_add_order_item_meta', 'custom_product_add_on_order_item_meta', 10, 2 );
function custom_product_add_on_order_item_meta( $item_id, $values ) {
    if ( ! empty( $values['custom_text_add_on'] ) ) {
        wc_add_order_item_meta( $item_id, 'Custom Text Add-On', 
$values['custom_text_add_on'], true );
    }
}

add_filter( 'woocommerce_order_item_product', 'custom_product_add_on_display_order', 10, 2 );
function custom_product_add_on_display_order( $cart_item, $order_item ){
    if( isset( $order_item['custom_text_add_on'] ) ){
        $cart_item_meta['custom_text_add_on'] = 
$order_item['custom_text_add_on'];
    }
    return $cart_item;
}

add_filter( 'woocommerce_email_order_meta_fields', 'custom_product_add_on_display_emails' );
function custom_product_add_on_display_emails( $fields ) { 
    $fields['custom_text_add_on'] = 'Custom Text Add-On';
    return $fields; 
}

  • Warum fügen Sie so viele Kommentare hinzu, anstatt die Frage selbst zu ändern?

    – Amarnasan

    20. Mai 2019 um 11:38 Uhr

  • Es tut mir leid, ich bin neu in Foren, ich entschuldige mich, werde herausfinden, wie das jetzt geht.

    – RompelStompel

    20. Mai 2019 um 11:40 Uhr

  • Entschuldigung, ich habe die Kommentare gelöscht und meine Frage bearbeitet.

    – RompelStompel

    20. Mai 2019 um 12:01 Uhr

Fugen Sie ein Produkt Add On Feld zu bestimmten Produkten auf WooCommerce hinzu
LoicTheAztec

Ich habe Ihren Code fast überall überarbeitet, einige Änderungen vorgenommen und unnötige Funktionen entfernt.

Sie müssen Ihre gewünschten Produkt-IDs in den ersten beiden Funktionen definieren, in $defined_products_ids Variablen-Array.

Ich habe einen Abstand von 12 Pixeln zwischen Dropdowns / Add-Ons und der Schaltfläche „In den Warenkorb“ in variablen Produkten hinzugefügt.

Der vollständige Code:

// Display Product add on custom fields
add_action( 'woocommerce_before_add_to_cart_button', 'display_product_add_on_custom_fields', 9 );
function display_product_add_on_custom_fields() {
    global $product;

    $defined_products_ids = array( 37, 53 );

    if( in_array($product->get_id(), $defined_products_ids) ) {
        $value = isset($_POST['custom_text']) ? $_POST['custom_text'] : '';

        // Adding some space between dropdowns and add to cart button for variable products
        $style = $product->is_type('variable') ? ' style="padding-bottom:12px;"' : '';

        echo '<div class="product-add-on"' . $style . '>
            <label>'. __('Add your customized text', 'woocommerce') . ' ' .
                '<abbr class="required" title="required">*</abbr>
            </label>
            <p>
                <input name="custom_text" value="' . $value . '">
            </p>
        </div>';
    }
}

// Product add on custom fields validation
add_filter( 'woocommerce_add_to_cart_validation', 'product_add_on_custom_fields_validation', 10, 3 );
function product_add_on_custom_fields_validation( $passed, $product_id, $quantity ){
    $defined_products_ids = array( 37, 53 );

    if( isset( $_POST['custom_text'] ) && empty( $_POST['custom_text'] )
    && in_array($product_id, $defined_products_ids) ) {
         wc_add_notice( '"Custom Text" is a required field', 'error' );
         $passed = false;
    }
    return $passed;
}

// Add custom cart item data
add_filter( 'woocommerce_add_cart_item_data', 'add_custom_cart_item_data', 10, 2 );
function add_custom_cart_item_data( $cart_item_data, $product_id ){
    if( isset($_POST['custom_text']) ) {
        $cart_item_data['custom_text'] = sanitize_text_field( $_POST['custom_text'] );
        $cart_item_data['unique_key'] = md5( microtime().rand() ); // Make each item unique
    }
    return $cart_item_data;
}

// Display custom cart item data on cart and checkout
add_filter( 'woocommerce_get_item_data', 'display_custom_cart_item_data', 10, 2 );
function display_custom_cart_item_data( $cart_item_data, $cart_item ) {
    if ( isset( $cart_item['custom_text'] ) ){
        $cart_item_data[] = array(
            'name' => __('Your custom text', 'woocommerce'),
            'value' => $cart_item['custom_text'] // Already sanitized field
        );
    }
    return $cart_item_data;
}

// Save and display custom item data everywhere on orders and email notifications
add_action( 'woocommerce_checkout_create_order_line_item', 'add_product_custom_field_as_order_item_meta', 10, 4 );
function add_product_custom_field_as_order_item_meta( $item, $cart_item_key, $values, $order ) {
    if ( isset($values['custom_text']) ) {
        $item->update_meta_data('Your custom text', $values['custom_text'] );
    }
}

Der Code wird in die Datei functions.php Ihres aktiven untergeordneten Designs (oder aktiven Designs) eingefügt. Getestet und funktioniert.

  • Wenn ich diesen Code ändern wollte, anstatt Text einzugeben, wo der Benutzer lieber 1, 2 oder 4 Dateien hochladen kann, auch an E-Mail anhängen und wie oben bestellen muss, sollte ich nur einen Container erstellen und oben auf Container statt auf custom_text_area zeigen lassen

    – RompelStompel

    20. Mai 2019 um 13:30 Uhr

  • @RompelStompel Das ist etwas anderes und Sie sollten zuerst selbst Code suchen und ausprobieren… Dann können Sie bei Bedarf eine neue Frage stellen.

    – LoicTheAztec

    20. Mai 2019 um 13:39 Uhr

  • Anscheinend gibt es in diesem Forum keine persönlichen Nachrichten. Ich möchte Ihnen nur für Ihre Zeit und Bemühungen danken. Ich habe einen Beitrag gefunden, in dem Sie kommentiert haben, was genau ich brauche, aber nur für bestimmte Produkte wie den obigen Code. stackoverflow.com/questions/51716368/…

    – RompelStompel

    28. Mai 2019 um 9:58 Uhr


1004400cookie-checkFügen Sie ein Produkt-Add-On-Feld zu bestimmten Produkten auf WooCommerce hinzu

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

Privacy policy