我不认为最近查看的产品有原生的短代码,但这里有两种选择。一种是使用具有唯一侧栏的小部件,将其包装在一个条件中,或者您可以创建自己的函数/短代码。
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"]\' ); ?>