Categories
CMS Mastering Development Wordpress

get all products of one category

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???

Leave a Reply

Your email address will not be published. Required fields are marked *