WooFrance Dépannage et support WordPress WooCommerce Forums Forum de Support WooCommerce Relier un ou plusieurs produit(s) a un custom post

2 sujets de 1 à 2 (sur un total de 2)
  • Auteur
    Messages
  • #126035
    Harald
    Participant
    Points: 94 pts
    Modérateur

    Salut,

    Si tu t’y connais un peu en développement je peux te donner une piste de réflexion :

    <?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' ) );
    	$artists = get_posts( array( 'numberposts' => -1 ) ) ; 
    
        array_walk( $artists, 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 ) : ?> <?php endif; } ?>
    ====================
    depanagewp
    vpsa2
    #126048
    folly29
    Participant
    Points: 84 pts

    Merci Harald mais je suis vraiment très mauvais en développement.

    Voici mon code pour la création de mon custom post : https://framabin.org/p/?c76e5a2c1618457e#GWxorlLxS4w8cMh3l/dSMtmlikjL3qHuOD0Xl7Okm3M=

    Avec l’exemple que tu m’a donné, je ne vois pas comment relier un de mes custom post a un produit.

    Peux tu m’aider stp ?

    Merci d’avance.

    ====================
    depanagewp
    vpsa2
2 sujets de 1 à 2 (sur un total de 2)
  • Vous devez être connecté pour répondre à ce sujet.