Woocommerce / Yith / ACF - code shows ACF field but only default text

28 Views Asked by At

SOLUTION at END!


We sell meal box subscriptions, "Culture Boxes". The actual Culture Boxes are individual products, one sent each month to everyone. Using Woocommerce Subscriptions, Yith Product Add-Ons, ACF and Oxygen Builder. We're not PHP coders but familiar with CSS, HTML.

This is our onboarding page for new subscriptions. https://justbecooking.com/subscriptions. This uses Woo to provide the Subscription product with Yith Add-Ons to let the customer choose various additional options.

Stage 2, "Choose your first Culture Box", the customer can choose from a list of available boxes for their first box (after that we choose). We wanted to add in the short description and "Features List" (via ACF at product) with different features for each box.

BASIC DISPLAY enter image description here

DESIRED DISPLAY enter image description here

Note that each Box has different Features.

Yith providing a section of code and we used PHP snippets plugin to run it (replacing 'your acf field' with the field key, field_65e15dd48d8f8.)

if ( ! function_exists( 'yith_wapo_custom_after_product_price' ) ) {
    add_action( 'yith_wapo_after_addon_product_price', 'yith_wapo_custom_after_product_price', 99, 3 );
    function yith_wapo_custom_after_product_price( $product, $product_id, $addon ) {
        if ( ! empty( $product ) && $product instanceof WC_Product ) {
            echo '<p class="wapo-addon-description">' . stripslashes( $product->get_short_description()  ) . '</p>'; // Product description
        }

        if ( ! empty( $product_id ) ) {
            echo '<p>' . get_field( 'your_acf_field', $product_id ) . '</p>'; // Your ACF Field
        }
    }
}

But it does not work correctly or consistently. The field is called but only displays the default text. The short description is called but does not appear. enter image description here enter image description here

We're unable to figure out what the issue is.

Is it connected to caching? :shrug: Using WP Rocket, Cloudlfare.

Yith's code works, technically, so they are not providing further development. We need to go live very soon and we absolutely need the short description and various features per box to appear.

Any ideas, suggestions?

We used the provided code, included ACF field key. Default text only appears, no short description.


SOLUTION, WORKED OUT BY YITH SUPPORT: Uses the product ID of the parent product instead in the case of the variations added as add-ons.

if ( ! function_exists( 'yith_wapo_custom_after_product_price' ) ) {
    add_action( 'yith_wapo_after_addon_product_price', 'yith_wapo_custom_after_product_price', 99, 3 );
    function yith_wapo_custom_after_product_price( $product, $product_id, $addon ) {
        if ( ! empty( $product ) && $product instanceof WC_Product ) {
            $description = $product instanceof WC_Product_Variation ? $product->get_description() : $product->get_short_description();
            if ( empty( $description ) && $product instanceof WC_Product_Variation ) {
                $parent_product = wc_get_product( $product->get_parent_id() );
                $description    = $parent_product->get_short_description();
            }
            echo '<p class="wapo-addon-description">' . stripslashes( $description  ) . '</p>'; // Product description
        }

        if ( ! empty( $product ) && ! empty( $product_id ) ) {
            $acf_product_id = $product instanceof WC_Product_Variation ? $product->get_parent_id() : $product_id;
            echo '<p>' . get_field( 'field_65e15dd48d8f8', $acf_product_id ) . '</p>'; // Your ACF Field
        }
    }
}
0

There are 0 best solutions below