Skip to content

Select field inside a Complex field does not return the correct value using carbon_get_theme_options() #1298

@gnikolopoulos

Description

@gnikolopoulos

Hi all,

I am facing an issue with a select field that I nested in a complex field, along with a couple text fields. When viewing the theme options, the select field value seems properly populated (options added from a callback) and any changes are correctly displayed after save.
Here's what I am trying to do... I am creating a transient of some of these theme options upon user login. When I use carbon_get_theme_options() to get the complex field, all values are correct, except the select values, which display a random id instead of the correct one.
Here's how I am adding the complex field:

Field::make( 'complex', 'subscriptions', __( 'Subscription Levels', 'wine-subscriptions' ))
  ->set_layout('tabbed-vertical')
  ->set_collapsed(true)
  ->setup_labels([
    'plural_name' => __('Subscription Levels', 'wine-subscriptions'),
    'singular_name' => __('Subscription Level', 'wine-subscriptions'),
  ])
  ->add_fields([
    Field::make( 'text', 'level', __('Subscription Level', 'wine-subscriptions') )
      ->set_required( true )
      ->set_default_value('Subscription'),
    Field::make( 'select', 'subscription_product_id', __('Subscription', 'wine-subscriptions') )
      ->set_options([$this, 'get_subscription_products']),
    Field::make( 'text', 'discount', __('Discount Percent', 'wine-subscriptions') )
      ->set_required( true )
      ->set_attribute( 'type', 'number' )
      ->set_help_text( __('This is calculated on the final price of each product', 'wine-subscriptions') )
      ->set_default_value(10),
    Field::make( 'text', 'bottles', __('Max number of bottles per billing period', 'wine-subscriptions') )
      ->set_required( true )
      ->set_attribute( 'type', 'number' )
      ->set_default_value(1),
    Field::make( 'text', 'max_orders', __('Max number of orders per billing period', 'wine-subscriptions') )
      ->set_required( true )
      ->set_attribute( 'type', 'number' )
      ->set_default_value(3),
  ])

And here is my callback function:

public function get_subscription_products()
{
  return array_column(
    array_map(
      function ($item) { return ['id' => $item->get_id(), 'name' => $item->get_name()]; },
      wc_get_products([
        'product_type' => 'subscription',
        'status' => 'publish',
      ])
    ),
    'name', 'id'
  );
}

The field I have an issue with is the subscription_product_id field, which not only displays a random product id during the transient creation, but also a product id that is not returned from the callback function.

Can anyone point me in the right direction here?

I am using WP 6.8.2 and CF 3.6.5 (latest version that can be used as a plugin)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions