Fügen Sie eine Validierung für benutzerdefinierte zusätzliche erforderliche Checkout-Felder für WooCommerce hinzu
P粉204136428
P粉204136428 2023-07-27 12:38:49
0
1
434
<p>Wenn das Produkt also zu einer bestimmten Kategorie gehört, füge ich diese WooCommerce-Felder zur Checkout-Seite hinzu. Aber jetzt habe ich für beide Felder „required=true“ gesetzt, aber es funktioniert nicht? </p><p>So habe ich diese Felder erstellt: </p><p><br /></p> <pre class="brush:php;toolbar:false;"><?php add_action( 'woocommerce_before_order_notes', 'add_video_short_checkout_fields', 1000, 1 ); Funktion add_video_short_checkout_fields( $checkout ) { date_default_timezone_set('Europa/Amsterdam'); // Warenkorbartikel durchgehen foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { $product = $cart_item['data']; if ( has_term( 'video-short', 'product_cat', $product->get_id() ) ) { $product_title = $product->get_name(); $product_id = $product->get_id(); $unique_id = $cart_item_key; $current_date = date('d-m-Y', strtotime('+72 Stunden')); $hours_to_add = 24; // Benutzerdefinierte Felder für das Produkt hinzufügen echo '<div class="video-short-fields group_extra_info">'; echo '<h3>' . echo '<div class="video-short-fields form-row-group">'; woocommerce_form_field( 'custom_field_1_' . $unique_id, array( 'Typ' => 'Text', 'class' => array( 'video_veld customfield_start form-row-wide' ), 'label' => __( 'Video Title Short', 'woocommerce' ), 'placeholder' => __( 'Ihr Titel', 'woocommerce' ), 'erforderlich' => wahr, ), $cart_item['my_custom_field_1'] ); $hours_to_add += 24; } ?></pre> <p><br /></p>
P粉204136428
P粉204136428

Antworte allen(1)
P粉135799949

您提到了不同的函数名称。

函数的名称是add_youtube_short_checkout_fields,但钩子引用了add_video_short_checkout_fields。

<?php
    
    
    add_action( 'woocommerce_before_order_notes', 'add_youtube_short_checkout_fields', 1000, 1 );
    function add_youtube_short_checkout_fields( $checkout ) {
    
        date_default_timezone_set('Europe/Amsterdam');
    
        // Loop through cart items
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            $product = $cart_item['data'];
    
            if ( has_term( 'video-short', 'product_cat', $product->get_id() ) ) {
                $product_title = $product->get_name();
                $product_id = $product->get_id();
                $unique_id = $cart_item_key;
    
                $current_date = date('d-m-Y', strtotime('+72 hours'));
                $hours_to_add = 24;
    
                // Add custom fields for the product
                echo '<div class="video-short-fields group_extra_info">';
                echo '<h3>' . esc_html( $product_title ) . '</h3>';
                echo '<div class="video-short-fields form-row-group">';
    
                woocommerce_form_field( 'custom_field_1_' . $unique_id, array(
                    'type' => 'text',
                    'class' => array( 'video_veld customfield_start form-row-wide' ),
                    'label' => __( 'Video Title Short', 'woocommerce' ),
                    'placeholder' => __( 'Your title', 'woocommerce' ),
                    'required' => true,
                    ), $cart_item['my_custom_field_1'] );
    
    
    
                $hours_to_add += 24;
        
            }
    
    
    
    ?>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage