【问题标题】:how to add default product sorting of woocommerce on the custom page template?如何在自定义页面模板上添加woocommerce的默认产品排序?
【发布时间】:2016-06-30 09:09:04
【问题描述】:

我创建了一个自定义页面模板,在该模板中我使用 WooCommerce 功能而不是模板加载产品列表。但现在我想在我的自定义页面模板上实现默认的 WooCommerce 排序功能。我该如何实施?

我不想使用 WooCommerce 模板。我只是想使用 WooCommerce 功能创建一个类似于默认值的排序功能。

这是我的自定义页面模板代码:

<?php
/**
 Template Name: Shop page custom layout template
 */

get_header(); ?>

<?php
    $full_product_list = array();
$loop = new WP_Query( array( 'post_type' =>array( 'product', 'product_variation' ), 'posts_per_page' => -1,) );

while ( $loop->have_posts() ) : $loop->the_post();
    $theid = get_the_ID();
    $product = new WC_Product($theid); ?>
    <?php $product_url=$product->add_to_cart_url(); ?>
    <ul><li><a href="<?php echo get_the_permalink(); ?>">
<?php
   echo '<h3>' . get_the_title() . '</h3>';
    echo woocommerce_get_product_thumbnail();
    if ( $product->is_on_sale() ) : 
       echo apply_filters( 'woocommerce_sale_flash', '<span class="onsale">' . __( 'Sale!', 'woocommerce' ) . '</span>', $post, $product ); 
    endif; 
    if ( $price_html = $product->get_price_html() ) : ?>
    <span class="price"><?php echo $price_html; ?></span>
    <?php endif; 
    if ( $rating_html = $product->get_rating_html() ) : ?>
    <?php echo $rating_html; ?>
<?php endif; ?>

  <?php $cart_url="/construction/shop/?add-to-cart=".$theid; ?>
   <a class="button product_type_simple add_to_cart_button ajax_add_to_cart" data-product_sku="" data-product_id="<?php echo $theid ?>" data-quantity="1" href="<?php echo $cart_url; ?>" rel="nofollow">Add to cart</a>
    </a></li></ul>
<?php
endwhile; wp_reset_query();

?>
<?php get_footer(); ?>

【问题讨论】:

    标签: php wordpress templates woocommerce


    【解决方案1】:

    没有“默认 Woocommerce 排序功能”之类的东西,因为显示的订单产品取决于附加到显示产品页面的功能。但假设您想按产品标题排序,请将您的查询更改为:

    $loop = new wp_query(array('post_type' =&gt;array('product', 'product_variation'), 'posts_per_page' =&gt; -1, 'orderby' =&gt; 'title', 'order' =&gt; 'ASC'));

    这将按标题升序排序,我猜这就是你想要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-04
      • 1970-01-01
      • 1970-01-01
      • 2021-06-20
      相关资源
      最近更新 更多