/**
* Auto Complete all WooCommerce virtual orders.
*
* @param int $order_id The order ID to check
* @return void
*/
function custom_woocommerce_auto_complete_virtual_orders( $order_id ) {
// if there is no order id, exit
if ( ! $order_id ) {
return;
}
// get the order and its exit
$order = wc_get_order( $order_id );
$items = $order->get_items();
// if there are no items, exit
if ( 0 >= count( $items ) ) {
return;
}
// go through each item
foreach ( $items as $item ) {
// if it is a variation
if ( '0' != $item['variation_id'] ) {
// make a product based upon variation
$product = new WC_Product( $item['variation_id'] );
} else {
// else make a product off of the product id
$product = new WC_Product( $item['product_id'] );
}
// if the product isn't virtual, exit
if ( ! $product->is_virtual() ) {
return;
}
}
/*
* If we made it this far, then all of our items are virual
* We set the order to completed.
*/
$order->update_status( 'completed' );
}
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_virtual_orders' );
code snippets
woo product title above image
// woo - product title above image
function product_change_title_position() {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
add_action( 'woocommerce_before_single_product', 'woocommerce_template_single_title', 5 );
}
add_action( 'init', 'product_change_title_position' );
woo place description after short description
// woo - place description after short description
add_action( 'woocommerce_single_product_summary', 'ta_the_content' );
function ta_the_content() {
echo the_content();
}
woo remove tabs on product page
//Remove WooCommerce Tabs - this code removes all 3 tabs - to be more specific just remove actual unset lines
add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tabs', 98 );
function woo_remove_product_tabs( $tabs ) {
unset( $tabs['description'] ); // Remove the description tab
unset( $tabs['reviews'] ); // Remove the reviews tab
unset( $tabs['additional_information'] ); // Remove the additional information tab
return $tabs;
}
empty cart in nav redirect to shop
// woo gp - navigation - empty cart redirect to shop
add_action( 'template_redirect', 'empty_cart_redirect' );
function empty_cart_redirect(){
if( is_cart() && WC()->cart->is_empty() ) {
wp_safe_redirect( get_permalink( wc_get_page_id( 'shop' ) ) );
exit();
}
}
woo cart change continue shopping button text
add_action('woocommerce_cart_actions', function() {
?>
<a class="button wc-backward" href="<?php echo esc_url( apply_filters( 'woocommerce_return_to_shop_redirect', wc_get_page_permalink( 'shop' ) ) ); ?>"> <?php _e( 'Return to shop', 'woocommerce' ) ?> </a>
<?php
});
woo products per page
//woo Change number of products that are displayed per page (shop page)
add_filter( 'loop_shop_per_page', 'new_loop_shop_per_page', 20 );
function new_loop_shop_per_page( $cols ) {
// $cols contains the current number of products per page based on the value stored on Options –> Reading
// Return the number of products you wanna show per page.
$cols = 12;
return $cols;
}
woo clear cart button
//Add clear cart button on checkout page
// check for empty-cart get param to clear the cart
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $woocommerce;
if ( isset( $_GET['empty-cart'] ) ) {
$woocommerce->cart->empty_cart();
}
}
add_action( 'woocommerce_cart_actions', 'patricks_add_clear_cart_button', 20 );
function patricks_add_clear_cart_button() {
echo "<a class='button' href='?empty-cart=true'>" . __( 'Empty Cart', 'woocommerce' ) . "</a>";
}
woo breadcrumb replace home link url
// Replace the home link URL
add_filter( 'woocommerce_breadcrumb_home_url', 'woo_custom_breadrumb_home_url' );
function woo_custom_breadrumb_home_url() {
return 'http://woocommerce.com';
}
woo change order status on payment
add_action( 'sliced_payment_made', 'sliced_woocommerce_set_order_status_2018_08_17', 10, 3 );
function sliced_woocommerce_set_order_status_2018_08_17( $id, $gateway, $status ) {
if( $status === 'success' ) {
$order_id = get_post_meta( $id, '_sliced_invoice_woocommerce_order', true );
if( $order_id ) {
// update woocommerce order status to 'completed'
$order = new WC_Order($order_id);
$order->update_status('completed');
} else {
return; // not tied to woocommerce order
}
}
}