1. Home
  2. Knowledge Base
  3. WooCommerce Product Add-Ons Ultimate
  4. Advanced Topics for Add-Ons Ultimate
  5. Filter product add-ons – adding fields programmatically
  1. Home
  2. Knowledge Base
  3. WooCommerce Product Add-Ons Ultimate
  4. Filter product add-ons – adding fields programmatically

Filter product add-ons – adding fields programmatically

You can filter product add-ons after you’ve added them in the product or global settings. This is particularly useful if you have complex select fields to add. Here’s an example from Henrik Jacobsen showing how to insert a list of countries:

<?php
/**
* All credit to Henrik Jacobsen for this: https://bitbucket.org/snippets/henjak/4eBAqR/product-extras-for-woocommerce-add
*
* Add countries as options to a selectbox.
*
* When creating the selectbox in admin set "countrylist" as the first option and
* this code will insert all the countries.
*
* @uses product-extras-for-woocommerce/inc/functions-single-product.php/pewc_filter_item_start_list
* on line 122
*
* @param [type] $item [description]
* @param [type] $group [description]
* @param [type] $group_id [description]
* @param [type] $post_id [description]
* @return [type] [description]
* @version 1.0.0
*/
if( !function_exists( 'themeslug_filter_item_start_list' ) ){
function themeslug_filter_item_start_list( $item, $group, $group_id, $post_id ) {
$countries = array("Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegowina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory", "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo", "Congo, the Democratic Republic of the", "Cook Islands", "Costa Rica", "Cote d'Ivoire", "Croatia (Hrvatska)", "Cuba", "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands (Malvinas)", "Faroe Islands", "Fiji", "Finland", "France", "France Metropolitan", "French Guiana", "French Polynesia", "French Southern Territories", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Heard and Mc Donald Islands", "Holy See (Vatican City State)", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran (Islamic Republic of)", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea, Democratic People's Republic of", "Korea, Republic of", "Kuwait", "Kyrgyzstan", "Lao, People's Democratic Republic", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libyan Arab Jamahiriya", "Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Macedonia, The Former Yugoslav Republic of", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia, Federated States of", "Moldova, Republic of", "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "Northern Mariana Islands", "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Pitcairn", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russian Federation", "Rwanda", "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Seychelles", "Sierra Leone", "Singapore", "Slovakia (Slovak Republic)", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "Spain", "Sri Lanka", "St. Helena", "St. Pierre and Miquelon", "Sudan", "Suriname", "Svalbard and Jan Mayen Islands", "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic", "Taiwan, Province of China", "Tajikistan", "Tanzania, United Republic of", "Thailand", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela", "Vietnam", "Virgin Islands (British)", "Virgin Islands (U.S.)", "Wallis and Futuna Islands", "Western Sahara", "Yemen", "Zambia", "Zimbabwe");
$options = array();
if( !empty( $item['field_options'] ) && $item['field_options'][0]['value'] == 'countrylist' ) {
foreach( $countries as $country ){
$options[] = array(
'image' => '',
'value' => esc_attr( $country ),
'price' => ''
);
}
$item['field_options'] = $options;
}
return $item;
}
add_filter( 'pewc_filter_item_start_list', 'themeslug_filter_item_start_list', 10, 4 );
}

Using this code, you can create a select field for your product. Assign one option and call it ‘countrylist’.

When the filter runs, it will look for any select fields whose first option is ‘countrylist’ then replace the options with a list of countries. You can extend this to whatever list you need.

Specifying child products via SKUs and ACF

This method could be helpful if you want to import fields via CSV, e.g. via WP All Import. Instead of specifying child products in the ‘Products’ field itself, you can use Advanced Custom Fields to specify the SKUs of your child products.

Step 1

Install and activate Advanced Custom Fields and create a textarea field with the name child_product_skus that appears on Products.

Step 2

In the product where you wish the child products to appear, create a field using Add-Ons Ultimate. The field type should be ‘Products’ and you can choose whatever layout or other parameters you like. But you can leave the ‘Child products’ setting empty.

Step 3

On the same product, your ACF field should appear. Just enter the SKUs of your child products as a comma-separated list, e.g. Woo-beanie-logo, Woo-tshirt-logo, woo-long-sleeve-tee:

Step 4

Finally, add this snippet:

<?php
/**
* Populate products field using SKUs from ACF field
* Create an ACF field with the name child_product_skus
* Enter the SKUs of the child products into this field as a comma-separated list
*/
function prefix_populate_products_through_ACF( $item, $group, $group_id, $post_id ) {
// Check ACF is active
if( ! function_exists( 'get_field' ) ) {
return $item;
}
$skus = get_field( 'child_product_skus', $post_id );
// Bounce if this field or product doesn't apply
if( ! $skus || empty( $item['field_type'] ) || $item['field_type'] != 'products' ) {
return $item;
}
// Split the list of SKUs into an array
$skus = explode( ',', $skus );
$child_products = array();
// Iterate through each SKU and find the child product ID
foreach( $skus as $sku ) {
// Add the child product ID
$child_products[] = wc_get_product_id_by_sku( trim( $sku ) );
}
// Populate the child products for this product
$item['child_products'] = $child_products;
return $item;
}
add_filter( 'pewc_filter_item_start_list', 'prefix_populate_products_through_ACF', 10, 4 );
/**
* Filter add-on groups ensuring that a specific group is included if the product has the child_product_skus ACF field
* Edit the group ID in $global_group_id below.
*/
function prefix_filter_groups_by_ACF( $groups, $product_id ) {
// Update this to the ID of the global group you would like to insert into products automatically
$global_group_id = 72;
// Check ACF is active
if( ! function_exists( 'get_field' ) ) {
return $groups;
}
$skus = get_field( 'child_product_skus', $product_id );
// Bounce if this field is empty
if( ! $skus ) {
return $groups;
}
$global_group = pewc_get_group_fields( $global_group_id );
$groups[$global_group_id]['items'] = $global_group;
return $groups;
}
add_filter( 'pewc_filter_product_extra_groups', 'prefix_filter_groups_by_ACF', 10, 2 );

Here’s how to add the snippet: https://pluginrepublic.com/documentation/how-to-add-a-code-snippet-to-your-site/.

The snippet will now find the products with the specified SKUs and populate the ‘Products’ field accordingly.

Was this article helpful?

Related Articles