Skip to content

Instantly share code, notes, and snippets.

@bogdanpopa90
Last active February 21, 2017 16:29
Show Gist options
  • Save bogdanpopa90/f80537d2c3ab76213d80f02bfdf0f3a1 to your computer and use it in GitHub Desktop.
Save bogdanpopa90/f80537d2c3ab76213d80f02bfdf0f3a1 to your computer and use it in GitHub Desktop.
Change order for Upsell & Related products sections on single product in WooCommerce
<?php
/**
* Change order for Upsell & Related products sections on single product
*/
function CUSTOM_change_order_upsell_related() {
if ( is_product() ) {
// Remove Upsell section
remove_action( 'woocommerce_after_single_product_summary','woocommerce_upsell_display', 15 );
// Remove Related products section
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
// Add the new order
add_action( 'woocommerce_after_main_content', 'CUSTOM_upsell_related_display', 25 );
}
}
/**
* Display content for new order
*/
function CUSTOM_upsell_related_display() {
global $product;
$args_related = array(
'posts_per_page' => 3, // Number of related products
);
$args_upsell = array(
'posts_per_page' => 2, // Number of upsell items
);
$related = $product->get_related();
if ( ! empty( $related ) && (count( $related ) > 0) ) { ?>
<div class="woocommerce-related-products">
<div class="container">
<div class="row">
<?php echo woocommerce_related_products( $args_related ); ?>
</div>
</div>
</div>
<?php
}
$upsells = $product->get_upsells();
if ( ! empty( $upsells ) && (count( $upsells ) > 0) ) { ?>
<div class="woocommerce-upsells-products">
<div class="container">
<div class="row">
<?php echo woocommerce_upsell_display( $args_upsell ); ?>
</div>
</div>
</div>
<?php
}
}
?>
<?php
// Hook
add_action( 'template_redirect', 'CUSTOM_change_order_upsell_related' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment