Forum Replies Created

Viewing 5 replies - 1 through 5 (of 5 total)
  • Still no rest api support ??

    Thread Starter ITomSawyerI

    (@itomsawyeri)

    Hi Sir,

    Please help me

    i am getting this error

    https://ibelievetees.com/wp-json/wc/v2/products/

    {“code”:”woocommerce_rest_cannot_view”,”message”:”Sorry, you cannot list resources.”,”data”:{“status”:401}}

    You need to pass secrets to header or query, also you can make check in code to make products available to all customers even non-registered.

    Thread Starter ITomSawyerI

    (@itomsawyeri)

    Ok, now it’s works:

    found this lines of code in:
    woocommerce/packages/woocommerce-rest-api/src/Controllers/Version3/class-wc-rest-posts-controller.php

    
    if ( 'wc/v1' === $this->namespace ) {
      if ( is_array( $request['filter'] ) ) {
        $args = array_merge( $args, $request['filter'] );
        unset( $args['filter'] );
      }
    }
    

    And filter now works with that querry:

    site.com/wp-json/wc/v1/products?filter%5Bmeta_key%5D=izbrannyj_tovar&filter%5Bmeta_value%5D=1

    Thread Starter ITomSawyerI

    (@itomsawyeri)

    I would dump this get_post_types(array(‘show_in_rest’ => true), ‘objects’ – are WooCommerce products post type included?

    I got array of objects and one of the objects is Woocommerce product:

    
    post
    page
    attachment
    wp_block
    product
    slider
    news
    
    "product" string(6)
    
    _builtin: false
    _edit_link: "post.php?post=%d"
    can_export: true
    cap: {edit_post: "edit_product", read_post: "read_product", delete_post: "delete_product", edit_posts: "edit_products", edit_others_posts: "edit_others_products", …}
    capability_type: "product"
    delete_with_user: null
    description: "Здесь вы можете добавлять новые товары в ваш магазин."
    exclude_from_search: false
    has_archive: "shop"
    hierarchical: false
    label: "Товары"
    labels: {name: "Товары", singular_name: "Товар", add_new: "Добавить", add_new_item: "Добавить товар", edit_item: "Изменить товар", …}
    map_meta_cap: true
    menu_icon: null
    menu_position: null
    name: "product"
    public: true
    publicly_queryable: true
    query_var: "product"
    register_meta_box_cb: null
    rest_base: false
    rest_controller: null
    rest_controller_class: false
    rewrite: {slug: "product", with_front: false, feeds: true, pages: true, ep_mask: 1}
    show_in_admin_bar: true
    show_in_menu: true
    show_in_nav_menus: true
    show_in_rest: true
    show_ui: true
    taxonomies: [] (0)
    

    I also noticed that this part of program executes in urls like this:
    site.com/wp-json/wc/v3/products

    ( echo 1 prints to screen )

    
    add_action('rest_api_init', 'wp_rest_filter_add_filters');
    
    function wp_rest_filter_add_filters() {
    	/* Adds Filter for All Post Type*/
    	foreach (get_post_types(array('show_in_rest' => true), 'objects') as $post_type) {
    		add_filter('rest_' . $post_type->name . '_query', 'wp_rest_filter_add_filter_param', 10, 2);
    	}
    	/* Adds Filter for All Taxonomy Type*/
    	foreach (get_taxonomies(array('show_in_rest' => true), 'objects') as $tax_type) {
    		add_filter('rest_' . $tax_type->name . '_query', 'wp_rest_filter_add_filter_param', 10, 2);
    	}
    	/* Adds Filter for User Type*/
    	foreach (get_taxonomies(array('show_in_rest' => true), 'objects') as $tax_type) {
    		add_filter('rest_' . 'user' . '_query', 'wp_rest_filter_add_filter_param', 10, 2);
    	}
    }

    But not this part:
    ( echo 1 doesn’t print to screen )

    function wp_rest_filter_add_filter_param($args, $request) {
    	// Bail out if no filter parameter is set.
    	if (empty($request['filter']) || !is_array($request['filter'])) {
    		return $args;
    	}
    
    	$filter = $request['filter'];
    
    	// posts_per_page
    	if (isset($filter['posts_per_page']) && ((int) $filter['posts_per_page'] >= 1 && (int) $filter['posts_per_page'] <= 100)) {
    		$args['posts_per_page'] = $filter['posts_per_page'];
    	}
    
    	global $wp;
    
    	$vars = apply_filters('rest_query_vars', $wp->public_query_vars);
    
    	function allow_meta_query($valid_vars) {
    		$valid_vars = array_merge($valid_vars, array('meta_query', 'meta_key', 'meta_value', 'meta_compare'));
    		return $valid_vars;
    	}
    	$vars = allow_meta_query($vars);
    
    	foreach ($vars as $var) {
    		if (isset($filter[$var])) {
    			$args[$var] = $filter[$var];
    		}
    	}
    
    	return $args;
    }

    So problem in this line:

    add_filter(‘rest_’ . ‘user’ . ‘_query’, ‘wp_rest_filter_add_filter_param’, 10, 2)

    ‘rest_product_query’ doesn’t work with Woocommerce.

    I also tried to change products endpoint from /wc/v3 to /wp/v2/ but that didn’t work.

    Thread Starter ITomSawyerI

    (@itomsawyeri)

    foreach (get_post_types(array('show_in_rest' => true), 'objects') as $post_type) {  
    add_filter('rest_' . $post_type->name . '_query'

    return posts, pages, all custom routes and product.

    Maybe i need change wp filter “rest_$THIS->POST_TYPE_query” to woocommerce specified?

    I tried
    add_filter('woocommerce_rest_products_query', 'wp_rest_filter_add_filter_param', 10, 2);
    but still no luck

Viewing 5 replies - 1 through 5 (of 5 total)