Skip Navigation

[Resolved] Get Product name for dynamic select field

This support ticket is created 5 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 1 reply, has 2 voices.

Last updated by Christian Cox 5 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#1336653

Hi

This question is related to the support ticket I submit a few days ago.

https://toolset.com/forums/topic/how-to-automatic-get-data-from-other-cpt-for-custom-checkbox-field/

add_filter( 'wpt_field_options', 'tssupp_mod_options', 10, 2 );
function tssupp_mod_options( $current_options, $title ){
 
    if ( $title == "Skus" ) { // Edit for the title of your select custom field
 
        // Get published products
        $products = get_posts(
            array(
                'post_type'     => 'product',
                'post_status'   => 'publish',
                'meta_key'      => '_sku',
                'nopaging'      =>  true
            )
        );
        // Get SKU of each product
        $skus = array();
        foreach ($products as $product) {
            $skus[] = get_post_meta( $product->ID, '_sku', true );
        }
 
        // Customise current options
        $current_options = array();
 
        foreach ($skus as $sku) {
            $current_options[] = array(
                '#value'    => $sku,
                '#title'    => $sku
            );
        }
    }
 
    return $current_options;
}

The code above successful get the product SKU, but now I also need to get the product name.

How can I do it?

#1337051

Hello, are you saying you want to use the Product title as the text instead of the SKU? Or are you saying there is another separate select field for choosing a Product? In either case, you can access the product title from the $product variable after calling get_posts, like this:

        $products = get_posts(
            array(
                'post_type'     => 'product',
                'post_status'   => 'publish',
                'nopaging'      =>  true
            )
        );
         // Get title of each product
        $product_titles = array();
        foreach ($products as $product) {
            $product_titles[] = $product->post_title;
        }

Now you have an array of product titles in $product_titles.