Tag Archives: WooCommerce

Fix "out of stock" products count on archives in WooCommerce

/**
 * fix_hide_out_of_stock_products.
 */
add_action( 'pre_get_posts', 'fix_hide_out_of_stock_products' );
function fix_hide_out_of_stock_products( $q ) {
	if ( ! $q->is_main_query() || is_admin() || ! $q->is_archive() ) {
		return;
	}
	$meta_query   = ( array ) $q->get( 'meta_query' );
	$meta_query[] = array(
		'key'       => '_stock_status',
		'value'     => 'outofstock',
		'compare'   => 'NOT IN'
	);
	$q->set( 'meta_query', $meta_query );
	remove_action( 'pre_get_posts', 'fix_hide_out_of_stock_products' );
}

Check if product is in cart by product ID in WooCommerce

if ( ! function_exists( 'is_product_in_cart' ) ) {
	/**
	 * is_product_in_cart.
	 *
	 * @return bool
	 */
	function is_product_in_cart( $product_id ) {
		if ( 0 != $product_id ) {
			if ( isset( WC()->cart->cart_contents ) && is_array( WC()->cart->cart_contents ) ) {
				foreach ( WC()->cart->cart_contents as $cart_item_key => $cart_item_data ) {
					if (
						( isset( $cart_item_data['product_id'] )   && $product_id == $cart_item_data['product_id'] ) ||
						( isset( $cart_item_data['variation_id'] ) && $product_id == $cart_item_data['variation_id'] )
					) {
						return true;
					}
				}
			}
		}
		return false;
	}
}

Add order complete action to WooCommerce My Orders customer table

if ( ! function_exists( 'wcj_add_my_account_order_actions' ) ) {
	/*
	 * wcj_add_my_account_order_actions.
	 */
	function wcj_add_my_account_order_actions( $actions, $order ) {
		if ( 'completed' != $order->get_status() ) {
			$actions['wcj_mark_completed_by_customer'] = array(
				'url'  => wp_nonce_url( add_query_arg( array(
					'wcj_action' => 'wcj_woocommerce_mark_order_status',
					'order_id'   => $order->get_id() ) ), 'wcj-woocommerce-mark-order-status' ),
				'name' => __( 'Complete', 'woocommerce-jetpack' ),
			);
		}
		return $actions;
	}
}
add_filter( 'woocommerce_my_account_my_orders_actions', 'wcj_add_my_account_order_actions', 10, 2 );
if ( ! function_exists( 'wcj_add_js_conformation' ) ) {
	/*
	 * wcj_add_js_conformation.
	 */
	function wcj_add_js_conformation() {
		echo '<script>
			jQuery("a.wcj_mark_completed_by_customer").each( function() { jQuery(this).attr("onclick", "return confirm(\'' . __( 'Are you sure?', 'woocommerce-jetpack' ) . '\')") } );
		</script>';
	}
}
add_action( 'wp_footer', 'wcj_add_js_conformation' );
if ( ! function_exists( 'wcj_woocommerce_mark_order_status' ) ) {
	/*
	 * wcj_woocommerce_mark_order_status.
	 */
	function wcj_woocommerce_mark_order_status() {
		if ( isset( $_GET['wcj_action'] ) && 'wcj_woocommerce_mark_order_status' === $_GET['wcj_action'] && isset( $_GET['order_id'] ) && isset( $_GET['_wpnonce'] ) ) {
			if ( wp_verify_nonce( $_GET['_wpnonce'], 'wcj-woocommerce-mark-order-status' ) ) {
				$_order = wc_get_order( $_GET['order_id'] );
				if ( $_order->get_customer_id() === get_current_user_id() ) {
					$_order->update_status( 'completed' );
					wp_safe_redirect( remove_query_arg( array( 'wcj_action', 'order_id', '_wpnonce' ) ) );
					exit;
				}
			}
		}
	}
}
add_action( 'init', 'wcj_woocommerce_mark_order_status' );

Add total customer orders counter column to admin orders list in WooCommerce

/**
 * Add "Orders" column
 */
add_filter( 'manage_edit-shop_order_columns', 'add_shop_order_column_completed_orders', PHP_INT_MAX );
function add_shop_order_column_completed_orders( $columns ) {
	$columns['completed_orders'] = 'Orders';
	return $columns;
}
/**
 * Output "Orders" column
 */
add_action( 'manage_shop_order_posts_custom_column', 'render_shop_order_column_completed_orders', PHP_INT_MAX );
function render_shop_order_column_completed_orders( $column ) {
	if ( 'completed_orders' != $column ) {
		return;
	}
	$_order = wc_get_order( get_the_ID() );
	if ( 'completed' === $_order->get_status() ) {
		if ( 0 != $_order->customer_user ) {
			$customer_orders = get_posts( array(
				'numberposts' => -1,
				'meta_key'    => '_customer_user',
				'meta_value'  => $_order->customer_user,
				'post_type'   => wc_get_order_types(),
				'post_status' => array( 'wc-completed' ), // for all orders use: array_keys( wc_get_order_statuses() )
			) );
			$info = '<a href="/wp-admin/edit.php?post_status=wc-completed&post_type=shop_order&_customer_user='
				. $_order->customer_user . '">' . count( $customer_orders ) . '</a>';
			wp_reset_postdata();
		} else {
			$info = '[guest]';
		}
		echo $info;
	}
}