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

3 sujets de 1 à 3 (sur un total de 3)
  • Auteur
    Messages
  • #126019
    folly29
    Participant
    Points: 84 pts

    Bonjour,

    Je sollicite votre aide car j’ai un problème que je n’arrive pas a résoudre. Je m’explique. J’ai ajouté un nouvel onglets dans l’admin de wordpress qui s’intitule Groupes de musique, je me sert de ce custom post pour créer tout les pages des groupes de musique. J’Ai aussi un catalogue produit (woocommerce) avec plusieurs produits (cd, vinyl …). J’aimerais si c’est possible ajouter sur la page admin de mon produit, la liste des groupes de musiques (en mode case a cocher) et ainsi pouvoir relier un produit a un groupes de musiques. Ensuite j’aimerais pouvoir afficher sur chaque page du groupes de musiques les produits qui y sont relier.

    J’espère avoir était assez clair. Si quelqu’un peut m’aider ce serait vraiment sympa.

    Merci d’avance,

    Romain

    depanagewp
    #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
    #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
3 sujets de 1 à 3 (sur un total de 3)
  • Vous devez être connecté pour répondre à ce sujet.