Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 186045
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 10, 20222022-06-10T15:15:11+00:00 2022-06-10T15:15:11+00:00

php – Woocommerce single product select field not able to perform ajax function

  • 0

[ad_1]

HI i am adding select product field for a single product i tried a code for multiple select but now i need it for single product select field.

here is the code for adding field which works fine.

woocommerce_wp_text_input( array( 
'id'                    =>  "seed-bundle-seeds-0-seed", 
'name'                  =>  $this->options_name."[seed-bundle][seeds][0][seed]",
'label'                 =>  __( 'Seed', $this->slug ),
'placeholder'           =>  __( 'Search for a sseed…', $this->slug ),
'class'                 =>  "wc-product-search repeatable",
'wrapper_class'         =>  "no-label",
'style'                 =>  "width: 100%;",
'type'                  =>  "hidden",
'custom_attributes'     =>  array(
                                'data-action' => "woocommerce_json_search_seeds",     
                                'data-multiple' => "false",
                                'data-val' => 0
                            )
) );

And This is the ajax function, this was working fine on an older version of woocommerce on php 5.6 which is why i am updating it.

Somehow its not working .. i saw on multiple select field it runs an ajax call of Type GET but in this case its not working.
Not adding the Multiple select field code because its different from that one.

Here is the AJAX Code

add_action( 'wp_ajax_woocommerce_json_search_seeds',        array( $this, 'json_search_cs' ) );

/** =============================
    *
    * AJAX - Search for seed based products from a bundle
    *
    ============================= */
    
    /**
     * Search for products and echo json
     *
     * @param string $x (default: '')
     * @param string $post_types (default: array('product'))
     */
    public function json_search_cs( $x = '', $post_types = array( 'product' ) ) {
        ob_start();
        check_ajax_referer( 'search-products', 'security' );
        $term = (string) wc_clean( stripslashes( $_GET['term'] ) );
        if ( empty( $term ) ) {
            die();
        }
        
        $taxQuery = array(
            array(
                'taxonomy' => 'product_type',
                'field'    => 'slug',
                'terms'    => 'seed',
            )
        );
        
        $args = array(
            'post_type'      => $post_types,
            'post_status'    => 'publish',
            'posts_per_page' => -1,
            's'              => $term,
            'fields'         => 'ids',
            'tax_query'      => $taxQuery
        );
        if ( is_numeric( $term ) ) {
            $args2 = array(
                'post_type'      => $post_types,
                'post_status'    => 'publish',
                'posts_per_page' => -1,
                'post__in'       => array( 0, $term ),
                'fields'         => 'ids',
                'tax_query'      => $taxQuery
            );
            $args3 = array(
                'post_type'      => $post_types,
                'post_status'    => 'publish',
                'posts_per_page' => -1,
                'post_parent'    => $term,
                'fields'         => 'ids',
                'tax_query'      => $taxQuery
            );
            $args4 = array(
                'post_type'      => $post_types,
                'post_status'    => 'publish',
                'posts_per_page' => -1,
                'meta_query'     => array(
                    array(
                        'key'     => '_sku',
                        'value'   => $term,
                        'compare' => 'LIKE'
                    )
                ),
                'fields'         => 'ids',
                'tax_query'      => $taxQuery
            );
            $posts = array_unique( array_merge( get_posts( $args ), get_posts( $args2 ), get_posts( $args3 ), get_posts( $args4 ) ) );
        } else {
            $args2 = array(
                'post_type'      => $post_types,
                'post_status'    => 'publish',
                'posts_per_page' => -1,
                'meta_query'     => array(
                    array(
                    'key'     => '_sku',
                    'value'   => $term,
                    'compare' => 'LIKE'
                    )
                ),
                'fields'         => 'ids',
                'tax_query'      => $taxQuery
            );
            $posts = array_unique( array_merge( get_posts( $args ), get_posts( $args2 ) ) );
        }
        $found_products = array();
        if ( $posts ) {
            foreach ( $posts as $post ) {
                $product = wc_get_product( $post );
                $found_products[ $post ] = rawurldecode( $product->get_formatted_name() );
            }
        }
        $found_products = apply_filters( 'woocommerce_json_search_found_products', $found_products );
        wp_send_json( $found_products );
    }

Do we need to run the call through jQuery function Or it should work with Custom Attribute Name Data-action which i used.
Thanks In advance.

[ad_2]

  • 0 0 Answers
  • 9 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 318 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 291 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 285 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.