WooCommerce is chockablock of filters, hooks and functions to help you customize and utilize the plugin in your theme or plugin. hera are some small, but utilitarian, code examples, a-ha ’ sulfur and how-tos of common changes of different text outputs .

Changing “Add to cart” button texts

There are two filters ; one for “ add to cart ” text in loop ( woocommerce_product_add_to_cart_text ), and another for the button in unmarried product ( woocommerce_product_single_add_to_cart_text ) :

// Single product view
add_filter('woocommerce_product_single_add_to_cart_text', function($text) {
	return __('Buy this now', 'txtdomain');
});

// Loop
add_filter('woocommerce_product_add_to_cart_text', function($text) {
	return __('Buy this', 'txtdomain');
});

however keep in take care that this overrides all “ Add to cart ” text. even those which were primitively “ Select options ” ( varying products ) or “ Read more ” ( unpurchasable products – no monetary value or out of stock ). If you want more finetuning, include the irregular argument to these filters ; the product object. Use the product object to conditionally control the end product. For exercise by product type or whether or not merchandise can be purchased :

add_filter('woocommerce_product_add_to_cart_text', function($text, $product) {
	if ($product->is_type('variable')) {
		return __('Take your pick', 'txtdomain');
	}
	if (!$product->is_purchasable()) {
		return __('Read more', 'txtdomain');
	}
	return $text;
}, 10, 2);

Change the price range output on variable and grouped products

As default option WooCommerce will display the lowest and the highest price on variable and grouped products, with a “ – ” inbetween. You can very easily modify this roll output. For example make surely it only shows the lowest monetary value, in the form of “ From : ”. WooCommerce has one filter for variable products and another for grouped products, so let’s look at them both in turn.

Controlling the price roll HTML output signal for variable products is done with the trickle woocommerce_variable_price_html. The product object is provided as irregular argument .
Inside the routine we need to fetch all the prices. WooCommerce offers a handy function on variable product objects, get_variation_prices(). This returns an range of all discrepancy prices, sorted by lowest first. All we need to do if pop off the first base element in the prices array, and output it nicely :

add_filter('woocommerce_variable_price_html', function($html, $product) {
	$prices = $product->get_variation_prices(true);
	$from_price = wc_price(current($prices['price']));
	return sprintf(__('From: %s', 'txtdomain'), $from_price. $product->get_price_suffix());
}, 10, 2);

The price image HTML output percolate for group products is very exchangeable to varying products but with one dispute ; an align of all group intersection “ children ” ‘ randomness prices is included as argumentation to the trickle. The percolate to use is woocommerce_grouped_price_html :

add_filter('woocommerce_grouped_price_html', function($html, $product, $child_prices) {
	$from_price = wc_price(current($child_prices));
	return sprintf(__('From: %s', 'txtdomain'), $from_price. $product->get_price_suffix());
}, 10, 3);

Modify stock status text output

If you have activated display of malcolm stock status in WooCommerce settings, it should display the precise come of stock left in single merchandise view ( e.g. “ 42 in lineage ” ). however you can modify the output to alternatively show a range rather than the precise sum. Or just “ In lineage ” if you want to conceal the number .
The filter for doing this is woocommerce_get_stock_html and if you include the second argument you get the product object. With the intersection object you can do the necessary operations for finetuning the output signal. Keep in mind that the filterable output is wrapped inside a

element, and your end product should excessively.

For simply showing “ In stock ” or “ not in sprout ” this will suffice :

add_filter('woocommerce_get_stock_html', function($html, $product) {
	if ($product->is_in_stock()) {
		return sprintf(' % second', __('In stock', 'txtdomain'));
	} else {
		return sprintf(' % second', __('Out of stock', 'txtdomain'));
	}
	return $html;
}, 10, 2);

Showing stock in “ range values ” can be done like this :

add_filter('woocommerce_get_stock_html', function($html, $product) {
	$current_stock = $product->get_stock_quantity();
	if ($current_stock <= 0) {
		$new_html = __('Out of stock', 'txtdomain');
	} else if ($current_stock > 500) {
		$new_html = sprintf('500+ %s', __('in stock', 'txtdomain'));
	} else if ($current_stock > 100) {
		$new_html = sprintf('100+ %s', __('in stock', 'txtdomain'));
	} else if ($current_stock > 20) {
		$new_html = sprintf('20+ %s', __('in stock', 'txtdomain'));
	} else {
		$new_html = __('In stock', 'txtdomain');
	}
	return sprintf(' % second', $new_html);
}, 10, 2);

Rename order statuses

sometimes a customer wants different labels on holy order statuses to make more feel according to their webshop practices. For example you might want “ On hold ” to be renamed into “ Unsent ” or “ Completed ” to be renamed “ Sent ”. Just to make more common sense which orders need to be sent and which has already been sent. WooCommerce offers a simple filter, wc_order_statuses, on all available club statuses ’ names .

add_filter('wc_order_statuses', function($order_statuses) {
	$order_statuses['wc-on-hold'] = __('Unsent', 'txtdomain');
	$order_statuses['wc-completed'] = __('Sent', 'txtdomain');
	return $order_statuses;
});

available keys are wc-pending, wc-processing, wc-on-hold, wc-completed, wc-cancelled, wc-refunded, and wc-failed .

Leave a Reply

Your email address will not be published.