Cross sell output on product page and theme filter for them
Alex
Currently, Cross sell selected in the admin for products only affects the output of offered products on the cart page for cross-selling. This is a standard feature of WooCommerce.
Add settings in the theme to enable Cross sell output on the product page. And create a filter to display such products.
This will allow us to manage product offers more easily and flexibly, avoiding excessive custom code
Description of the difference between Related Products, Up-Sells and Cross-Sells: https://woocommerce.com/document/related-products-up-sells-and-cross-sells/
Alex
Related suggestion:
Deferred loading via Ajax of product blocks (upsells, related, cross sells etc.) and widgets - https://woodmart.canny.io/feature-requests/p/deferred-loading-via-ajax-of-product-blocks-upsells-related-cross-sells-etc-and
Alex
+ should also be added Cross sell в Gutenberg block "Products" (from the theme)
Alex
Now you can display it like this. But this is a list, and we need a carousel in the theme styles.
add_action( 'woocommerce_after_single_product', 'my_crossels', 20 );
function my_crossels( $limit = 2, $columns = 2, $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,
)
);
}