使用 woocommerce 短代码在自定义页面上添加“排序依据”下拉列表

2023-12-12

我使用 woocommerce 短代码为产品类别创建了一个自定义页面。目前,它非常稀疏,因为我才刚刚开始使用新网站。

我只需要添加默认的“排序依据”下拉元素,但不知道该怎么做。

我在这里找到了一些代码>>Woocommerce,基于短代码的产品列表上的排序下拉列表

它确实将我需要的下拉菜单放置在网站上(尽管我希望它左对齐)。

现在唯一的问题是,它用巨大的图像显示产品,并且全部显示在一个垂直列中。

我不是 PHP 程序员,想知道我是否删除了我修改的 php 文件中的一些重要代码。 (感谢上帝,我拿了一份原件的副本!)。

有人可以帮忙吗?有没有更好的方法将下拉排序添加到我的自定义页面上?

这是我创建的页面之一>>http://www.sdmtest1.co.uk/notebooks-journals/

这是我添加的代码。

/**
 * List products in a category shortcode
 *
 * @param array $atts
 * @return string
 */
public static function product_category( $atts ) {
    $atts = shortcode_atts( array(
        'per_page' => '12',
        'columns'  => '4',
        'orderby'  => 'title',
        'order'    => 'desc',
        'category' => '',  // Slugs
        'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
    ), $atts );

    if ( ! $atts['category'] ) {
        return '';
    }

    // Default ordering args
    $ordering_args = WC()->query->get_catalog_ordering_args( $atts['orderby'],    
$atts['order'] );
$orderby = 'title';
$order = 'asc';
if ( isset( $_GET['orderby'] ) ) {
    $getorderby = $_GET['orderby'];
}
if ($getorderby == 'popularity') {
    $orderby = 'meta_value_num';
    $order = 'desc';
    $meta_key = 'total_sales';
} elseif ($getorderby == 'rating') {

    $fields .= ", AVG( $wpdb->commentmeta.meta_value ) as average_rating ";

    $where .= " AND ( $wpdb->commentmeta.meta_key = 'rating' OR $wpdb->commentmeta.meta_key IS null ) ";

    $join .= "
        LEFT OUTER JOIN $wpdb->comments ON($wpdb->posts.ID = $wpdb->comments.comment_post_ID)
        LEFT JOIN $wpdb->commentmeta ON($wpdb->comments.comment_ID = $wpdb->commentmeta.comment_id)
    ";

    $orderby = "average_rating DESC, $wpdb->posts.post_date DESC";

    $groupby = "$wpdb->posts.ID";   

} elseif ($getorderby == 'date') {
    $orderby = 'date';
    $order = 'desc';
} elseif ($getorderby == 'price') {
    $orderby = 'meta_value_num';
    $order = 'asc';
    $meta_key = '_price';
} elseif ($getorderby == 'price-desc') {
    $orderby = 'meta_value_num';
    $order = 'desc';
    $meta_key = '_price';
}
$args = array(
    'post_type'             => 'product',
    'post_status'           => 'publish',
    'ignore_sticky_posts'   => 1,
    'orderby'               => $orderby, // $ordering_args['orderby'],
    'order'                 => $order, // $ordering_args['order'],
    'meta_key'              => $meta_key,
    'fields'                => $fields,
    'where'                 => $where,
    'join'                  => $join,
    'groupby'               => $groupby,
    'posts_per_page'        => $per_page,
    'meta_query'            => array(
        array(
            'key'           => '_visibility',
            'value'         => array('catalog', 'visible'),
            'compare'       => 'IN'
        )
    ),
    'tax_query'             => array(
        array(
            'taxonomy'      => 'product_cat',
            'terms'         => array( esc_attr( $category ) ),
            'field'         => 'slug',
            'operator'      => $operator
        )
    )
);

if ( isset( $ordering_args['meta_key'] ) ) {
    $args['meta_key'] = $ordering_args['meta_key'];
}

ob_start();

$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $args, $atts ) );

$woocommerce_loop['columns'] = $columns;

if ( $products->have_posts() ) : ?>

<div style="width:100%;">
    <div style="float:right">
        <form class="woocommerce-ordering" method="get">
            <select name="orderby" class="orderby">
                <?php
                    $catalog_orderby = apply_filters( 'woocommerce_catalog_orderby', array(
                        'menu_order' => __( 'Default sorting', 'woocommerce' ),
                        'popularity' => __( 'Sort by popularity', 'woocommerce' ),
                        'rating'     => __( 'Sort by average rating', 'woocommerce' ),
                        'date'       => __( 'Sort by newness', 'woocommerce' ),
                        'price'      => __( 'Sort by price: low to high', 'woocommerce' ),
                        'price-desc' => __( 'Sort by price: high to low', 'woocommerce' )
                    ) );

                    if ( get_option( 'woocommerce_enable_review_rating' ) === 'no' )
                        unset( $catalog_orderby['rating'] );

                    foreach ( $catalog_orderby as $id => $name )
                        echo '<option value="' . esc_attr( $id ) . '" ' . selected( $getorderby, $id, false ) . '>' . esc_attr( $name ) . '</option>';
                ?>
            </select>
            <?php
                // Keep query string vars intact
                foreach ( $_GET as $key => $val ) {
                    if ( 'orderby' === $key || 'submit' === $key )
                        continue;

                    if ( is_array( $val ) ) {
                        foreach( $val as $innerVal ) {
                            echo '<input type="hidden" name="' . esc_attr( $key ) . '[]" value="' . esc_attr( $innerVal ) . '" />';
                        }

                    } else {
                        echo '<input type="hidden" name="' . esc_attr( $key ) . '" value="' . esc_attr( $val ) . '" />';
                    }
                }
            ?>
        </form>
    </div>
</div>
<div style="clear:both;"></div>

    <?php woocommerce_product_loop_start(); ?>

        <?php while ( $products->have_posts() ) : $products->the_post(); ?>

            <?php wc_get_template_part( 'content', 'product' ); ?>

        <?php endwhile; // end of the loop. ?>

    <?php woocommerce_product_loop_end(); ?>

<?php endif;

woocommerce_reset_loop();
wp_reset_postdata();

return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';

}

我在 Dreamweaver 中编辑,它标记了语法错误。我不确定我粘贴的代码在哪里开始和结束,在这个过程中我有点丢失了这一点。

        // Remove ordering query arguments
    WC()->query->remove_ordering_args();

    return $return;
}

谢谢, 罗伦


如果您添加paginate="true"归因于你的[products]短代码,然后是短代码,然后是Sort by页面上将出现下拉菜单。

我使用 woocommerce 简码为产品类别创建了一个自定义页面。

您说您正在使用短代码,但没有提供示例。那我就补一张吧

如果您使用它来显示“靴子”类别中的所有产品:

[product_category category="boots"]

然后将其更改为这样:

[product_category category="boots" paginate="true"]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 woocommerce 短代码在自定义页面上添加“排序依据”下拉列表 的相关文章

随机推荐