anonymous
anonymous

Reputation: 369

Connecting WooCommerce REST API to React Native

I am trying to use the woocommerce rest API in my react native e-commerce app. I have been unsuccessful in connecting the API using the following library: https://github.com/JamesUgbanu/react-native-woocommerce-api My code looks like this and it says that "response" is undefined:

import React, { Component } from "react";
import { Button, View, Text } from "react-native";
import WooCommerceAPI from "react-native-woocommerce-api";

const WooCommerce = new WooCommerceAPI({
  url: "website.com/wp-json",
  ssl: true,
  consumerKey: "*********",
  consumerSecret: "**********",
  wpAPI: true,
  version: "wc/v2",
  queryStringAuth: true,
});

WooCommerce.get("products")
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.log(error.response.data);
  });

export default class Products extends Component {
  render() {
    return (
      <View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
        <Text>Products</Text>
      </View>
    );
  }
}

If anyone has some insight feel free to reply or message me personally. I think the data may also be in HTML instead of JSON?

Upvotes: 0

Views: 1239

Answers (1)

anonymous
anonymous

Reputation: 369

Was able to solve this using the following code:

import WooCommerceAPI from "react-native-woocommerce-api";

export function getProducts() {
  return (dispatch) => {
    const WooCommerce = new WooCommerceAPI({
      url: "https://pexhouse.com", // Your store URL
      ssl: true,
      consumerKey: "ck_xxxxxx", // Your consumer secret
      consumerSecret: "cs_xxxxxx", // Your consumer secret
      wpAPI: true, // Enable the WP REST API integration
      version: "wc/v3", // WooCommerce WP REST API version
      queryStringAuth: true,
    });

    return WooCommerce.get("products")
      .then((data) => {
        console.log("Success");
      })
      .catch((error) => {
        console.log(error);
      });
  };
}

Upvotes: 1

Related Questions