2016-06-18 66 views
1

我正在研究與WooCommerce商店進行交互的插件,問題是插件和商店位於同一WordPress安裝(相同的服務器和域)以及WooCommerce Rest API didn沒有工作。我已經問過這個問題:本地服務器上的WooCommerce Rest API問題

WooCommerce API Issue with Authentication Consumer Key is missing

我的問題:有沒有辦法與WooCommerce直接交互,而不REST API,特別是如果我的插件和WooCommerce店都是在同一臺服務器上?

回答

1

我終於找到了解決辦法,以直接訪問WooCommerce API不使用REST API我第一次發現這個鏈接偉大的代碼:

https://wordpress.org/support/topic/programming-question-memory-leak-when-accessing-products

而且使用這個偉大的鏈接文檔

http://woocommerce.wp-a2z.org/oik_file/includesapiv2class-wc-api-orders-php/

然後通過在WooCommerce中導航插件文件夾中的源代碼在 插件/ woocommerce/includes/api

我成功訪問WooCommerce,這裏是一個簡單的例子來獲得一個類別的產品和使用頁碼:

//you need to sign in with wordpress admin account to access WooCommerce data 
function setupWooCommerce() { 
    $wooCommercePath = realpath(WP_PLUGIN_DIR . '/woocommerce/woocommerce.php'); 
    require_once $wooCommercePath; 

    WC()->api->includes(); 
    WC()->api->register_resources(new WC_API_Server('/')); 

    $credentials = [ 
     'user_login' => 'username', 
     'user_password' => 'password' 
    ]; 
    $user = wp_signon($credentials, false); 
    wp_set_current_user($user->ID); 
} 

function getProducts($category, $pageNumber) { 
    setupWooCommerce(); 
    $products = NULL; 
    try { 
     $api = WC()->api->WC_API_Products; 
     $products = $api->get_products(null, null, array('category' => $category), $pageNumber); 
    } catch (Exception $e) { 
     error_log("Caught $e"); 
    } 
    return $products; 
} 
相關問題