WooCommerce Dropdown Text “Option wählen” ändern

Ich hab für ein Kundenprojekt eine Möglichkeit gesucht, den Dropdown Text im WooCommerce Shop, individuell zu ändern pro Produkt.

Das macht vor allem Sinn wenn man zb. Kurse anbietet.

schnappschuss 111023 091334 am
schnappschuss 111023 091700 am

diese Funktion könnt ihr mit folgenden Snippet erreichen:

/**
*   Add Custom Field to Products and allow to change the default Dropdown Value for this product.
*   V 1.0.0 - 10112023 
*   BUILD: 101120230835
*   Author: Digital-Workshop.at
*/

function add_custom_field_to_variations() {
    woocommerce_wp_text_input( 
        array( 
            'id'          => '_custom_dropdown_text', 
            'label'       => 'Benutzerdefinierter Dropdown-Text', 
            'placeholder' => 'Geben Sie einen benutzerdefinierten Dropdown-Text ein',
            'desc_tip'    => 'true',
            'description' => 'Wenn Sie hier einen Text eingeben, wird dieser als Standardtext für das Dropdown-Menü verwendet.'
        )
    );
}
add_action( 'woocommerce_product_options_advanced', 'add_custom_field_to_variations' );

function save_custom_field( $post_id ) {
    $custom_field_value = isset( $_POST['_custom_dropdown_text'] ) ? $_POST['_custom_dropdown_text'] : '';
    update_post_meta( $post_id, '_custom_dropdown_text', sanitize_text_field( $custom_field_value ) );
}
add_action( 'woocommerce_process_product_meta', 'save_custom_field' );

function custom_wc_dropdown_variation_change_text( $args ) {
    global $product;
    
    if ( ! is_null( $args ) && is_a( $product, 'WC_Product' ) ) {
        $custom_text = get_post_meta( $product->get_id(), '_custom_dropdown_text', true );
        if ( ! empty( $custom_text ) ) {
            $args['show_option_none'] = $custom_text;
        }
    }
    
    if ( empty( $args['show_option_none'] ) ) {
        $args['show_option_none'] = 'Wählen Sie eine Option';
    }

    return $args;
}
add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'custom_wc_dropdown_variation_change_text');
Bild eines Logos von Discord

Hinterlassen Sie den ersten Kommentar

*Affiliatelinks/Werbelinks
Die mit Sternchen (*) gekennzeichneten Links sind sogenannte Affiliate-Links.
Wenn du auf so einen Affiliate-Link klickst und über diesen Link einkaufst, bekomme ich von dem betreffenden Online-Shop oder Anbieter eine Provision.
Für dich verändert sich der Preis nicht.