Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add override loop template code snippet #40750

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/code-snippets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ Various code snippets you can add to your site to enable custom functionality:
- [Adjust the quantity input values](./adjust-quantity-input-values.md)
- [Change a currency symbol](./change-a-currency-symbol.md)
- [Change number of related products output](./number-of-products-per-row.md)
- [Override loop template and show quantities next to add to cart buttons.](./override-loop-template-show-quantities-next-add-cart.md)
- [Rename a country](./rename-a-country.md)
- [Unhook and remove WooCommerce emails](./unhook--remove-woocommerce-emails.md)
5 changes: 2 additions & 3 deletions docs/code-snippets/add-a-country.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# Add a country


Add this code to your child theme’s `functions.php` file or via a plugin that allows custom functions to be added, such as the [Code Snippets](https://wordpress.org/plugins/code-snippets/) plugin. Avoid adding custom code directly to your parent theme’s functions.php file, as this will be wiped entirely when you update the theme.

```php
if ( ! function_exists( 'YOUR_PREFIX_add_country_to_countries_list' ) ) {
/**
* Add a country to countries list
*
*
* @param array $countries Existing country list.
* @return array $countries Modified country list.
*/
Expand All @@ -24,7 +23,7 @@ if ( ! function_exists( 'YOUR_PREFIX_add_country_to_countries_list' ) ) {
if ( ! function_exists( 'YOUR_PREFIX_add_country_to_continents_list' ) ) {
/**
* Add a country to continents list
*
*
* @param array $continents Existing continents list.
* @return array $continents Modified continents list.
*/
Expand Down
4 changes: 2 additions & 2 deletions docs/code-snippets/add-a-currency-symbol.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Add this code to your child theme’s `functions.php` file or via a plugin that
if ( ! function_exists( 'YOUR_PREFIX_add_currency_name' ) ) {
/**
* Add custom currency
*
*
* @param array $currencies Existing currencies.
* @return array $currencies Updated currencies.
*/
Expand All @@ -21,7 +21,7 @@ if ( ! function_exists( 'YOUR_PREFIX_add_currency_name' ) ) {
if ( ! function_exists( 'YOUR_PREFIX_add_currency_symbol' ) ) {
/**
* Add custom currency symbol
*
*
* @param string $currency_symbol Existing currency symbols.
* @param string $currency Currency code.
* @return string $currency_symbol Updated currency symbol(s).
Expand Down
2 changes: 1 addition & 1 deletion docs/code-snippets/add-or-modify-states.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Add your own or modify shipping states in WooCommerce.
if ( ! function_exists( 'YOUR_PREFIX_add_or_modify_states' ) ) {
/**
* Add or modify States
*
*
* @param array $states Existing country states.
* @return array $states Modified country states.
*/
Expand Down
2 changes: 1 addition & 1 deletion docs/code-snippets/adjust-quantity-input-values.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ if ( ! function_exists( 'YOUR_PREFIX_woocommerce_available_variation' ) ) {
function YOUR_PREFIX_woocommerce_available_variation( $args ) {
$args['max_qty'] = 20; // Maximum value (variations)
$args['min_qty'] = 2; // Minimum value (variations)

// Note: the starting value and step for variations is controlled
// from the 'woocommerce_quantity_input_args' filter shown above for
// simple products
Expand Down
8 changes: 4 additions & 4 deletions docs/code-snippets/change-a-currency-symbol.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ Add this code to your child theme’s `functions.php` file or via a plugin that
if ( ! function_exists( 'YOUR_PREFIX_change_currency_symbol' ) ) {
/**
* Change a currency symbol
*
*
* @param string $currency_symbol Existing currency symbols.
* @param string $currency Currency code.
* @return string $currency_symbol Updated currency symbol(s).
*/
*/
function YOUR_PREFIX_change_currency_symbol( $currency_symbol, $currency ) {
switch ( $currency ) {
case 'AUD': $currency_symbol = 'AUD$'; break;
}

return $currency_symbol;
return $currency_symbol;
}
add_filter( 'woocommerce_currency_symbol', 'YOUR_PREFIX_change_currency_symbol', 10, 2 );
add_filter( 'woocommerce_currency_symbol', 'YOUR_PREFIX_change_currency_symbol', 10, 2 );
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Override loop template and show quantities next to add to cart buttons

Add this code to your child theme’s `functions.php` file or via a plugin that allows custom functions to be added, such as the [Code Snippets](https://wordpress.org/plugins/code-snippets/) plugin. Avoid adding custom code directly to your parent theme’s functions.php file, as this will be wiped entirely when you update the theme.

```php
if ( ! function_exists( 'YOUR_PREFIX_quantity_inputs_for_woocommerce_loop_add_to_cart_link' ) ) {
/**
* Override loop template and show quantities next to add to cart buttons
* @param string $html Default loop template.
* @param object $product Product data.
* @return string Modified loop template.
*/
function YOUR_PREFIX_quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
$html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
$html .= woocommerce_quantity_input( array(), $product, false );
$html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
$html .= '</form>';
}

return $html;
}
add_filter( 'woocommerce_loop_add_to_cart_link', 'YOUR_PREFIX_quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
}
```
1 change: 0 additions & 1 deletion docs/code-snippets/rename-a-country.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Rename a country


Add this code to your child theme’s `functions.php` file or via a plugin that allows custom functions to be added, such as the [Code Snippets](https://wordpress.org/plugins/code-snippets/) plugin. Avoid adding custom code directly to your parent theme’s functions.php file, as this will be wiped entirely when you update the theme.

```php
Expand Down