connect woocommerce products to posts

I am using woocommerce on my wordpress site.
I am selling paintings. The products are paintings.
I have a list of artists as posts. Each artist is one post.
I would like to connect the posts and products so I can show the artist’s name on the painting page and the user can click on the name and it takes them to the artist post.
How do I do this?

Related posts

Leave a Reply

1 comment

  1. This is an example of how to add a custom field in WooCommerce product general tab. Since artists are posts ( category is not specified ), it will collect links for all posts and place them in a dropdown. The value of this field will be visible on the single product page below the price ( you can open the content-single-product.php file in WooCommerce theme, to see the actions for single product template, and functions attached, and change the priority of woocommerce_product_artist function if you want to change the place where the link will appear ).

    <?php
    
    add_action( 'admin_init', 'woocommerce_custom_admin_init' );
    
    function woocommerce_custom_admin_init() {
    
        // display fields
        add_action( 'woocommerce_product_options_general_product_data', 'woocommerce_add_custom_general_fields' );
    
        // save fields
        add_action( 'woocommerce_process_product_meta', 'woocommerce_save_custom_general_fields' );
    
    }
    
    function woocommerce_add_custom_general_fields() {
    
        // creating post array for the options ( id => title)
        $posts = array( '' => __( 'Select Artist' ) );
        array_walk( get_posts( array( 'numberposts' => -1 ) ), function( $item ) use ( &$posts ) {
            $posts[ $item->ID ] = $item->post_title;
        } );
    
        // creating dropdown ( woocommerce will sanitize all values )
        echo '<div class="options_group">';
        woocommerce_wp_select(
            array(
                'id' => '_artist',
                'label' => __( 'Artist' ),
                'options' => $posts
            )
        );
        echo '</div>';
    
    }
    
    
    function woocommerce_save_custom_general_fields( $post_id ) {
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    
        // validate id of artist page and save
        if ( isset( $_POST['_artist'] ) ) {
            $value = filter_input( INPUT_POST, '_artist', FILTER_VALIDATE_INT, array( 'options' => array( 'min_range' => 0 ) ) );       
            update_post_meta( $post_id, '_artist', $value );        
        }
    
    }
    
    add_action( 'init', 'woocommerce_custom_init' );
    
    function woocommerce_custom_init() {    
    
        // hook the woocommerce_product_artist function on to woocommerce_single_product_summary action ( priority 15 )
        add_action( 'woocommerce_single_product_summary', 'woocommerce_product_artist', 15 );
    
    }
    
    function woocommerce_product_artist() {
        global $post;
    
        // get the artist page id and show in template ( if exists )
        $artist_id = get_post_meta( $post->ID, '_artist', true );
    
        if ( $artist_id ) : 
        ?>
            <div class="product-artist"><a href="<?php echo get_permalink( $artist_id ); ?>"><?php echo get_the_title( $artist_id ); ?></a></div>
    
        <?php endif;
    }
    
    ?>