在 WooCommerce 单一产品自定义选项卡中显示交叉销售 [英] Display cross sells inside WooCommerce single products custom tab

查看:113
本文介绍了在 WooCommerce 单一产品自定义选项卡中显示交叉销售的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在单个产品页面的自定义选项卡内显示 WooCommerce 交叉销售(与评论"和附加信息"类似的选项卡).为此,我尝试使用 WooCommerce 功能:

woocommerce_cross_sell_display();

但它不起作用(我没有收到错误和视觉结果).

这是我目前尝试过的:

//添加自定义标签过滤器add_filter('woocommerce_product_tabs', 'add_new_default_product_tab');函数 add_new_default_product_tab( $tabs ) {全球$产品;//将新的优先级设置为评论";标签$tabs['reviews']['priority'] = 20;//获取用作新标签页标题和标签的值$custom_tab_title = "אביזרים";$custom_tab_title2 = אביזרים משלימים";//如果设置了自定义字段,则添加新选项卡如果(!空($custom_tab_title)){$tabs['awp-' .sanitize_title('道具')] = 数组('标题' =>'אביזרים','回调' =>'awp_custom_woocommerce_tabs','优先级' =>5);}如果(!空($custom_tab_title)){$tabs['awp-' .sanitize_title('附加道具')] = array('标题' =>'אביזרים משלימים','回调' =>'awp_custom_woocommerce_tabs2','优先级' =>10);}返回 $tabs;}//回调以显示加售(WORKS)功能 awp_custom_woocommerce_tabs($key, $tab) {woocommerce_upsell_display(3,3);}//回调显示交叉销售(不起作用)功能 awp_custom_woocommerce_tabs2($key, $tab) {woocommerce_cross_sell_display();}

它是有线的,因为追加销售工作正常,但交叉销售(基本上是相同的)不起作用.

如何在 WooCommerce 单个产品页面自定义选项卡中显示交叉销售?

链接到我的网站

解决方案

产品交叉销售适用于购物车和 woocommerce_cross_sell_display() 函数是必需的,以使其在产品单页中工作.>

这可以通过在自定义函数中克隆该函数的代码并更改 这一行:

$cross_sells = array_filter(array_map('wc_get_product', WC()->cart->get_cross_sells()), 'wc_products_array_filter_visible');

$cross_sells = array_filter( array_map( 'wc_get_product', $product->get_cross_sell_ids() ), 'wc_products_array_filter_visible' );

所以你的最终代码将是:

//添加自定义标签过滤器add_filter('woocommerce_product_tabs', 'add_new_default_product_tab');函数 add_new_default_product_tab( $tabs ) {全球$产品;//将新的优先级设置为评论";标签$tabs['reviews']['priority'] = 20;//获取用作新标签页标题和标签的值$custom_tab_title = "אביזרים";$custom_tab_title2 = אביזרים משלימים";//如果设置了自定义字段,则添加新选项卡如果(!空($custom_tab_title)){$tabs['awp-' .sanitize_title('道具')] = 数组('标题' =>'אביזרים','回调' =>'woocommerce_upsell_display_in_tab','优先级' =>5);}如果(!空($custom_tab_title)){$tabs['awp-' .sanitize_title('附加道具')] = array('标题' =>'אביזרים משלימים','回调' =>'woocommerce_cross_sell_display_in_tab','优先级' =>10);}返回 $tabs;}//回调以显示加售函数 woocommerce_upsell_display_in_tab() {woocommerce_upsell_display(3, 3);}//回调以显示交叉销售函数 woocommerce_cross_sell_display_in_tab( $limit = 3, $columns = 3, $orderby = 'rand', $order = 'desc' ) {全球$产品;$cross_sells = array_filter(array_map('wc_get_product', $product->get_cross_sell_ids()), 'wc_products_array_filter_visible');wc_set_loop_prop('name', 'cross-sells');wc_set_loop_prop('columns', apply_filters('woocommerce_cross_sells_columns', $columns));//处理 orderby 和限制结果.$orderby = apply_filters( 'woocommerce_cross_sells_orderby', $orderby );$order = apply_filters( 'woocommerce_cross_sells_order', $order );$cross_sells = wc_products_array_orderby( $cross_sells, $orderby, $order );$limit = apply_filters( 'woocommerce_cross_sells_total', $limit );$cross_sells = $limit >0 ?array_slice( $cross_sells, 0, $limit ) : $cross_sells;wc_get_template('购物车/cross-sells.php',大批('cross_sells' =>$cross_sells,//现在不使用,但在 up-sells.php 的以前版本中使用.'posts_per_page' =>$限制,'orderby' =>$orderby,'列' =>$列,));}

代码位于活动子主题(或活动主题)的functions.php 文件中.经测试有效.

