简体   繁体   中英

bigcommerce api php get order coupon

We are moving to BC and I am getting orders through the api while using the usexml function as I have to output a file for each order. I have been successful getting orders, products in the orders, and shipping address from BC. I have had to add a number of function to the bigcommerce.php library to do this, as while they may be referenced in the online docs, they don't actually exist in the library. My last step is getting the coupons associated with an order. The functions which I implemented in the library are as follows:

// Function missing from library addded 030215 PH - Get products on an order

public static function getOrderProducts($id)
{
         return self::getCollection('/orders/' . $id . '/products','Order');      
}

// Function missing form library added 030215 - Get Shipping adds on order

    public static function getOrderShippingAddresses($id)
    {
return self::getCollection('/orders/' . $id .'/shipping_addresses','Order');             
    }

// Function missing form library added 030215 - Get Coupons on an order

    public static function getOrderCoupons($id)
    {
         return self::getCollection('/orders/' . $id . '/coupons','Order');       
    }

While similar in syntax I can't get the getOrderCoupons function to return coupon info to substitute for the link provided in the normal getOrders return.

I am using the same logic for coupon as executed for getting shipping addresses as shown below. Any solutions are greatly appreciated.

NB Bigcommerce::useXML(); is used so XML strings are being used

      $shipping_xml = Bigcommerce::getOrderShippingAddresses($order->id);
      $shippings = simplexml_load_string($shipping_xml);

// Should only have one shipping address per order

      foreach($shippings as $shipping) {
             $xmlship = $shipping->asXML();
             $domship = new DOMDocument();
             $domship = dom_import_simplexml($shipping);
             $domordship = dom_import_simplexml($order->shipping_addresses);
          $domship = $domordship->ownerDocument->importNode($domship, TRUE);
          $domordship->appendChild($domship);
      }  

    $coupon_xml = Bigcommerce::getOrderCoupons($order->id); 
    $coupons = simplexml_load_string($coupon_xml);

// Should only have one coupon per order

    foreach($coupons as $coupon) {                       
          $xmlcoup = $coupon->asXML();
          $domcoup = new DOMDocument();
          $domcoup = dom_import_simplexml($coupon);
          $domordcoup = dom_import_simplexml($order->coupons);
          $domcoup = $domordcoup->ownerDocument->importNode($domcoup, TRUE);
          $domordcoup->appendChild($domcoup);
          }   

i use the bigcommerce api and i can get orders,coupons,coupon by id,shipping addresses, but I only use the class Connection.php with my own methods to get ones.

you have to track and check if the curl url is right, doing var_dum($url);

method in connection.php //Bigcommerce\\Api\\Connection

public function get($url, $query=false)
{
        $this->initializeRequest();

        if (is_array($query)) {
            $url .= '?' . http_build_query($query);
        }

        var_dum($url);//**CHECK IF THIS URL IS BUILT CORRECTLY**//

        curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($this->curl, CURLOPT_URL, $url);
        curl_setopt($this->curl, CURLOPT_HTTPGET, true);
        curl_exec($this->curl);

        return $this->handleResponse();
}

You also can check the response in connection.php

private function handleResponse()
{
        if (curl_errno($this->curl)) {

            throw new NetworkError(curl_error($this->curl), curl_errno($this->curl));
        }
        var_dump(body);//CHECK THIS LINE
        $body = ($this->useXml) ? $this->getBody() : json_decode($this->getBody());

        $status = $this->getStatus();
        var_dump(status);//CHECK THIS LINE
        if ($status >= 400 && $status <= 499) {
            if ($this->failOnError) {
                throw new ClientError($body, $status);
            } else {
                $this->lastError = $body;
                return false;
            }
        } elseif ($status >= 500 && $status <= 599) {
            if ($this->failOnError) {
                throw new ServerError($body, $status);
            } else {
                $this->lastError = $body;
                return false;
            }
    }

        if ($this->followLocation) {
            $this->followRedirectPath();
        }
        var_dump(body);//CHECK THIS LINE
        return $body;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM