php - 在 WooCommerce 单一产品自定义选项卡中显示交叉销售

标签 php wordpress woocommerce tabs product

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

woocommerce_cross_sell_display();
但它不起作用(我没有收到错误,也没有视觉结果)。
这是我到目前为止尝试过的:
//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();
    }
它是有线的,因为加售工作正常,但交叉销售(基本上是相同的)不起作用。
如何在 WooCommerce 单个产品页面自定义选项卡中显示交叉销售?
Link to my website

最佳答案

产品交叉销售适用于购物车和 woocommerce_cross_sell_display() function 中的一些更改是必需的,以使其在产品单页中工作。
这可以通过在自定义函数中克隆该函数的代码并更改 this line 来完成。 :

$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 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,
        )
    );
}
代码位于事件子主题(或事件主题)的 functions.php 文件中。测试和工作。

关于php - 在 WooCommerce 单一产品自定义选项卡中显示交叉销售,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67007158/

相关文章:

wordpress - 自定义产品上缺少“添加到购物车”按钮

php - Laravel 获取所有带有帖子数的类别

javascript - 如何在 jQuery 中传递事件和 $ 作为参数(在 wordpress 中启用无冲突模式)

php - 自定义帖子类型中的 WooCommerce 产品

php - Javascript .replace 在页面加载时被重写

javascript - Lightbox CSS/JS 画廊集成到 WordPress 图像上传

woocommerce - 我在哪里可以为 woocommerce 包装器添加另一个类?

php - MySQL 到 php 临时结果

php - 无法在Laravel命令中创建多个Redis连接

PHP 无法将变量传递给包含的文件