wordpresswoocommerceadvanced-custom-fieldsdivi

Custom Post Type that pulls in WooCommerce product data dynamically in Divi Theme Builder?


I am working with woocommerce, ACF and divi. I am creating a library / review site and I am looking to make a custom post type of Book Review which pulls in wooCommerce product data (Book) and style it with Divi Theme Builder.

Book Review Custom post type currently contains the following custom fields.

I have created a product, custom post type and a theme builder for all post types of Book Review.

When I add the divi woo modules how can i specify the product that I have selected on the custom post type with the post object field? Is this even possible or do i need to set the product via a filter / action / shortcode in order for the items to pull in correctly?


Solution

  • The Divi Woo Modules don't have a built-in way to to allow you to set the product based on a custom field. One way you could do it is to enable dynamic content support on the product picker within Woo modules, and then select your custom field from the dynamic content menu.

    You can use the following PHP code to enable dynamic content support on the product field of Divi Woo Modules:

    // === Enable On Woo Product Pickers ===
    
    class EnableDynamicContentOnWooProductPickers {
    
        private $module_slugs = array(
            'et_pb_wc_description',
            'et_pb_wc_add_to_cart',
            'et_pb_wc_gallery',
            'et_pb_wc_images',
            'et_pb_wc_additional_info',
            'et_pb_wc_breadcrumb',
            'et_pb_wc_cart_notice',
            'et_pb_wc_meta',
            'et_pb_wc_price',
            'et_pb_wc_rating',
            'et_pb_wc_related_products',
            'et_pb_wc_reviews',
            'et_pb_wc_stock',
            'et_pb_wc_tabs',
            'et_pb_wc_title',
            'et_pb_wc_upsells'
        );
    
        public function __construct() {
            foreach ($this->module_slugs as $slug) {
                \add_filter('et_pb_all_fields_unprocessed_' . $slug, array($this, 'enable_on_product_field'));
            }
            \add_filter('wp_footer', array($this, 'dynamic_content_icon_styles'));
        }
    
        public function enable_on_product_field($fields) {
            if (isset($fields['product']) && is_array($fields['product'])) {
                $fields['product']['dynamic_content'] = 'text';
            }
            return $fields;
        }
    
        public function dynamic_content_icon_styles() {
    
            if (is_admin() || !empty($_GET['et_fb'])) {
    ?>
                <style>
                    /* Reposition the dynamic content icon to avoid covering the select controls */
                    .et-fb-settings-option-dynamic__button--select_product {
                        right: 24px !important;
                    }
    
                    /* Enabling dynamic content makes the AI button display, so hide it */
                    .et-db #et-boc .et-l button.et-fb-settings-option-ai__button--select_product {
                        display: none;
                    }
    
                    /* Hide / replace the warning message shown when dynamic content is deactivated */
                    .et-fb-option--select-product .et-fb-option--warning {
                        visibility: hidden;
                        height: 0 !important;
                        overflow: hidden;
                        margin: 0 !important;
                        padding: 0 !important;
                    }
    
                    .et-fb-option--select-product:has(.et-fb-option--warning)::after {
                        content: "Please save these module settings and reopen to restore this setting.";
                        display: block;
                        color: #333;
                        font-size: 13px;
                        font-weight: 600;
                        background-color: #f9f9f9;
                        padding: 10px;
                        margin-top: 10px;
                    }
                </style>
    <?php
            }
        }
    }
    
    new EnableDynamicContentOnWooProductPickers();
    

    Note that it assumes that the custom field contains the product ID (i.e. the WordPress post ID for the product).

    Source: Divi Booster - Enable Dynamic Content on the Product Field in Divi's Woo Modules