Support

Account

Home Forums Backend Issues (wp-admin) Customer Tag

Unread

Customer Tag

  • Hi there,

    When a customer buys a product, I want to attach a tag to it.

    I have created a selection field in ACF with the following pre-filled values:
    – Kamerplanten
    – Gazon
    – Moestuin
    – Siertuin

    I will fill in these values for each product. When a customer purchases a product, I want this value to be visible in the order.

    I am using the same field for both the products and the orders, so it’s a single field.

    When a customer places an order, I want the values from the product to be copied to the order. Therefore, it is possible for an order to have more than one filled value.

    I have been working with ChatGPT, but I can’t get the code to work. You can find the chat with ChatGPT here: Link(I apologize that it’s in Dutch).

    The latest code that ChatGPT shared with me is:

    
    // Voeg deze code toe aan je functions.php bestand in het thema van je WordPress-website
    
    // Voeg het verborgen aangepaste veld "Klanttag" toe aan het bestelformulier op de checkout-pagina
    function add_custom_field_to_checkout($checkout) {
        $product_id = 0;
        $product = null;
    
        // Haal het product-ID op van de eerst toegevoegde productregel in de bestelling
        if (WC()->cart && WC()->cart->get_cart()) {
            foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
                $product_id = $cart_item['product_id'];
                break;
            }
        }
    
        if ($product_id) {
            $product = wc_get_product($product_id);
        }
    
        if ($product) {
            $klanttag_field = get_field_object('klanttag', $product_id);
            $klanttag_options = $klanttag_field['choices'];
    
            if (!empty($klanttag_options)) {
                echo '<input type="hidden" class="input-hidden" name="klanttag" id="klanttag" value="">';
            }
        }
    }
    add_action('woocommerce_after_order_notes', 'add_custom_field_to_checkout');
    
    // Sla de waarde van het geselecteerde aangepaste veld "Klanttag" op in de bestelling
    function save_custom_field_to_order($order) {
        if (isset($_POST['klanttag'])) {
            $klanttag = sanitize_text_field($_POST['klanttag']);
            $order->update_meta_data('Klanttag', $klanttag);
        }
    }
    add_action('woocommerce_checkout_create_order', 'save_custom_field_to_order');
    
    // Kopieer de waarde van het selectievakje "Klanttag" van het product naar de bestelling
    function copy_custom_field_to_order($item, $cart_item_key) {
        $product_id = $item['product_id'];
        $klanttag = get_field('klanttag', $product_id);
    
        if (!empty($klanttag)) {
            $item->add_meta_data('Klanttag', $klanttag, true);
        }
    }
    add_action('woocommerce_checkout_create_order_line_item', 'copy_custom_field_to_order', 10, 2);
    

    Please place this complete code in the functions.php file of your active theme in WordPress. After adding the code, the “Customer Tag” field will be hidden on the checkout page, the selected value will be saved in the “Customer Tag” field of the order, and it will also be copied to the order line item of each product in the order.

    Can anyone help me get this code working?

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.