how to get product list with redux framework in wordpress option in select dropdown

95 Views Asked by At

I am using the redux framework and I want to use the select section as drpwdown and display the list of products, but I don't know how to call the products? Show products in the dropdown list

$fields = array(
        'id' => 'creations_pro_id',
        'type' => 'select',
        'data'     => 'post_type',
        'args'      => array(
            'post_type' => 'product',
        )
    ),
2

There are 2 best solutions below

0
On BEST ANSWER

This is another way:

$fields = array(
    'id'    => 'opt-select-post',
    'type'  => 'select', 
    'data'  => 'posts',
    'args'  => array(
        'post_type'      => 'product',
        'posts_per_page' => -1,
        'orderby'        => 'title',
        'order'          => 'ASC',
    )
);

This method also works correctly

0
On

create function() with any name:

function my_callback_function()
{
    $items = array();

    $args = array(
        'post_type' => 'product',
        'status' => 'publish',
        'orderby' => 'title',
        'order' => 'ASC',
        'limit' => -1,
    );
    $products = wc_get_products($args);
    if (count($products) > 0) {
        foreach ($products as $product) {
            $items[] = $product->get_name() . ' ['.$product->get_id().']' ;
        }
        return $items;
    }
}

then use function in below:

$fields = array(
    'id'    => 'opt-button-set-term',
    'type' => 'select',
    'data' => 'callback',
    'args' => 'my_callback_function',
);

This code works correctly!