douxunzui1519
douxunzui1519
2015-11-17 15:13

WooCommerce链接到特定事物的相关产品

已采纳

I want to add a link to related product on the a with 'Obtenir en Grade Pro'. All my product have only 1 related element.

I am not able to have ONLY the link to the related product.

I am able to have the image, title and couple of thing of the related product but I can't have ONLY the link. How can I do that?

<?php
/**
 * The main template file.
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * E.g., it puts together the home page when no home.php file exists.
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package storefront
 */
get_header();
 ?> 

    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">
        <?php if( is_home() ) : ?>
            <section id="recent">

    <h2>Les différents bâtons</h2>

    <?php
     $args = array( 'post_type' => 'product', 'meta_key' => '_featured','posts_per_page' => 15,'columns' => '3', 'meta_value' => 'yes' );
     $loop = new WP_Query( $args );?>
     <ul class="products">
     <?php while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
        <li class="product">
            <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
            <h3><?php the_title(); ?></h3>
            <span class="price"><?php echo $product->get_price_html(); ?></span>
            <a>" title="<?php the_title(); ?>">
            <div class="hover-fiche"><p>Fiche complète du produit</p></div>
        </a>

//ADD THE LINK TO RELATED PRODUCT IN THE A
    <a href="LINK TO RELATED PRODUCT HERE">
    <div class="gradeprobutton">Obtenir en Grade Pro</div>
    <img src="wp-content/themes/storefront-child/images/info.png" alt="informations supplémentaires">
    <span class="gradepro">
        Bois de gamme *grade pro* sélectionné et confectionné dans un de nos 4 modèles. Il est seulement disponible dans notre édition spéciale pour la couleur.(99.99$)
    </span>
</a>

<!--Second tooltip-->
<!--<div class="gradeprodiv">
                     <a>post->ID ); ?>"><span class="gradeprobutton">Obtenir en grade Pro</span> <span class="info-icon"><div class="info"><p>Bois de gamme *grade pro* sélectionné et confectionné dans un de nos 4 modèles. Il est seulement disponible dans notre édition spécial pour la couleur </p></div></span></a>
</div> -->

                <?php
            /**
             * woocommerce_pagination hook
             *
             * @hooked woocommerce_pagination - 10
             * @hooked woocommerce_catalog_ordering - 20
             */
            do_action( 'woocommerce_pagination' );
        ?>
<?php endwhile; ?>

<?php wp_reset_query(); ?>

        <?php wp_reset_query(); ?>

</section><!-- /recent -->
        <?php else : ?>

            <?php get_template_part( 'content', 'none' ); ?>

        <?php endif; ?>

        </main><!-- #main -->
    </div><!-- #primary -->
<?php get_footer(); ?>

Thank you for everyone who will give a answer.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • donglei1616 donglei1616 6年前

    I have found the answer by myself.

    I was wrong, this was not the related product that I needed but upsells products. Here is the complete functional code:

                <section id="recent">
    <div class="wrapper">
        <h2>Les différents bâtons</h2>
    
        <?php
         $args = array( 'post_type' => 'product', 'meta_key' => '_featured','posts_per_page' => 15,'columns' => '3', 'meta_value' => 'yes' );
         $loop = new WP_Query( $args );?>
         <ul class="products">
         <?php while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
            <li class="product"> 
                <div class="div-info">   
                    <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
                    <h3><?php the_title(); ?></h3>
                    <span class="price"><span class="amount"><?php echo $product->get_price_excluding_tax(); ?> $</span> Prix sans les taxes</span>
                    <a id="fiche" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                        <div class="hover-fiche"><p>Fiche complète du produit</p></div>
                    </a>
                    <a class="fichecomplete" href="<?php the_permalink(); ?>">Voir la fiche compléte du produit </a>
                </div>    
                <?php
                    // Setup your custom query
                    $upsells = $product->get_upsells();
                    $meta_query = WC()->query->get_meta_query();
                    $args2 = array(
                        'post_type'           => 'product',
                        'ignore_sticky_posts' => 1,
                        'no_found_rows'       => 1,
                        'posts_per_page'      => 1,
                        'post__in'            => $upsells,
                        'post__not_in'        => array( $product->id ),
                        'meta_query'          => $meta_query
                    );
                    $loop2 = new WP_Query( $args2 );
                    while ( $loop2->have_posts() ) : $loop2->the_post(); ?>
    
            <a href="<?php echo get_permalink( $loop2->post->ID ) ?>" class="tooltip right tooltip-gradepro">
                <div class="gradeprobutton">Obtenir en grade Pro</div>
                <img src="wp-content/themes/storefront-child/images/info.png" alt="informations supplémentaires">
                <span class="gradepro">
                    Bois de gamme *grade pro* sélectionné et confectionné dans un de nos 4 modèles. Il est seulement disponible dans notre édition spéciale pour la couleur.<br /><b>(99.99$)</b>
                </span>
            </a>
            <?php endwhile; ?> <?php wp_reset_query(); // Remember to reset ?>
        </li>
                    <?php
                /**
                 * woocommerce_pagination hook
                 *
                 * @hooked woocommerce_pagination - 10
                 * @hooked woocommerce_catalog_ordering - 20
                 */
                do_action( 'woocommerce_pagination' );
            ?>
    <?php endwhile; ?>
    </ul>
    <?php wp_reset_query(); ?>
    
            <?php wp_reset_query(); ?>
    </div>
    

    点赞 评论 复制链接分享

相关推荐