添加'按'排序使用woocommerce短代码下载自定义页面

时间:2015-10-12 19:18:00

标签: php wordpress woocommerce shortcode

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

我只需要默认排序'排序'下拉元素添加,但不知道如何做。

我在这里找到了一些代码>> Woocommerce, sort dropdown on shortcode based product lists

它确实将我需要的下拉放在网站上(虽然我喜欢它左对齐)。

现在唯一的问题是,它会在一个垂直列中显示带有大量图像的产品。

我不是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;
}

谢谢, 洛伦

0 个答案:

没有答案