Skip Navigation

[Resolved] How to automatic get data from other CPT for custom checkbox 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 6 replies, has 2 voices.

Last updated by wilfredC 5 years, 5 months ago.

Assisted by: Nigel.

Author
Posts
#1333065
sku.JPG

Hi,

I have a CPT called "Retailer", in the retailer, how to automatic connect/sync all the product SKU for the checkbox custom field?

Just like you see the in the attachment.

The reason why I want to do this instead of manually type the data Post Field Group is that I want to stay updated with the product status.

meaning when the product(woocommerce) no longer available or being deleted, the following SKU/SKUs will also not display on the "Retailer" checkbox custom field.

#1333171

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Wilfred

This is not possible, I'm afraid.

There are no means to customise checkboxes fields available.

If this were a select field then we have a filter available—wpt_field_options—that can be used to modify the select options on the fly, but there is no such filter for modifying checkboxes fields. (It would be a good idea to request one, though that doesn't help you now: https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/.)

A possible solution would be to use a select field, modify the options dynamically so that they only included SKUs of published products, and then improve the usability of that field in the post edit screens by adding select2 to it (hidden link).

#1333229

Hi,

Can i have more detail about the select feild solution?

Thank

#1333251

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Sure.

Select fields (and radio fields) when preparing for display in edit screens are passed through the wpt_field_options filter, described here: https://toolset.com/documentation/programmer-reference/types-api-filters/#wpt_field_options

So you can create a Product SKU select custom field with a couple of dummy options that you assign to your retailer post type.

You then add a code snippet that uses the wpt_field_options filter to replace those dummy options with the options you require. That would mean running a custom query to get published products and then get the distinct SKUs for those products. (WooCommerce stores the SKU in a hidden custom field with key '_sku'.) You would then iterate over those SKUs to generate the actual options for the select field. (You would need to check the existing format required for the $current_options parameter which is what you will need to reproduce.)

So that would dynamically populate your select field with the product SKUs.

The final step would be to convert the single select field into something with a better UI, e.g. by adding select2 to it.

You should find that select2 is already enqueued on the post edit screens by Types, meaning you just need to add some custom JS yourself to instantiate select2 on the select field (described in the select2 documentation shared above). There is no simple way to add custom JS to backend admin pages, you'll need to create a .js file and use admin_enqueue_scripts to load it (https://developer.wordpress.org/reference/hooks/admin_enqueue_scripts/). See hidden link if you are not familiar with the best way to do this.

#1333323

I try to follow the instruction but now I have no idea how to change it for the woocommerce product sku

add_filter( 'wpt_field_options', function ( $current_options, $title_of_field ) {
    if ( $title_of_field != 'Skus' ) {
       
        return $current_options;
    }
 
    $skus = get_option( 'skus' );
 
    $new_options = array();
    foreach ( $skus as $color_title => $color_value ) {
        $new_options[] = array(
            '#title' => '1',
            '#value' => 1
        );
    }
 
    return $new_options;
}, 10, 2 );
#1333495

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I'm not really sure what you are trying to do with the above code, it's... not doing what I described above.

If you are not comfortable with coding you should really get a developer to implement it for you (you can contact Toolset contractors here: https://toolset.com/contractors/).

I had a little time so I produced some sample code for you, but please note that this falls outside the scope of support and if you have problems with the code we cannot provide support for it.

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;
}

You will probably still want to add select2 to this, as I described above.

#1333503

This is the solution I looking for.

My issue is resolved now. Thank you!