如何显示" Woocommerce最近浏览的产品"在页脚上方水平放置?

时间:2017-11-02 13:24:06

标签: php wordpress woocommerce hook-woocommerce

我正在使用avada作为主题。我没有为最近浏览过的产品使用额外的插件。

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

请不要提议第三方插件。* enter image description here

1 个答案:

答案 0 :(得分:0)

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

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