woocommerce / woocommerce

A customizable, open-source ecommerce platform built on WordPress. Build any commerce solution you can imagine.
https://woocommerce.com
9.29k stars 10.73k forks source link

Get product by SKU #15661

Closed nvanoost closed 7 years ago

nvanoost commented 7 years ago

EXPLANATION OF THE ISSUE

I want to get a product/products which a specific SKU via de WC Api, but I get an 404 error. Message Error: Geen route gevonden die overeenkomt met de URL en aanvraagmethode [rest_no_route] Request

Automattic\WooCommerce\HttpClient\Request Object
(
    [url:Automattic\WooCommerce\HttpClient\Request:private] => https://www.excellentwebshop.nl/wp-json/v3/products?filter%5Bsku%5D=8718309829858
    [method:Automattic\WooCommerce\HttpClient\Request:private] => GET
    [parameters:Automattic\WooCommerce\HttpClient\Request:private] => Array
        (
            [filter] => Array
                (
                    [sku] => 8718309829858
                )

        )

    [headers:Automattic\WooCommerce\HttpClient\Request:private] => Array
        (
            [Accept] => application/json
            [User-Agent] => WooCommerce API Client-PHP/1.3.0
        )

    [body:Automattic\WooCommerce\HttpClient\Request:private] => 
)

Response

Automattic\WooCommerce\HttpClient\Response Object
(
    [code:Automattic\WooCommerce\HttpClient\Response:private] => 404
    [headers:Automattic\WooCommerce\HttpClient\Response:private] => Array
        (
            [Date] => Fri, 16 Jun 2017 13:36:56 GMT
            [Server] => Apache/2
            [X-Robots-Tag] => noindex
            [Link] => ; rel="https://api.w.org/"
            [X-Content-Type-Options] => nosniff
            [Access-Control-Expose-Headers] => X-WP-Total, X-WP-TotalPages
            [Access-Control-Allow-Headers] => Authorization, Content-Type
            [Vary] => Accept-Encoding,User-Agent
            [Transfer-Encoding] => chunked
            [Content-Type] => application/json; charset=UTF-8
        )

    [body:Automattic\WooCommerce\HttpClient\Response:private] => {"code":"rest_no_route","message":"Geen route gevonden die overeenkomt met de URL en aanvraagmethode","data":{"status":404}}
)

STEPS TO REPRODUCE THE ISSUE

Create an api call with the sku filter parameter

try{
            $params = [
                'filter' => [
                    'sku' => '8718309829858'
                ]
            ];
            $product = $this->woocommerce->get("products", $params);

            if(isset($product) && count($product > 1)){
                echo 'Found some products with this SKU';
            }else{
                echo '<h1>prod</h1>';
                pre($product);
            }
        }catch (HttpClientException $e){
            echo '<h1>Message</h1>';
        pre($e->getMessage()); // Error message.
        echo '<h1>Request</h1>';
        pre($e->getRequest()); // Last request data.
        echo '<h1>Response</h1>';
        pre($e->getResponse()); // Last response data.
        }

SYSTEM STATUS

