admin管理员组文章数量:1122846
i use this function to get all products of one category in ajax request
function get_products()
{
$cat_id = $_POST['category'];
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => $cat_id,
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
),
array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => 'exclude-from-catalog', // Possibly 'exclude-from-search' too
'operator' => 'NOT IN'
)
)
);
$products = new WP_Query($args);
echo json_encode($products);
wp_die();
}
then i received data of each product but not all data of each product
ID: 75
post_author: "4"
post_date: "2020-04-21 08:45:26"
post_date_gmt: "2020-04-21 08:45:26"
post_content: "samsung galaxy is very attractive mobile"
post_title: "samsung galaxy"
post_excerpt: "this samsung galaxy vvvvvvvvvvvv"
post_status: "publish"
comment_status: "open"
ping_status: "closed"
post_password: ""
post_name: "samsung-galaxy"
to_ping: ""
pinged: ""
post_modified: "2020-04-29 12:06:42"
post_modified_gmt: "2020-04-29 12:06:42"
post_content_filtered: ""
post_parent: 0
guid: "http:
menu_order: 0
post_type: "product"
post_mime_type: ""
comment_count: "1"
filter: "raw"
i can't find image or product price or sale price or rate
i want to get this data in the script
function get_div(obj)
{
console.log("heeeeeeeeeeeeeeeere");
console.log(obj.ID);
console.log(obj.post_name);
console.log(obj.regular_price);=> return undefined
}
what is the proplem???
i use this function to get all products of one category in ajax request
function get_products()
{
$cat_id = $_POST['category'];
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => $cat_id,
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
),
array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => 'exclude-from-catalog', // Possibly 'exclude-from-search' too
'operator' => 'NOT IN'
)
)
);
$products = new WP_Query($args);
echo json_encode($products);
wp_die();
}
then i received data of each product but not all data of each product
ID: 75
post_author: "4"
post_date: "2020-04-21 08:45:26"
post_date_gmt: "2020-04-21 08:45:26"
post_content: "samsung galaxy is very attractive mobile"
post_title: "samsung galaxy"
post_excerpt: "this samsung galaxy vvvvvvvvvvvv"
post_status: "publish"
comment_status: "open"
ping_status: "closed"
post_password: ""
post_name: "samsung-galaxy"
to_ping: ""
pinged: ""
post_modified: "2020-04-29 12:06:42"
post_modified_gmt: "2020-04-29 12:06:42"
post_content_filtered: ""
post_parent: 0
guid: "http:
menu_order: 0
post_type: "product"
post_mime_type: ""
comment_count: "1"
filter: "raw"
i can't find image or product price or sale price or rate
i want to get this data in the script
function get_div(obj)
{
console.log("heeeeeeeeeeeeeeeere");
console.log(obj.ID);
console.log(obj.post_name);
console.log(obj.regular_price);=> return undefined
}
what is the proplem???
Share Improve this question edited Apr 29, 2020 at 15:02 Omnia Magd asked Apr 29, 2020 at 14:28 Omnia MagdOmnia Magd 1151 silver badge6 bronze badges1 Answer
Reset to default 0For instance, WP_Query is only for fetching standard post data. Additional data by plugins such as WC will not be fetched.
To do a custom query
If you would like to use a custom query, you could use WC_Product_Query
Place the following in theme's functions.php and save the javascript into a separate php file. The quick test used WC default sample products.
// in theme's functions.php
// before init, no products is prepared if just direct call in functions.php, so to have quick test, need to place inside a init
add_action( 'init', 'ws365398_simple_test' );
function ws365398_simple_test() {
add_action('wp_enqueue_scripts', 'ws365398_enqueue_scripts', 101);
add_action( 'wp_ajax_my_action', 'get_products' );
add_action( 'wp_ajax_nopriv_my_action', 'get_products' );
}
// actions for ajax
function get_products()
{
if( !empty( $_POST['category'] ) ) {
$cat_id = $_POST['category'];
} else {
$cat_id = 19; // default change to yours, in the test case ensure it have something by default
}
$args = array(
'status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'tax_query' => array(
'relation' => 'AND', // relation of 2 taxonomy queries
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => $cat_id,
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
),
array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => 'exclude-from-catalog', // Possibly 'exclude-from-search' too
'operator' => 'NOT IN'
)
)
);
$query = new WC_Product_Query($args);
$products = $query->get_products();
// convert to array data before pushing to json
$product_arr = [];
foreach ($products as $key => $product) {
$product_arr[] = $product->get_data();
}
// echo json_encode($product_arr);
wp_send_json_success( $product_arr ); // with success flag and use directly
wp_die();
}
// enqueue ajax script
function ws365398_enqueue_scripts()
{
wp_enqueue_script('test-custom-scripts', get_theme_file_uri('ws365398-test-ajax.php'), array(), 't' . time(), true);
}
<?php
// ws365398-test-ajax.php
/** Load WordPress Bootstrap */
// change to your path
require_once( '/project/wp-load.php' );
header("Content-type: text/javascript");?>
function test_ajax() {
let myData = {
action: 'my_action',
};
jQuery.post('<?php echo admin_url( 'admin-ajax.php' );?>', myData, function (response) {
if (response.success == true) {
console.log(response);
}
});
}
To test, just prepare the above files with corrected path for your environment.
Run test_ajax()
in browser inspector and you will see something like the following:
本文标签: plugin developmentget all products of one category
版权声明:本文标题:plugin development - get all products of one category 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736289186a1928253.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论