[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]