是否为WooCommerce查询快捷代码分配最小结果计数?

时间:2017-12-19 作者:Louis W

我使用内置的查询短代码来显示访问者最近查看的产品,如下所示:

 <?=do_shortcode(\'[recent_products limit="4"]\'); ?>
我看到的问题是,当你第一次访问这个网站时,你可能会得到少于4个结果,而网格看起来并不太好。我宁愿把整个部分都藏在一起。

我回顾了documentation 而且,除了将整个查询复制为PHP之外,想不出其他解决方案。有人有什么想法吗?

1 个回复
最合适的回答,由SO网友:Kieran McClung 整理而成

我不认为最近查看的产品有原生的短代码,但这里有两种选择。一种是使用具有唯一侧栏的小部件,将其包装在一个条件中,或者您可以创建自己的函数/短代码。

1. Conditional sidebar

将以下代码添加到函数中。php文件。这将启用一个新的侧栏(小部件区域),并创建一个助手函数供以后使用。

functions.php

function register_custom_sidebar()
{
    // Register new sidebar
    register_sidebar( array(
        \'name\' => __( \'Custom Sidebar\', \'stack-exchange\' ),
        \'id\' => \'custom-sidebar\',
        \'description\' => __( \'This sidebar is used only for recently view products.\', \'stack-exchange\' ),
        \'before_widget\' => \'<section id="%1$s" class="widget %2$s">\',
        \'after_widget\'  => \'</section>\',
        \'before_title\'  => \'<h2 class="widget-title">\',
        \'after_title\'   => \'</h2>\',
    ) );    
}
add_action( \'widgets_init\', \'register_custom_sidebar\' );

function get_recently_viewed_products_count()
{   
    // Create helper function to check number of recently viewed products
    $viewed_products = ! empty( $_COOKIE[\'woocommerce_recently_viewed\'] ) ? (array) explode( \'|\', $_COOKIE[\'woocommerce_recently_viewed\'] ) : array();  
    return count( $viewed_products );
}
在模板中,使用我们的新功能检查最近查看的项目是否足够。如果有,请加载侧栏和小部件。

您需要通过转到在管理区域中添加小部件Appearance > Widgets 并查找最近查看的产品小部件。将其拖到新创建的侧栏中(通过示例自定义侧栏)。

Template file

<?php
// Use our function to check count of viewed products
// If true (i.e. >= 4) load the sidebar/widget
if ( get_recently_viewed_products_count() >= 4 ) {
    dynamic_sidebar( \'custom-sidebar\' );
}
?>

2. Custom shortcode

在函数中创建新函数。php文件如下。这或多或少是/woocommerce/includes/widgets/class-wc-widget-recently-viewed.php

functions.php

function recently_viewed_products_function( $atts )
{
    // Get viewed products
    $viewed_products = ! empty( $_COOKIE[\'woocommerce_recently_viewed\'] ) ? (array) explode( \'|\', $_COOKIE[\'woocommerce_recently_viewed\'] ) : array();

    // Reverse array (order of view)
    $viewed_products = array_reverse( array_filter( array_map( \'absint\', $viewed_products ) ) );

    // If there are no $viewed_products return;
    if ( empty( $viewed_products ) ) {
        return;
    }

    // Get limit, if not found set to 4
    ( isset( $atts[\'limit\'] ) ? $limit = $atts[\'limit\'] : $limit = 4 );

    // If number of $viewed_products is greater than or equal to $limit carry on
    if ( count( $viewed_products ) >= $limit ) {

        ob_start();

        // Set query args using $limit and $viewed_products
        $query_args = array(
            \'posts_per_page\' => $limit,
            \'no_found_rows\'  => 1,
            \'post_status\'    => \'publish\',
            \'post_type\'      => \'product\',
            \'post__in\'       => $viewed_products,
            \'orderby\'        => \'post__in\',
        );

        $query = new WP_Query( $query_args );

        if ( $query->have_posts() ) :
            echo \'<ul class="products">\';

            while ( $query->have_posts() ) : $query->the_post();

                // Output product content
                // I\'ve used the default as standard but you can do anything here
                wc_get_template_part( \'content\', \'product\' );

            endwhile;

            echo \'</ul>\';
        endif;

        wp_reset_postdata();

        $content = ob_get_clean();

        echo $content;

    } else {
        return;
    }
}
add_shortcode( \'recently_viewed_products\', \'recently_viewed_products_function\' );

// This function is required to set the cookie for recently viewed products
// Thanks for @louis-w for the heads-up (https://github.com/woocommerce/woocommerce/issues/9724#issuecomment-160618200)
function custom_track_product_view() {
    if ( ! is_singular( \'product\' ) ) {
        return;
    }

    global $post;

    if ( empty( $_COOKIE[\'woocommerce_recently_viewed\'] ) )
        $viewed_products = array();
    else
        $viewed_products = (array) explode( \'|\', $_COOKIE[\'woocommerce_recently_viewed\'] );

    if ( ! in_array( $post->ID, $viewed_products ) ) {
        $viewed_products[] = $post->ID;
    }

    if ( sizeof( $viewed_products ) > 15 ) {
        array_shift( $viewed_products );
    }

    // Store for session only
    wc_setcookie( \'woocommerce_recently_viewed\', implode( \'|\', $viewed_products ) );
}

add_action( \'template_redirect\', \'custom_track_product_view\', 20 );
回显模板中的短代码,您就可以这样做了。

Template file

<?= do_shortcode( \'[recently_viewed_products limit="2"]\' ); ?>

结束

相关推荐

Append HTML Using Shortcode

我写了一个WordPress插件,它有一个短代码。短代码位于WordPress页面上,该页面上有其他短代码。其他短代码输出HTML,我想将我的短代码中的HTML附加到该输出中。我想知道是否可以使用DOMDocument和getElementById来实现这一点。WordPress页面如下所示:[Shortcode #1] [Shortcode #2] [Shortcode #3] [date_info] 我的插件执行以下操作:function