php - 在 WooCommerce 单一产品自定义选项卡中显示交叉销售
问题描述
我正在尝试在单个产品页面的自定义选项卡中显示 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 单个产品页面自定义选项卡中显示交叉销售?
解决方案
产品交叉销售是针对购物车的,需要对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 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 文件中。测试和工作。
推荐阅读
- python - 将虚拟数据集读取到 NumPy 数组中时出现 h5py 错误
- sql - SQL Server Optimizer 删除 ISNULL 调用
- flutter - 找不到正确的提供者
- > 在这个 myorder Widget 上方
- google-bigquery - 将日期的计算转换为 INT
- git - 如何阻止用户在 Git 中同时处理同一个文件?
- c# - 如何在 Gridview 的“标题”中添加按钮 - c#
- python - 使用 rpy2 检查我是否安装了最新版本的 R 包
- popsql - 列的含义与第 1 行的值计数不匹配
- html - 如何在 Vue-plyr 中播放本地视频?
- node.js - 使用并发 API 请求管理数组数据结构