وردپرس

How to add custom field at a loop and show it in cart, order details, meta etc


‘m using a WooCommerce plugin called WooCommerce Photography it shows helps to sell photographs, and we can make collection of photographs, this file is reasonable for showing product loops in the collection page

This is the template for displaying photography content within loops: content-photography.php

I added the following to collect users note for photo editing in my file

<div class="photoshop-note" style="display: block;">
    <label for="photoshop_note_text"><?php _e('Photoshop Note:', 'woocommerce'); ?></label>
    <textarea name="photoshop_note_text" rows="4" cols="50"></textarea>
</div>

Now I am stuck on How to handle the saving and displaying functionality.

I tried the following code:

// Save Photoshop Note to Cart Item Data
add_filter('woocommerce_add_cart_item_data', 'save_photoshop_note_to_cart', 10, 2);
function save_photoshop_note_to_cart($cart_item_data, $product_id)
{
    if (isset($_POST['_custom_photoshop_field']) && isset($_POST['photoshop_note_text'])) {
        $cart_item_data['photoshop_note'] = sanitize_text_field($_POST['photoshop_note_text']);
    }
    return $cart_item_data;
}

// Display Photoshop Note in Cart
add_filter('woocommerce_cart_item_name', 'display_photoshop_note_in_cart', 10, 3);
function display_photoshop_note_in_cart($product_name, $cart_item, $cart_item_key)
{
    if (isset($cart_item['photoshop_note'])) {
        // Debugging: Output a message to check if the function is being triggered
        echo '<pre>Debug: Photoshop Note data - ' . print_r($cart_item['photoshop_note'], true) . '</pre>';

        $product_name .= '<div class="cart-photoshop-note">' . esc_html($cart_item['photoshop_note']) . '</div>';
    }
    return $product_name;
}


// Display Photoshop Note in Email
add_action('woocommerce_email_order_meta', 'display_photoshop_note_in_email', 10, 3);
function display_photoshop_note_in_email($order, $sent_to_admin, $plain_text)
{
    foreach ($order->get_items() as $item_id => $item) {
        $product = $item->get_product();
        $photoshop_note = $product->get_meta('photoshop_note');
        if ($photoshop_note) {
            echo '<p><strong>' . __('Photoshop Note:', 'your-theme-domain') . '</strong><br>' . esc_html($photoshop_note) . '</p>';
        }
    }
}

// Display Photoshop Note in Order Details
add_action('woocommerce_order_details_after_order_table', 'display_photoshop_note_in_order_details', 10, 1);
function display_photoshop_note_in_order_details($order)
{
    foreach ($order->get_items() as $item_id => $item) {
        $product = $item->get_product();
        $photoshop_note = $product->get_meta('photoshop_note');
        if ($photoshop_note) {
            echo '<p><strong>' . __('Photoshop Note:', 'your-theme-domain') . '</strong><br>' . esc_html($photoshop_note) . '</p>';
        }
    }
}

But still no luck there is no photoshop note at cart that we written in note text area

Any help will be highly appreciated.

این خبر را در ایران وب سازان مرجع وب و فناوری دنبال کنید

مشاهده پاسخ های این مطلب
———————————————
این مطلب از سایت انجمن وردپرس گردآوری شده است و کلیه حقوق مطلق به انجمن وردپرس می باشد در صورت مغایرت و یا بروز مشکل اطلاع دهید تا حذف گردد

منبع: انجمن وردپرس

دکمه بازگشت به بالا