Ecommerce Shopify WordPress Discussion

WordPress Shortcode Code in Functions.php is throwing a Foreach Error

I am getting the following error in one shortcode function that I use multiple times in different ways on my site. Only one of the functions causes an error. We are using a combination of Wordpress, WooCommerce and ACF to display various linked ACF fields on each product page. Warning: Invalid argument supplied for foreach() in /wp-content/themes/astra-child/functions.php on line 229 It also doesn't show the Accessories in the correct area on the product page where the shortcode is used. Here is an example product page: https://mcdanielmetals.com/product/14-knocked-down-roof-curb-48/. I have turned off the debug errors so users won't see them for now since it is a live site. Here is the code that is throwing an error (I have added comments for which is line 229 where the foreach code is): /* Product Accessories Shortcode [acf_accessories] */ function acf_accessory() { //if ( is_product() ) { //global $post; $prod_accessories = get_field('accessories' ); if ( ! empty( $prod_accessories ) && ! is_wp_error( $prod_accessories ) ) { $accessory_links = array(); foreach ( $prod_accessories as $prod_accessory ) { // LINE 229 $accessory_links[] = '<a href="' . esc_url(get_term_link( $prod_accessory )) . '">' . esc_html( $prod_accessory->name ) . '</a>'; } return implode( ', ', $accessory_links ); } //} return ''; } add_shortcode( 'acf_accessories', 'acf_accessory' ); Interestingly I use the same code in three separate functions to create other short codes. These all are before the code above that is throwing the error and they are all working (notice they are all filled in in the example product above). /* Product Applications Shortcode [acf_applications] */ function acf_application() { //if ( is_product() ) { //global $post; $prod_applications = get_field('application' ); if ( ! empty( $prod_applications ) && ! is_wp_error( $prod_applications ) ) { $application_links = array(); foreach ( $prod_applications as $prod_application ) { $application_links[] = '<a href="' . esc_url(get_term_link( $prod_application )) . '">' . esc_html( $prod_application->name ) . '</a>'; } return implode( ', ', $application_links ); } //} return ''; } add_shortcode( 'acf_applications', 'acf_application' ); /* Product Manufactuer Model Numbers Shortcode [acf_prod_mod_nums] */ function acf_prod_mod_num() { //if ( is_product() ) { //global $post; $prod_mod_nums = get_field('model_numbers' ); if ( ! empty( $prod_mod_nums ) && ! is_wp_error( $prod_mod_nums ) ) { $mod_num_links = array(); foreach ( $prod_mod_nums as $prod_mod_num ) { $mod_num_links[] = '<a href="' . esc_url(get_term_link( $prod_mod_num )) . '">' . esc_html( $prod_mod_num->name ) . '</a>'; } return implode( ', ', $mod_num_links ); } //} return ''; } add_shortcode( 'acf_prod_mod_nums', 'acf_prod_mod_num' ); /* Product Manufactuers Shortcode [acf_manufacturers] */ function acf_manufacturer() { //if ( is_product() ) { //global $post; $prod_manufacturers = get_field('manufacturers' ); if ( ! empty( $prod_manufacturers ) && ! is_wp_error( $prod_manufacturers ) ) { $manufacturer_links = array(); foreach ( $prod_manufacturers as $prod_manufacturer ) { $manufacturer_links[] = '<a href="' . esc_url(get_term_link( $prod_manufacturer )) . '">' . esc_html( $prod_manufacturer->name ) . '</a>'; } return implode( ', ', $manufacturer_links ); } //} return ''; } add_shortcode( 'acf_manufacturers', 'acf_manufacturer' ); The code is almost identical for each of these. I originally had all of the variables in each function set to the same variable name for each occurance but I changed them hoping it would fix the problem. I also moved the accessories shortcode function above the rest to see if something in the order of things was causing the problem but that didn't change anything either. The only thing that I did to make sure the code works is change this line: $prod_accessories = get_field('accessories' ); to another ACF field that is currently working: $prod_accessories = get_field('manufacturers' ); I have quadruple checked the ACF fields above and they are both setup exactly the same. I am using the correct field id for both, but accessories doesn't show up. The only other factor I can think of is that I recently moved the site to the live server from the dev site. The dev site still works with the same code so maybe something changed with the change in domain and server? If anyone can help me track down why this one function doesn't work while the others do I would appreciate it.
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.