我使用这个函数来获取ajax请求中一个类别的所有产品
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();
}然后我收到了每个产品的数据,但不是每个产品的全部数据。
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"我找不到形象或产品的价格,也找不到销售价格或价格
我想在脚本中得到这些数据
function get_div(obj)
{
console.log("heeeeeeeeeeeeeeeere");
console.log(obj.ID);
console.log(obj.post_name);
console.log(obj.regular_price);=> return undefined
} 问题是什么?
发布于 2020-04-29 14:41:13
例如,WP_Query只用于获取标准post数据。插件(如WC )的附加数据将不会被获取。
如果您想使用自定义查询,可以使用WC_产品_查询
将以下内容放在主题的functions.php中,并将javascript保存到一个单独的php文件中。快速测试使用WC默认示例产品。
// 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);
}function test_ajax() {
let myData = {
action: 'my_action',
};
jQuery.post('', myData, function (response) {
if (response.success == true) {
console.log(response);
}
});
}要进行测试,只需为您的环境准备上面的带有修正路径的文件。在浏览器检查器中运行test_ajax(),您将看到如下内容:

https://wordpress.stackexchange.com/questions/365398
复制相似问题