📖[woocommerce的进阶记录]woocommerce插件记录

今日有使用wordpress的woocommerce插件制作一个商城类的项目,现在来做一下记录

 

woocommerce/archive-product.php 这个是woocommerce的产品列表模板,这里会展示当前分类下的产品

woocommerce/single-product.php 这个是woocommerce的产品详情模板,展示产品的详细信息

1

[woocommerce的进阶记录]woocommerce插件记录插图

//判断用户是否已经登录,并根据登录状态显示不同的功能
<div class="user" data-v-1aeb568f>
     <img src="<?php echo get_template_directory_uri(); ?>/static/picture/anma_man.2c1dede.png" alt="user" data-v-1aeb568f> 
     <?php 
          if (is_user_logged_in()) {  // 用户已登录  
               $current_user = wp_get_current_user();   
     ?>
          <p class="userTxt" data-v-1aeb568f><?php echo $current_user->display_name?></p>
          <a href="<?php echo wc_logout_url(); ?>">
               <button type="success" class="Out-login" data-v-1aeb568f>退出登录</button>
          </a> 
     <?php } else { ?>// 用户未登录
          <div class="btn btn-special" data-v-1aeb568f>
               <a href="<?php echo get_page_link(11) ?>">
                    <button type="success" class="btn-login" data-v-1aeb568f>登录</button>
               </a>
               <a href="<?php echo get_page_link(11) ?>">
                    <button class="btn-register" data-v-1aeb568f>注册</button>
               </a>
          </div>
     <?php }?>
</div>

2

[woocommerce的进阶记录]woocommerce插件记录插图1

<?php //获取指定ID的分类名,链接,缩略图
     $category_id = get_queried_object_id();
     $args = array(
          'hide_empty' => false,
          'taxonomy' => 'product_cat',
          'include' => [34,35,36,37,38,39,40,41,42,43,44],//这里设置需要的分类ID
     );
     $child_categories = get_terms('product_cat', $args);
     foreach ($child_categories as $child_category) {
          $category_link = get_term_link($child_category->slug, $child_category->taxonomy); 
          $category_id = $child_category->term_id;
          $category_name = $child_category->name;
          $category_description = $child_category->description;
          $category_thumbnail_id = get_woocommerce_term_meta($category_id, 'thumbnail_id', true);
          $category_thumbnail_url = wp_get_attachment_image_url($category_thumbnail_id, 'full');
?>
     <div data-v-7d745b42 class="swiper-no-swiping swiper-slide Banner_Div">
          <a href="<?php echo $category_link?>" target="_black">
               <div data-v-7d745b42 data-href2="2196" class="swiper-page-item">
                    <img data-v-7d745b42 src="<?php echo $category_thumbnail_url?>" alt="<?php echo $category_name?>" data-href2="2196" class="swiper-page-item-img">
               </div>
          </a>
     </div>
<?php } ?>

3

<a href="https://www.wuyanboke.cn/wp-content/uploads/2023/11/Snipaste_2023-11-20_16-50-49.jpg"><img src="https://www.wuyanboke.cn/wp-content/uploads/2023/11/Snipaste_2023-11-20_16-50-49.jpg" alt="" width="1200" height="393" class="alignnone size-full wp-image-1556" /></a>
<?php
     $args = array(  
          'post_type' => 'product',  //类型为产品
          'orderby' => 'price', // 根据价格排序 
          'order' => 'ASC',  
          'posts_per_page' => 10,//获取十个产品 
     );  
     $loop = new WP_Query($args);
     while ($loop->have_posts()) : $loop->the_post();global $product; ?>//开始循环
          <a class="item" href="<?php echo get_permalink($loop->post->ID) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>" data-v-6084bb54>
          <?php if (has_post_thumbnail($loop->post->ID)) echo get_the_post_thumbnail($loop->post->ID, 'Index_Size_Img');
            else echo '<img src="' . woocommerce_placeholder_img_src() . '" alt="Placeholder" data-v-6084bb54/>'; ?>
          <p class="info" data-v-6084bb54><?php the_title(); ?></p>
          <p class="price" data-v-6084bb54>
            <span data-v-6084bb54>
              <span><?php echo $product->get_price_html(); ?></span>
            </span>
            <span data-v-6084bb54>(含税¥)</span>
          </p>
     </a>
<?php endwhile; ?>
     <?php echo get_permalink($loop->post->ID) ?> //产品链接

     <?php the_title(); ?>//产品标题

     <?php if (has_post_thumbnail($loop->post->ID)) echo get_the_post_thumbnail($loop->post->ID, 'Index_Size_Img');else echo '<img src="' . woocommerce_placeholder_img_src() . '" alt="Placeholder" data-v-6084bb54/>'; ?>//判断当前产品是否有图片,没有的话调用缩略图

     <?php echo $product->get_price_html(); ?>//产品价格

标签

🧐发表评论

您必须启用javascript才能在此处查看验证码