web-dev-qa-db-fra.com

Woocommerce

J'utilise WooCommerce sur un site construit avec Pagelines Framework. J'ai besoin qu'un sous-titre/champ personnalisable apparaisse sous le nom du produit, où qu'il apparaisse sur le site. En l'état actuel des choses, WooCommerce n'offre pas cette option.

J'ai essayé d'utiliser des champs personnalisés, mais WooCommerce les utilise aussi et génère un tas de choses que je ne veux pas avec mes sous-titres. Si je devais nommer mon champ personnalisé "bookauthor", ce code fonctionnerait-il pour afficher uniquement le champ personnalisé que je veux?

<?php echo get_post_meta($id, "bookauthor", true); ?>

Et si oui, comment puis-je créer ma nouvelle sortie sur le terrain immédiatement après le titre du produit sur le frontal?

J'ai trouvé les crochets dont j'ai besoin dans ce fichier php (je pense, je ne connais pas php, c'est pourquoi je vous le demande):

    <?php 
/*
  * @hooked woocommerce_template_single_title - 5
  * @hooked woocommerce_template_single_price - 10
  * @hooked woocommerce_template_single_excerpt - 20
  * @hooked woocommerce_template_single_add_to_cart - 30
  * @hooked woocommerce_template_single_meta - 40
  * @hooked woocommerce_template_single_sharing - 50
*/
?>

Je sais comment filtrer, mais comment puis-je ajouter un champ personnalisé à cette liste?

Ou existe-t-il un moyen totalement différent de réaliser ce dont j'ai besoin?

Une gratitude sans faille pour tous ceux qui peuvent aider.

8
loulousantaana

Pour répondre à votre première question, obtenir votre post meta "bookauthor" de cette manière fera écho/affichage. Si vous définissez la variable $id dans votre code, vous pouvez le faire comme indiqué ci-dessous.

Le code devrait répondre à votre deuxième question, comment insérer votre deuxième ligne de titre dans la page du produit via le crochet woocommerce_single_product_summary. Ajoutez simplement vos informations supplémentaires comme ceci:

    function wpse116660_wc_add_2nd_title() {
        ?>
        <div class="2nd-tile">
            <?php echo get_post_meta(get_the_ID(), "bookauthor", true); ?>
        </div>
        <?php
    }
    add_action( 'woocommerce_single_product_summary', 'wpse116660_wc_add_2nd_title', 6 );

Pour avoir plus de confort avec votre post meta personnalisé, vous pouvez faire ce que @ pl4g4 et @brasofilo ont suggéré et ajouter un metabox à l'écran d'édition du produit, mais ce n'est pas nécessaire, bien sûr, vous semblez savoir le faire avec le wordpress standard metabox des champs personnalisés .


Vous pouvez ajouter votre meta box comme ceci, le code est basé sur le premier exemple de la page de codex add_meta_box wordpress.

/**
 * Adds a box to the main column on the Post and Page edit screens.
 */
function wpse116660_wc_2nd_title_mb() {

    $screen = array( 'product' );

        add_meta_box(
            'wc_2nd_title_mb',
            __( '2nd title', 'your_textdomain' ),
            'wc_2nd_title_inner_mb',
            $screen,
            'advanced',
            'high'
        );
}
add_action( 'add_meta_boxes', 'wpse116660_wc_2nd_title_mb', 0 );

/**
 * Prints the box content.
 * 
 * @param WP_Post $post The object for the current post/page.
 */
function wpse116660_wc_2nd_title_inner_mb( $post ) {

  // Add an nonce field so we can check for it later.
  wp_nonce_field( 'wc_2nd_title_inner_mb', 'wc_2nd_title_inner_mb_nonce' );

  /*
   * Use get_post_meta() to retrieve an existing value
   * from the database and use the value for the form.
   */
  $value = get_post_meta( $post->ID, 'bookauthor', true );

  echo '<label for="bookauthor_field">';
       _e( "Bookauthor", 'your_textdomain' );
  echo '</label> ';
  echo '<input type="text" id="bookauthor_field" name="bookauthor_field" value="' . esc_attr( $value ) . '" size="50" />';

}

/**
 * When the post is saved, saves our custom data.
 *
 * @param int $post_id The ID of the post being saved.
 */
function wpse116660_wc_2nd_title_save_postdata( $post_id ) {

  /*
   * We need to verify this came from the our screen and with proper authorization,
   * because save_post can be triggered at other times.
   */

  // Check if our nonce is set.
  if ( ! isset( $_POST['wc_2nd_title_inner_mb_nonce'] ) )
    return $post_id;

  $nonce = $_POST['wc_2nd_title_inner_mb_nonce'];

  // Verify that the nonce is valid.
  if ( ! wp_verify_nonce( $nonce, 'wc_2nd_title_inner_mb' ) )
      return $post_id;

  // If this is an autosave, our form has not been submitted, so we don't want to do anything.
  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
      return $post_id;

  // Check the user's permissions.
  if ( 'page' == $_POST['post_type'] ) {

    if ( ! current_user_can( 'edit_page', $post_id ) )
        return $post_id;

  } else {

    if ( ! current_user_can( 'edit_post', $post_id ) )
        return $post_id;
  }

  /* OK, its safe for us to save the data now. */

  // Sanitize user input.
  $mydata = sanitize_text_field( $_POST['bookauthor_field'] );

  // Update the meta field in the database.
  update_post_meta( $post_id, 'bookauthor', $mydata );
}
add_action( 'save_post', 'wpse116660_wc_2nd_title_save_postdata' );
5
Nicolai

Vous pouvez ajouter un metabox supplémentaire à la publication du produit. Cette meta-box doit avoir et entrer un formulaire pour pouvoir entrer le sous-titre. Lorsque vous avez ajouté le metabox, sauvegardez la valeur dans le post_meta lors de la sauvegarde du produit, puis dans la page produit unique du modèle woocommerce, utilisez le code

<?php echo get_post_meta($id, "bookauthor", true); ?>

pour l'obtenir.

vous pouvez trouver des infos sur metaboxess Ici Et aussi Ici

3
pl4g4