Tagged: 

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #955333

    I have added a custom field in woocommerce backend to choose products using the following code (in functions.php)

    // Display the custom fields in the "Linked Products" section
    add_action( 'woocommerce_product_options_related', 'woocom_linked_products_data_custom_field' );
    
    // Save to custom fields
    add_action( 'woocommerce_process_product_meta', 'woocom_linked_products_data_custom_field_save' );
    
    // Function to generate the custom fields
    function woocom_linked_products_data_custom_field() {
        global $woocommerce, $post;
    ?>
        <p class="form-field">
            <label for="inkt_products"><?php _e( 'Inkten', 'woocommerce' ); ?></label>
            <select class="wc-product-search" multiple="multiple" style="width: 50%;" id="inkt_products" name="inkt_products[]" data-placeholder="<?php esc_attr_e( 'Search for a product&hellip;', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations" data-exclude="<?php echo intval( $post->ID ); ?>">
            <?php
                $product_ids = get_post_meta( $post->ID, '_inkt_products_ids', true );
    
                foreach ( $product_ids as $product_id ) {
                    $product = wc_get_product( $product_id );
                    if ( is_object( $product ) ) {
                        echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>';
                    }
                }
            ?>
        </select>
            <?php echo wc_help_tip( __( 'Select Products Here.', 'woocommerce' ) ); ?>
        </p>
    
        <p class="form-field">
            <label for="papier_products"><?php _e( 'Papier', 'woocommerce' ); ?></label>
            <select class="wc-product-search" multiple="multiple" style="width: 50%;" id="papier_products" name="papier_products[]" data-placeholder="<?php esc_attr_e( 'Search for a product&hellip;', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations" data-exclude="<?php echo intval( $post->ID ); ?>">
            <?php
                $product_ids = get_post_meta( $post->ID, '_papier_products_ids', true );
    
                foreach ( $product_ids as $product_id ) {
                    $product = wc_get_product( $product_id );
                    if ( is_object( $product ) ) {
                        echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>';
                    }
                }
            ?>
        </select>
            <?php echo wc_help_tip( __( 'Select Products Here.', 'woocommerce' ) ); ?>
        </p>
    
        <?php
    }
    
    // Function the save the custom fields
    function woocom_linked_products_data_custom_field_save( $post_id ){
        $product_field_type =  $_POST['inkt_products'];
        update_post_meta( $post_id, '_inkt_products_ids', $product_field_type );
        $product_field_type = $_POST['papier_products']; 
        update_post_meta( $post_id, '_papier_products_ids', $product_field_type );
    }

    I want to display this in the product page. I tried
    get_post_meta( $post->ID, '_inkt_products_ids', true );

    But that doesnt work. How can I show the ids/products?

    #956292

    Hey Plottershop,

    Thank you for using Enfold.

    Where did you add the get_post_meta function? And where do you want to display it? I can’t find the custom field dropdown in the product editor. Where can we test it?

    Best regards,
    Ismael

    #956350

    Hey Ismael,

    Thanks for looking into the issue. I was able to fix it by adding global $post;

    Thanks

    #956721

    Hi,

    Glad it worked for you! :)

    Please feel free to create a new ticket if you need anything else.

    Best regards,
    Basilis

Viewing 4 posts - 1 through 4 (of 4 total)
  • The topic ‘Display custom field contents woocommerce’ is closed to new replies.