I'm trying to display WooCommerce cross sells inside a custom tab in single product pages (similar tab as "Reviews" and "Additional information"). For that I tried using WooCommerce function:

woocommerce_cross_sell_display();

But it doesn't work (I receive no error and no visual result).

Here is what I tried so far:

//Add custom tabs filter
add_filter('woocommerce_product_tabs', 'add_new_default_product_tab' );
function add_new_default_product_tab( $tabs ) {

    global $product;

    // set the new priority to the "reviews" tab
    $tabs['reviews']['priority'] = 20;

    // gets the value to use as the title and slug of the new tab
    $custom_tab_title = "אביזרים";
    $custom_tab_title2 = "אביזרים משלימים";

    // if the custom field is set, it adds the new tab
    if ( ! empty($custom_tab_title) ) {
        $tabs['awp-' . sanitize_title('props')] = array(
            'title' => 'אביזרים',
            'callback' => 'awp_custom_woocommerce_tabs',
            'priority' => 5
        );
    }
    if ( ! empty($custom_tab_title) ) {
        $tabs['awp-' . sanitize_title('additional-props')] = array(
            'title' => 'אביזרים משלימים',
            'callback' => 'awp_custom_woocommerce_tabs2',
            'priority' => 10
        );
    }
    return $tabs;
}
 
    //Callback to display upsells (WORKS)
    
    function awp_custom_woocommerce_tabs($key, $tab) {
         woocommerce_upsell_display( 3,3 );
    }
    
    
    //Callback to display cross sells (Doesn't work)
    
    function awp_custom_woocommerce_tabs2($key, $tab) {
        woocommerce_cross_sell_display();
    }

Its wired because upsells working fine, but cross sells (which basically is the same) doesn't work.

How to display cross sells inside WooCommerce single product pages custom tab?

Link to my website

解决方案

Product cross sells are for cart and some changes in woocommerce_cross_sell_display() function are required, to get it work in product single pages.

This can be done cloning the code from that function in a custom function and changing this line:

$cross_sells = array_filter( array_map( 'wc_get_product', WC()->cart->get_cross_sells() ), 'wc_products_array_filter_visible' );

with

$cross_sells = array_filter( array_map( 'wc_get_product', $product->get_cross_sell_ids() ), 'wc_products_array_filter_visible' );

So your final code is going to be:

// Add custom tabs filter
add_filter('woocommerce_product_tabs', 'add_new_default_product_tab' );
function add_new_default_product_tab( $tabs ) {

    global $product;

    // set the new priority to the "reviews" tab
    $tabs['reviews']['priority'] = 20;

    // gets the value to use as the title and slug of the new tab
    $custom_tab_title = "אביזרים";
    $custom_tab_title2 = "אביזרים משלימים";

    // if the custom field is set, it adds the new tab
    if ( ! empty($custom_tab_title) ) {
        $tabs['awp-' . sanitize_title('props')] = array(
            'title' => 'אביזרים',
            'callback' => 'woocommerce_upsell_display_in_tab',
            'priority' => 5
        );
    }
    if ( ! empty($custom_tab_title) ) {
        $tabs['awp-' . sanitize_title('additional-props')] = array(
            'title' => 'אביזרים משלימים',
            'callback' => 'woocommerce_cross_sell_display_in_tab',
            'priority' => 10
        );
    }
    return $tabs;
}

// Callback to display upsells
function woocommerce_upsell_display_in_tab() {
     woocommerce_upsell_display( 3, 3 );
}


// Callback to display cross sells
function woocommerce_cross_sell_display_in_tab( $limit = 3, $columns = 3, $orderby = 'rand', $order = 'desc' ) {
    global $product;

    $cross_sells = array_filter( array_map( 'wc_get_product', $product->get_cross_sell_ids() ), 'wc_products_array_filter_visible' );

    wc_set_loop_prop( 'name', 'cross-sells' );
    wc_set_loop_prop( 'columns', apply_filters( 'woocommerce_cross_sells_columns', $columns ) );

    // Handle orderby and limit results.
    $orderby     = apply_filters( 'woocommerce_cross_sells_orderby', $orderby );
    $order       = apply_filters( 'woocommerce_cross_sells_order', $order );
    $cross_sells = wc_products_array_orderby( $cross_sells, $orderby, $order );
    $limit       = apply_filters( 'woocommerce_cross_sells_total', $limit );
    $cross_sells = $limit > 0 ? array_slice( $cross_sells, 0, $limit ) : $cross_sells;

    wc_get_template(
        'cart/cross-sells.php',
        array(
            'cross_sells'    => $cross_sells,

            // Not used now, but used in previous version of up-sells.php.
            'posts_per_page' => $limit,
            'orderby'        => $orderby,
            'columns'        => $columns,
        )
    );
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

这篇关于在 WooCommerce 单一产品自定义选项卡中显示交叉销售的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