উকমার্স কাস্টম ফিল্ডঃ চেকআউট পেইজ

আসসালামু আলাইকুম

উকমার্স চেক আউট পেজে কাস্টম ফিল্ড এড করতে চাচ্ছিলাম, ইন্টারনেটে এভেইলাবল অনেক ধরনের কোড ট্রাই করেছি, কোনটাই কাজ করছে না, কোন এরর শো করছে না।
কোন প্লাগিন কনফ্লিক্ট হতে পারে এই সন্দেহে লোকাল হোস্টে ওয়ার্ডপ্রেসের ফ্রেশ ইন্সটলেশনে শুধু উকমার্স প্লাগিন ইন্সটল দিয়ে কাস্টম ফিল্ড এড করার ট্রাই করেছি, কোন কাজ হচ্ছে না।

উকমার্সের ডকুমেন্টশনের কোড কপি করে দেখলাম, সেটাও কাজ করছে না। বাস্কেট পেজে সিলেক্টেড আইটেম এর নাম্বারের উপর ভিত্তি করে চেকআউট পেইজের কাস্টম ফিল্ড এর সংখ্যা নির্ধারিত হবে, সেক্ষেত্রে কোড ভিত্তিক সমাধান দরকার। কিন্তু ডকুমেন্টশনের দেয়া কোড ডিরেক্ট কপি পেস্ট করে কোন আউটপুট পাচ্ছি না। কি প্রব্লেম হতে পারে??

The code I am trying to add a customize filed on checkout page of woocommerce is following:

<?php 
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );

function my_custom_checkout_field( $checkout ) {

    echo '<div id="my_custom_checkout_field"><h2>' . esc_html__( 'My Field' ) . '</h2>';

    woocommerce_form_field(
        'my_field_name',
        array(
            'type'        => 'text',
            'class'       => array( 'my-field-class form-row-wide' ),
            'label'       => __( 'Fill in this field' ),
            'placeholder' => __( 'Enter something' ),
        ),
        $checkout->get_value( 'my_field_name' )
    );

    echo '</div>';

} ?>

woocommerce documentation Link:

2 Likes

Use this plugin. I used on many sites. Lightweight and useful.

2 Likes

আপনার রিপ্লাই এর জন্য ধন্যবাদ, বাস্কেট ফিল্ডে আইটেম নাম্বার কে লুপিং করে কাস্টম ফিল্ডকে চেকআউট পেইজে পপুলেট করতে হবে, এইজন্য প্লাগিন এর পরিবর্তে কোড ভিত্তিক সলুউশান দরকার। উকমার্স ডকুমেন্টশনের কোড কাজ না করার কি কারণ হতে পারে?

ধন্যবাদ।

Try this

<?php 
// Add the custom fields to the checkout page based on the number of cart items
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_fields_for_cart_items' );

function my_custom_checkout_fields_for_cart_items( $checkout ) {
    echo '<div id="my_custom_checkout_fields"><h2>' . esc_html__( 'Custom Fields for Each Product' ) . '</h2>';

    // Loop through each item in the cart
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $product_id = $cart_item['product_id'];
        $product_name = $cart_item['data']->get_name();

        // Create a custom field for each product
        woocommerce_form_field(
            'custom_field_' . $product_id, // Field name will be unique based on product ID
            array(
                'type'        => 'text',
                'class'       => array( 'my-field-class form-row-wide' ),
                'label'       => __( 'Field for ' ) . $product_name, // Label with the product name
                'placeholder' => __( 'Enter something for ' ) . $product_name,
                'required'    => true, // Set required to true
            ),
            $checkout->get_value( 'custom_field_' . $product_id )
        );
    }

    echo '</div>';
}

// Validate the custom fields for each item in the cart
add_action( 'woocommerce_checkout_process', 'my_custom_checkout_fields_validation' );

function my_custom_checkout_fields_validation() {
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $product_id = $cart_item['product_id'];

        // Check if the custom field for each product is filled
        if ( ! isset( $_POST[ 'custom_field_' . $product_id ] ) || empty( $_POST[ 'custom_field_' . $product_id ] ) ) {
            wc_add_notice( sprintf( __( 'Please fill in the field for %s.' ), $cart_item['data']->get_name() ), 'error' );
        }
    }
}

// Save the custom field values to the order meta after checkout
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_fields_save' );

function my_custom_checkout_fields_save( $order_id ) {
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $product_id = $cart_item['product_id'];

        // Save each custom field value based on the product ID
        if ( isset( $_POST[ 'custom_field_' . $product_id ] ) ) {
            update_post_meta( $order_id, '_custom_field_' . $product_id, sanitize_text_field( $_POST[ 'custom_field_' . $product_id ] ) );
        }
    }
}

// Display the custom fields in the WooCommerce admin order page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_fields_display_in_admin', 10, 1 );

function my_custom_checkout_fields_display_in_admin( $order ) {
    foreach ( $order->get_items() as $item_id => $item ) {
        $product_id = $item->get_product_id();
        $custom_field_value = get_post_meta( $order->get_id(), '_custom_field_' . $product_id, true );

        if ( ! empty( $custom_field_value ) ) {
            echo '<p><strong>' . __( 'Custom Field for ' ) . esc_html( $item->get_name() ) . ':</strong> ' . esc_html( $custom_field_value ) . '</p>';
        }
    }
}

// Add custom field values to order emails
add_filter( 'woocommerce_email_order_meta_fields', 'my_custom_checkout_fields_add_to_emails', 10, 3 );

