【问题标题】:How can I display "Woocommerce Recently Viewed Products" horizontally just above the footer?如何在页脚上方水平显示“Woocommerce 最近查看的产品”?
【发布时间】:2017-11-02 13:24:05
【问题描述】:

我正在使用 avada 作为主题。我不会为最近查看的产品使用额外的插件。

我想添加“最近浏览的产品” - 包含在 woocommerce 选项中作为页面的简码。喜欢此页面上的示例:docs.woocommerce.com/document/woocommerce-shortcodes 例如,我可以使用此代码向页面添加“订单跟踪”选项。 [woocommerce_order_tracking] 如何添加小部件以显示在页脚区域之前的所有页面上?如何使该小部件水平而不是垂直?是否有任何用于添加“最近查看的产品”选项的简码?

请不要提出第 3 方插件。*

【问题讨论】:

  • 这在很大程度上取决于您使用的主题。您可以复制显示最近查看的产品的 php 代码并将其粘贴到页脚文件之前。由于没有主题,我们无法直接提供帮助。
  • 你有一个链接到你正在使用的最近查看的产品插件吗?

标签: php wordpress woocommerce hook-woocommerce


【解决方案1】:

我是这样解决的。对于那些需要的人:

 /**
 * @snippet       [recently_viewed_products] Shortcode - WooCommerce
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.6.2
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
add_shortcode( 'recently_viewed_products', 'bbloomer_recently_viewed_shortcode' );
 
function bbloomer_recently_viewed_shortcode() {
 
   $viewed_products = ! empty( $_COOKIE['woocommerce_recently_viewed'] ) ? (array) explode( '|', wp_unslash( $_COOKIE['woocommerce_recently_viewed'] ) ) : array();
   $viewed_products = array_slice($viewed_products, 0, 8);

 
   if ( empty( $viewed_products ) ) return;
    
   $title = '<h3 class="product-section-title container-width product-section-title-related pt-half pb-half uppercase">Du tittade senast på</h3>';
   $product_ids = implode( ",", $viewed_products );
 
   return $title . do_shortcode("[products ids='$product_ids']");
   
}

// adds notice at single product page above add to cart
add_action( 'woocommerce_after_single_product', 'recviproducts', 31 );
function recviproducts() {
    echo do_shortcode ('[recently_viewed_products]');
}

// 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 );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-03
    • 1970-01-01
    • 2020-11-04
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多