``` ` ### WordPress Environment ### Home URL: https://www.excellentwebshop.nl Site URL: https://www.excellentwebshop.nl WC Version: 3.0.8 Log Directory Writable: ✔ WP Version: 4.8 WP Multisite: – WP Memory Limit: 512 MB WP Debug Mode: – WP Cron: ✔ Language: nl_NL ### Server Environment ### Server Info: Apache/2 PHP Version: 7.0.19 PHP Post Max Size: 1 GB PHP Time Limit: -1 PHP Max Input Vars: 1000 cURL Version: 7.29.0 NSS/3.21 Basic ECC SUHOSIN Installed: – Max Upload Size: 1 GB Default Timezone is UTC: ✔ fsockopen/cURL: ✔ SoapClient: ✔ DOMDocument: ✔ GZip: ✔ Multibyte String: ✔ Remote Post: ✔ Remote Get: ✔ ### Database ### WC Database Version: WC Database Prefix: amadea_ woocommerce_sessions: ✔ woocommerce_api_keys: ✔ woocommerce_attribute_taxonomies: ✔ woocommerce_downloadable_product_permissions: ✔ woocommerce_order_items: ✔ woocommerce_order_itemmeta: ✔ woocommerce_tax_rates: ✔ woocommerce_tax_rate_locations: ✔ woocommerce_shipping_zones: ✔ woocommerce_shipping_zone_locations: ✔ woocommerce_shipping_zone_methods: ✔ woocommerce_payment_tokens: ✔ woocommerce_payment_tokenmeta: ✔ MaxMind GeoIP Database: ✔ ### Security ### Secure connection (HTTPS): ✔ Hide errors from visitors: ✔ ### Active Plugins (49) ### Query Monitor: door John Blackbourn – 2.13.4 Redux Framework: door Team Redux – 3.6.5 AJAX AutoSuggest: door Farhad Ahmadi – 1.9.8 Better Notifications for WordPress: door Made with Fuel – 1.6.5 Contact Form 7: door Takayuki Miyoshi – 4.8 Customer Specific Pricing for WooCommerce: door WisdmLabs – 4.0.1 Display Widgets: door Strategy11 – 2.05 Duplicate Post: door Enrico Battocchi – 3.2 Emergency password reset: door andymoyle – 1.0 EWWW Image Optimizer Cloud: door Shane Bishop – 3.4.1 Excellent Webshop plugin: door Niek van Oost – 1.0 Google Webfont Optimizer: door Sigurdur Gudbrandsson – 0.2.4 Heartbeat Control: door Jeff Matson – 1.0.3 WPBakery Visual Composer: door Michael M - WPBakery.com – 4.12 Loco Translate: door Tim Whitlock – 2.0.14 Mega Main Menu: door MegaMain.com – 2.1.2 Members: door Justin Tadlock – 1.1.3 Menu Management Enhancer: door Chris Mavricos SevenSpark – 1.1 Meta Slider: door Matcha Labs – 3.5.1 Multisafepay: door Multisafepay – 2.2.7 Multisafepay Bancontact: door Multisafepay – 2.2.7 Multisafepay Banktransfer: door Multisafepay – 2.2.7 Multisafepay Creditcards: door Multisafepay – 2.2.7 Multisafepay iDEAL: door Multisafepay – 2.2.7 Multisafepay PayPal: door Multisafepay – 2.2.7 Multisafepay Sofort: door Multisafepay – 2.2.7 Multisafepay VISA: door Multisafepay – 2.2.7 Postman SMTP: door Jason Hendriks – 1.7.2 Recent Posts by Category Widget: door Ross Cornell – 1.3 Resend Welcome Email: door Hudson Atwell – 1.1.1 Shortcodes Ultimate: door Vladimir Anokhin – 4.9.9 TinyMCE Advanced: door Andrew Ozz – 4.6.3 VG PostCarousel: door VinaWebSolutions – 1.1 VinaGecko Helper: door VinaGecko – 1.0.0 W3 Total Cache: door Frederick Townes – 0.9.5.4 WooCommerce Conditional Shipping and Payments: door SomewhereWarm – 1.2.6 WooCommerce Email Customizer: door cxThemes – 3.11 WooCommerce Invoice Me Gateway: door RM Web Lab – 1.2 WooCommerce Google Analytics Integration: door WooCommerce – 1.4.2 WooCommerce: door Automattic – 3.0.8 WooCommerce Helper: door WooCommerce – 1.7.2 – Netwerk geactiveerd WordPress Related Posts: door Sovrn zemanta – 3.6.4 Yoast SEO: door Team Yoast – 4.9 WP All Export Pro: door Soflyy – 1.4.6 WP All Import Pro: door Soflyy – 4.4.8 WP Maintenance Mode: door Designmodo – 2.0.9 WP All Import - User Import Add-On: door Soflyy – 1.0.9 WP All Import - WooCommerce Add-On Pro: door Soflyy – 2.3.6 YITH WooCommerce Zoom Magnifier: door YITHEMES – 1.2.29 ### Settings ### API Enabled: ✔ Force SSL: – Currency: EUR (€) Currency Position: left Thousand Separator: . Decimal Separator: , Number of Decimals: 2 Taxonomies: Product Types: external (external) grouped (grouped) simple (simple) variable (variable) Taxonomies: Product Visibility: exclude-from-catalog (exclude-from-catalog) exclude-from-search (exclude-from-search) featured (featured) outofstock (outofstock) rated-1 (rated-1) rated-2 (rated-2) rated-3 (rated-3) rated-4 (rated-4) rated-5 (rated-5) ### WC Pages ### Winkelbasis: #2606 - /shop/ Winkelmand: #2607 - /cart/ Afrekenen: #2608 - /checkout/ Mijn account: #2609 - /my-account/ ### Theme ### Name: VG Amadea Version: 1.6 Author URL: http://www.vinagecko.com/ Child Theme: ❌ – Als je wijzigingen aanbrengt in WooCommerce of in een hoofdthema dat je niet zelf gebouwd hebt raden we het gebruik van een subthema aan. Zie: Hoe maak je een subthema WooCommerce Support: ✔ ### Templates ### Overrides: vg-amadea/woocommerce/archive-product.php vg-amadea/woocommerce/cart/cart-shipping.php vg-amadea/woocommerce/cart/cart-totals.php vg-amadea/woocommerce/cart/cart.php vg-amadea/woocommerce/cart/cross-sells.php vg-amadea/woocommerce/cart/mini-cart.php vg-amadea/woocommerce/cart/shipping-calculator.php vg-amadea/woocommerce/checkout/form-checkout.php vg-amadea/woocommerce/checkout/form-login.php vg-amadea/woocommerce/checkout/form-shipping.php vg-amadea/woocommerce/checkout/payment.php vg-amadea/woocommerce/checkout/review-order.php vg-amadea/woocommerce/checkout/thankyou.php vg-amadea/woocommerce/content-product.php vg-amadea/woocommerce/content-single-product.php vg-amadea/woocommerce/global/breadcrumb.php vg-amadea/woocommerce/global/quantity-input.php vg-amadea/woocommerce/loop/loop-end.php vg-amadea/woocommerce/loop/loop-start.php vg-amadea/woocommerce/loop/orderby.php vg-amadea/woocommerce/myaccount/form-login.php vg-amadea/woocommerce/myaccount/form-lost-password.php vg-amadea/woocommerce/myaccount/my-address.php vg-amadea/woocommerce/single-product/add-to-cart/simple.php vg-amadea/woocommerce/single-product/add-to-cart/variable.php vg-amadea/woocommerce/single-product/meta.php vg-amadea/woocommerce/single-product/price.php vg-amadea/woocommerce/single-product/rating.php vg-amadea/woocommerce/single-product/related.php vg-amadea/woocommerce/single-product/review.php vg-amadea/woocommerce/single-product/sale-flash.php vg-amadea/woocommerce/single-product/share.php vg-amadea/woocommerce/single-product/short-description.php vg-amadea/woocommerce/single-product/tabs/additional-information.php vg-amadea/woocommerce/single-product/tabs/description.php vg-amadea/woocommerce/single-product/tabs/tabs.php vg-amadea/woocommerce/single-product/title.php vg-amadea/woocommerce/single-product/up-sells.php vg-amadea/woocommerce/single-product-reviews.php vg-amadea/woocommerce/single-product.php ` ```
mikejolley commented 7 years ago

Filter is not supported by the WP Rest API any more. Read available props here https://woocommerce.github.io/woocommerce-rest-api-docs/#list-all-products

Hercilio1 commented 5 years ago

This worked for me:

$params = [
    'sku' => '55551255'
];
echo '<pre>';
print_r($woocommerce->get('products', $params));
tgiordani commented 5 years ago

Thanks @Hercilio1

beppe9000 commented 5 years ago
function getProductBySku($sku)
{
    global $woocommerce;

    $params = [
        'sku' => $sku
    ];

    $ret = $woocommerce->get('products', $params);

    if (is_array($ret) && (count($ret) == 1) && ($ret[0]->sku ?? '') == $sku) {
        return $ret[0];
    } else {
        return null;
    }
}

// example
$gift = getProductBySku('uranium-968') ?? createProduct('uranium-968') ?? false;