Archive by Author: Tom

Enable product revisions in WooCommerce

add_filter( 'woocommerce_register_post_type_product', 'wpcb_enable_product_revisions', PHP_INT_MAX );
if ( ! function_exists( 'wpcb_enable_product_revisions' ) ) {
	/**
	 * wpcb_enable_product_revisions.
	 *
	 * @see https://wpcodebook.com/snippets/enable-product-revisions-in-woocommerce/
	 */
	function wpcb_enable_product_revisions( $args ) {
		$args['supports'][] = 'revisions';
		return $args;
	}
}

Set product SKU by product ID in WooCommerce

add_filter( 'woocommerce_product_get_sku', 'wpcb_set_product_sku', PHP_INT_MAX, 2 );
if ( ! function_exists( 'wpcb_set_product_sku' ) ) {
	/**
	 * wpcb_set_product_sku.
	 * 
	 * @see https://wpcodebook.com/snippets/set-product-sku-by-product-id-in-woocommerce
	 */
	function wpcb_set_product_sku( $sku, $product ) {
		return $product->get_id();
		// more complex example, which includes adding up to six leading zeros and setting custom prefix in SKU, would be:
		// return sprintf( 'MY_PREFIX-%06s', $product->get_id() );
	}
}

Hide price for sold out products in WooCommerce

add_filter( 'woocommerce_get_price_html', 'wpcb_hide_price_on_sold_out', PHP_INT_MAX, 2 );
if ( ! function_exists( 'wpcb_hide_price_on_sold_out' ) ) {
	/**
	 * wpcb_hide_price_on_sold_out.
	 *
	 * @see https://wpcodebook.com/snippets/hide-price-for-sold-out-products-in-woocommerce/
	 */
	function wpcb_hide_price_on_sold_out( $price_html, $product ) {
		return ( ! $product->is_in_stock() ? '' : $price_html );
	}
}