wordpress - connect woocommerce products to posts -


i using woocommerce on wordpress site. selling paintings. products paintings. have list of artists posts. each artist 1 post. connect posts , products can show artist's name on painting page , user can click on name , takes them artist post. how do this?

this example of how add custom field in woocommerce product general tab. since artists posts ( category not specified ), collect links posts , place them in dropdown. value of field visible on single product page below price ( can open content-single-product.php file in woocommerce theme, see actions single product template, , functions attached, , change priority of woocommerce_product_artist function if want change place link 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 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 sanitize 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 , 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 woocommerce_product_artist function on woocommerce_single_product_summary action ( priority 15 )     add_action( 'woocommerce_single_product_summary', 'woocommerce_product_artist', 15 );  }  function woocommerce_product_artist() {     global $post;      // artist page id , 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; }  ?> 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -