Home › Forums › Quform WordPress › Product Field
- This topic has 2 replies, 2 voices, and was last updated 1 year, 8 months ago by Ally.
- AuthorPosts
- March 21, 2023 at 12:18 am #35249amplasticParticipant
I have added a Product field (multi select enhanced) by adding the following code to functions.php:
/* pull product data for claims form */
add_action(‘quform_pre_display_1’, function (Quform_Form $form) {
$element = $form->getElement(‘quform_1_17’);if ($element instanceof Quform_Element_Multi) {
$options = array();
$posts = get_posts(array(‘numberposts’ => -1, ‘post_type’ => ‘product’));foreach ($posts as $post) {
$options[] = array(‘label’ => $post->post_title, ‘value’ => $post->post_title, ‘id’ => $post->ID);
}$element->setOptions($options);
}However, I need it to only pull products that are public products that have catalog visibility of “Shop and search results.” Also, I need it to display the sku number after the product title. How can I go about doing this? Thanks
March 21, 2023 at 12:32 am #35250amplasticParticipantnvm i got chatGPT to write it for me
/* pull product data for claims form */
add_action(‘quform_pre_display_1’, function (Quform_Form $form) {
$element = $form->getElement(‘quform_1_17’);if ($element instanceof Quform_Element_Multi) {
$options = array();
$args = array(
‘posts_per_page’ => -1,
‘post_type’ => ‘product’,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘product_visibility’,
‘field’ => ‘name’,
‘terms’ => ‘exclude-from-catalog’,
‘operator’ => ‘NOT IN’,
),
),
);$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$product = wc_get_product(get_the_ID());
if ($product) {
$sku = $product->get_sku() ? ‘ (‘ . $product->get_sku() . ‘)’ : ”;
$options[] = array(‘label’ => get_the_title() . $sku, ‘value’ => get_the_title() . $sku, ‘id’ => get_the_ID());
}
}
wp_reset_postdata();
}$element->setOptions($options);
}
});March 21, 2023 at 8:40 am #35252AllySupport StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
- AuthorPosts
- You must be logged in to reply to this topic.