如何获取woocommerce数据(wc_get_product())?

时间:2019-02-12 00:35:03

标签: php wordpress woocommerce woocommerce-rest-api

我制作了一个带有自定义API终结点的自定义插件,以检索WooCommerce预订数据。

当前,当我使用wc_get_products时,状态为200,但数组为空。

我如何利用WooCommerce?

我已将其添加到我的文件中

add_action( 'woocommerce_init', array( 'woocommerce_loaded' ) );

但是它什么也没做。

运行此命令时:

function get_data() {

               $args = array( 'limit' => -1, 'return' => 'ids', 'type' => 'booking'  );

               $products = wc_get_products( $args );

               return $products;
           }

我取回了我拥有的11种产品的11个ID。但是没有其他细节吗? 当我删除I​​D并仅执行'limit'和'type'时,我会得到空的可用性规则,而没有其他数据。

如何获得与产品的WooCommerce API端点相似的返回产品数组?

2 个答案:

答案 0 :(得分:0)

function get_data() {

    $args = array('limit' => -1, 'return' => 'objects', 'type' => 'booking');

    $products = wc_get_products($args);

    return $products;
}

尝试这样

答案 1 :(得分:0)

rightmove_hmo_search = "https://www.rightmove.co.uk/property-for-sale/find.html?locationIdentifier=POSTCODE%5E1619792&maxBedrooms=4&minBedrooms=2&maxPrice=500000&radius=10.0&sortType=18&propertyTypes=&maxDaysSinceAdded=14&includeSSTC=false&mustHave=&dontShow=newHome%2CsharedOwnership%2Cretirement&furnishTypes=&keywords=stpp%2Cloft" #identify and extract listing links from each page (in this case first page only) def get_house_links(url, driver, pages=1): house_links = [] driver.get(url) for i in range(pages): soup = BeautifulSoup(driver.page_source, 'html.parser') listings = soup.find_all("a", class_="propertyCard-moreInfoItem is-carousel") page_data = ['https://rightmove.co.uk' + row['href'] for row in listings] house_links.append(page_data) print(house_links) # next_button = soup.select('button[class="pagination-button pagination-direction pagination-direction--next"]') # if next_button: # for page in range(0, 1): # index = page * 24 # next_button_link = 'https://www.rightmove.co.uk/property-for-sale/find.html?locationIdentifier=POSTCODE%5E1619792&maxBedrooms=4&minBedrooms=2&maxPrice=500000&radius=10.0&sortType=18&' + '&index=' + str(index) + '&propertyTypes=&maxDaysSinceAdded=14&includeSSTC=false&mustHave=&dontShow=newHome%2CsharedOwnership%2Cretirement&furnishTypes=&keywords=stpp%2Cloft' # driver.get(next_button_link) # if page > 1: # break return house_links #get html data from url and return as object def get_html_data(url, driver): driver.get(url) try: WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.XPATH, "//iframe[contains(@class, 'key')]"))) except TimeoutException: print("page took too long to load") BeautifulSoup(driver.page_source, 'html.parser') driver.switch_to.frame(driver.find_element_by_tag_name("iframe")) time.sleep(3) soup = BeautifulSoup(driver.page_source, 'html.parser') return soup 返回对象,因此您应该获取其数据。

尝试一下

wc_get_products