Product Field

Home Forums Quform WordPress Product Field

This topic is: resolved
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #35249
    amplastic
    Participant

    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

    #35250
    amplastic
    Participant

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

    #35252
    Ally
    Support Staff

    You don't have permission to view this content. Please log in or register and then verify your purchases to gain access.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.
Be inspired. © 2024 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy