douxunzui1519 2015-11-17 15:13
浏览 32
已采纳

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 2015-11-25 19:32
    关注

    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>
    

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题