我需要一些帮助来显示我的购物车中的所有产品在结帐页上的追加销售,下面的代码给我以下错误:
对数组的成员函数get_upsell_ids()的调用在...
下面是我的函数:
function show_cross_sell( $limit = '-1', $columns = 4, $orderby = 'rand', $order = 'desc' ) {
global $woocommerce;
$items = WC()->cart->get_cart();
if ( ! $items ) {
echo 'nothing';
return;
}
// Handle the legacy filter which controlled posts per page etc.
$args = apply_filters(
'woocommerce_upsell_display_args',
array(
'posts_per_page' => $limit,
'orderby' => $orderby,
'order' => $order,
'columns' => $columns,
)
);
foreach($items as $product) {
wc_set_loop_prop( 'name', 'up-sells' );
wc_set_loop_prop( 'columns', apply_filters( 'woocommerce_upsells_columns', isset( $args['columns'] ) ? $args['columns'] : $columns ) );
$orderby = apply_filters( 'woocommerce_upsells_orderby', isset( $args['orderby'] ) ? $args['orderby'] : $orderby );
$order = apply_filters( 'woocommerce_upsells_order', isset( $args['order'] ) ? $args['order'] : $order );
$limit = apply_filters( 'woocommerce_upsells_total', isset( $args['posts_per_page'] ) ? $args['posts_per_page'] : $limit );
// Get visible upsells then sort them at random, then limit result set.
$upsells = wc_products_array_orderby( array_filter( array_map( 'wc_get_product', $product->get_upsell_ids() ), 'wc_products_array_filter_visible' ), $orderby, $order );
$upsells = $limit > 0 ? array_slice( $upsells, 0, $limit ) : $upsells;
wc_get_template(
'single-product/up-sells.php',
array(
'upsells' => $upsells,
// Not used now, but used in previous version of up-sells.php.
'posts_per_page' => $limit,
'orderby' => $orderby,
'columns' => $columns,
)
);
}
}
1条答案
按热度按时间xxls0lw81#
您的代码中存在一些错误...以下代码将合并购物车项目中的所有追加销售ID(不重复),并将它们显示为唯一的追加销售块:
代码进入你的活动子主题(或活动主题)的function.php文件。测试和工作。
用途:
echo show_items_upsells();