function my_custom_checkout_fields_add_to_emails( $fields, $sent_to_admin, $order ) {
    foreach ( $order->get_items() as $item_id => $item ) {
        $product_id = $item->get_product_id();
        $custom_field_value = get_post_meta( $order->get_id(), '_custom_field_' . $product_id, true );

        if ( ! empty( $custom_field_value ) ) {
            $fields['custom_field_' . $product_id] = array(
                'label' => __( 'Custom Field for ' ) . esc_html( $item->get_name() ),
                'value' => esc_html( $custom_field_value ),
            );
        }
    }

    return $fields;
}
?>

please add as code block.

সমস্যার সমাধান পেয়েছি , উকমার্সের ব্লক চেক আউট পেইজে কাস্টম ফিল্ড কাজ করে না। তাই চেক আউট পেজকে ক্লাসিক চেক আঊট পেজে আপডেট করে কোড এপ্লাই করতে হয়।

. High-Performance Order Storage is enabled: এই ক্ষেত্রে get_post_meta ফাংশন কাজ করে না।

solution:

My code
`
// Add custom checkout fields based on product quantity (Full Names, Date of Birth, NIN Number)
add_action(‘woocommerce_after_order_notes’, ‘custom_checkout_fields’);

function custom_checkout_fields($checkout) {
// Loop through all cart items
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
$quantity = $cart_item[‘quantity’]; // Get product quantity
$product_name = $cart_item[‘data’]->get_name(); // Get product name

    for ($i = 1; $i <= $quantity; $i++) {
        echo "<h3>" . __("Details for {$product_name} (Item {$i})", 'woocommerce') . "</h3>";
        
        // Full Names field
        woocommerce_form_field(
            "full_name_{$cart_item_key}_{$i}",
            array(
                'type'        => 'text',
                'label'       => __("Full Name", 'woocommerce'),
                'required'    => true,
                'class'       => array('form-row-wide'),
                'placeholder' => __('Enter Full Name', 'woocommerce'),
            ),
            $checkout->get_value("full_name_{$cart_item_key}_{$i}")
        );

        // Date of Birth field
        woocommerce_form_field(
            "dob_{$cart_item_key}_{$i}",
            array(
                'type'        => 'date',
                'label'       => __("Date of Birth", 'woocommerce'),
                'required'    => true,
                'class'       => array('form-row-wide'),
                'placeholder' => __('Select Date of Birth', 'woocommerce'),
            ),
            $checkout->get_value("dob_{$cart_item_key}_{$i}")
        );

        // NIN Number field
        woocommerce_form_field(
            "nin_number_{$cart_item_key}_{$i}",
            array(
                'type'        => 'text',
                'label'       => __("NIN Number", 'woocommerce'),
                'required'    => true,
                'class'       => array('form-row-wide'),
                'placeholder' => __('Enter NIN Number', 'woocommerce'),
            ),
            $checkout->get_value("nin_number_{$cart_item_key}_{$i}")
        );
    }
}

}

// Save custom fields with the order
add_action(‘woocommerce_checkout_update_order_meta’, ‘save_custom_checkout_fields’);

function save_custom_checkout_fields($order_id) {
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {

    $quantity = $cart_item['quantity'];
    $order = wc_get_order( $order_id );
    
    for ($i = 1; $i <= $quantity; $i++) {
        
        if (!empty($_POST["full_name_{$cart_item_key}_{$i}"])) {
             // Save Full Name
            $order->update_meta_data("full_name_{$cart_item_key}_{$i}", sanitize_text_field($_POST["full_name_{$cart_item_key}_{$i}"]));
        }
        
        if (!empty($_POST["dob_{$cart_item_key}_{$i}"])) {
            // Save Date of Birth
            $order->update_meta_data("dob_{$cart_item_key}_{$i}", sanitize_text_field($_POST["dob_{$cart_item_key}_{$i}"]));
        }

        if (!empty($_POST["nin_number_{$cart_item_key}_{$i}"])) {
            // Save NIN Number
            $order->update_meta_data("nin_number_{$cart_item_key}_{$i}", sanitize_text_field($_POST["nin_number_{$cart_item_key}_{$i}"]));
        }
        
        $order->update_meta_data("nin_card_cart_item_key", $cart_item_key );
        $order->save_meta_data();
    }
}

}

//display at admin page
add_action( ‘woocommerce_admin_order_data_after_billing_address’, ‘my_custom_checkout_field_display_admin_order_meta’, 10, 1 );

function my_custom_checkout_field_display_admin_order_meta( $order ){

foreach ($order->get_items() as $item_id => $item) {

    $product_name = $item->get_name();
    $quantity = $item->get_quantity();
    
    ?>
      <h3 align="center">NIN Card Infromation</h3> 
      <table border="1" class="table">
            <tr>
                <th>S.L</th>
                <th>Full Name</th>
                <th>Date of Birth</th>
                <th>NIN Number</th>
            </tr>
    <?php
    for ($i = 1; $i <= $quantity; $i++) {
        
        $nin_card_cart_item_key = $order->get_meta("nin_card_cart_item_key");
       
        $full_name = $order->get_meta("full_name_{$nin_card_cart_item_key}_{$i}");
        $dob =  $order->get_meta("dob_{$nin_card_cart_item_key}_{$i}");
        $nin_number = $order->get_meta("nin_number_{$nin_card_cart_item_key}_{$i}");
        ?>
        <tr> 
                <th><?php echo $i; ?></th>
                <th><?php echo $full_name; ?></th>
                <th><?php echo $dob; ?></th>
                <th><?php echo $nin_number; ?></th>
        
        </tr>
            
         <?php 
           }
         ?>   
            
        </table>
        
        <?php
        
}

}`

@rasel @jakirsuman
Thanks again

1 Like