plugins – connect single product page woocommerce

Question

I would like to have a code that is the same us the one below but the code should collect all the pages instead of posts and place them in a dropdown. The value of this field will be visible on the single product page below the price.

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 ”;
woocommerce_wp_select(
array(
‘id’ => ‘_artist’,
‘label’ => __( ‘Artist’ ),
‘options’ => $posts
)
);
echo ”;

}

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;

}

?>

0
kennedy mokua 4 months 2022-04-18T08:20:38-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse