Antwort auf: Benutzerdefiniertes Produktfeld

[ad_1]

// Display the custom note field in the cart
function display_custom_note_field_in_cart() {
    echo '<tr class="custom-order-note">
        <th>' . __('Order Note', 'woocommerce') . '</th>
        <td><textarea name="custom_order_note" class="input-text"></textarea></td>
    </tr>';
}
add_action('woocommerce_cart_totals_before_order_total', 'display_custom_note_field_in_cart');

// Display the saved custom note value in the cart
function display_saved_custom_note_in_cart() {
    if (WC()->session->__isset('custom_order_note')) {
        echo '<tr class="custom-order-note">
            <th>' . __('Order Note', 'woocommerce') . '</th>
            <td>' . esc_html(WC()->session->get('custom_order_note')) . '</td>
        </tr>';
    }
}
add_action('woocommerce_cart_totals_after_order_total', 'display_saved_custom_note_in_cart');

// Display the custom note field in the checkout
function display_custom_note_field_in_checkout() {
    echo '<div id="custom-order-note-field" class="woocommerce-additional-fields">
        <h3>' . __('Order Note', 'woocommerce') . '</h3>
        <textarea name="custom_order_note" class="input-text"></textarea>
    </div>';
}
add_action('woocommerce_before_order_notes', 'display_custom_note_field_in_checkout');

// Save the custom note field value to the session
function save_custom_note_field_to_session() {
    if (isset($_POST['custom_order_note'])) {
        WC()->session->set('custom_order_note', sanitize_text_field($_POST['custom_order_note']));
    }
}
add_action('woocommerce_cart_calculate_fees', 'save_custom_note_field_to_session');

// Save the custom note field as an order note
function save_custom_note_field_to_order($order_id) {
    if (WC()->session->__isset('custom_order_note')) {
        $order = wc_get_order($order_id);
        $note = sanitize_text_field(WC()->session->get('custom_order_note'));
        $order->add_order_note($note);
    }
}
add_action('woocommerce_checkout_create_order', 'save_custom_note_field_to_order');

Vielleicht hilft dir dieser Code. Kommt vom ChatGPT, sieht aber ganz brauchbar aus auf den ersten Blick

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer