How to add offer count as a custom field

Add the following code to the functions.php file of your theme (preferably the child theme) to ensure future updates don’t overwrite your changes.

add_action('content_egg_save_data', 'add_offer_count_in_custom_field', 20, 4);

function add_offer_count_in_custom_field($data, $module_id, $post_id, $is_last_iteration) {
    if (!$is_last_iteration) {
        return;
    }

    $field_name = 'offercount';  // Change this to your custom field name
    $data = \ContentEgg\application\components\ContentManager::getViewProductData($post_id);

    // Filter out items where 'stock_status' is not set or is -1
    $filtered_data = array_filter($data, function ($item) {
        return isset($item['stock_status']) && $item['stock_status'] != -1;
    });

    \update_post_meta($post_id, $field_name, count($filtered_data));
}

Note: The custom field will be updated when posts containing Content Egg offers are created or updated.

Last updated