Beispiel #1
0
 public function createOrder($cwProductId, $item_qty)
 {
     $preOrdersPerItem = 0;
     $cwProduct = \CodesWholesale\Resource\Product::get($cwProductId);
     $codes = \CodesWholesale\Resource\Order::createBatchOrder($cwProduct, array('quantity' => $item_qty));
     foreach ($codes as $code) {
         if ($code->isPreOrder()) {
             $preOrdersPerItem++;
         }
         $links[] = $code->getHref();
     }
     $createdOrderArray = array('counted_pre_orders' => $preOrdersPerItem, 'links' => $links, 'codes' => $codes);
     return $createdOrderArray;
 }
Beispiel #2
0
 public function purchase($cwProductId, $qty)
 {
     $cwProduct = \CodesWholesale\Resource\Product::get($cwProductId);
     $codes = \CodesWholesale\Resource\Order::createBatchOrder($cwProduct, array('quantity' => $qty));
     $cwOrderId = $codes->getOrderId();
     $preOrders = 0;
     $links = array();
     foreach ($codes as $code) {
         if ($code->isPreOrder()) {
             $preOrders++;
         }
         $links[] = $code->getHref();
     }
     $orderedCodesArray = array('cwOrderId' => $cwOrderId, 'links' => $links, 'preOrders' => $preOrders);
     return $orderedCodesArray;
 }
 public function process()
 {
     if ($this->cwProductId == null) {
         $request = file_get_contents('php://input');
         if (empty($request)) {
             die("No request data");
         }
         $cwProductId = $this->connection->receiveUpdatedProductId();
     }
     try {
         $product = \CodesWholesale\Resource\Product::get($cwProductId);
     } catch (\CodesWholesale\Resource\ResourceError $e) {
         die("Received product id: " . $cwProductId . " Error: " . $e->getMessage());
     }
     $quantity = $product->getStockQuantity();
     $price = $product->getLowestPrice();
     $priceSpread = $this->spreadCalculator->calculateSpread($this->spreadParams->getSpreadParams(), $price);
     $this->productUpdater->updateProduct($cwProductId, $quantity, $priceSpread);
 }
     */
    $products = $client->getProducts();
    /**
     * Chose an random product
     */
    $randomIndex = rand(0, count($products) - 1);
    $randomProduct = $products->get($randomIndex);
    /**
     * Find a product by Href this is an id of product.
     *
     * Or directly by href url
     *
     * $url = "https://api.codeswholesale.com/v1/products/8cc3f405-8453-4031-be49-f826814faa0c";
     * \CodesWholesale\Resource\Product::get($url);
     *
     */
    $product = \CodesWholesale\Resource\Product::get($randomProduct->getHref());
    /**
     * Included from utils.php, displaying product details, just for testing purposes
     */
    displayProductDetails($product);
} catch (\CodesWholesale\Resource\ResourceError $e) {
    if ($e->isInvalidToken()) {
        echo "if you are using SessionStorage refresh your session and try one more time.";
    }
    echo $e->getCode();
    echo $e->getErrorCode();
    echo $e->getMoreInfo();
    echo $e->getDeveloperMessage();
    echo $e->getMessage();
}
  *
  * Or directly by href url
  *
  * $url = "https://api.sandbox.codeswholesale.com/v1/products/8cc3f405-8453-4031-be49-f826814faa0c";
  * \CodesWholesale\Resource\Product::get($url);
  *
  */
 /**
  * Sample products
  */
 // $url = "https://sandbox.codeswholesale.com/v1/products/33e3e81d-2b78-475a-8886-9848116f5133"; // - pre order product
 // $url = "https://sandbox.codeswholesale.com/v1/products/04aeaf1e-f7b5-4ba9-ba19-91003a04db0a"; // - not enough balance
 // $url = "https://sandbox.codeswholesale.com/v1/products/6313677f-5219-47e4-a067-7401f55c5a3a"; // - image code
 $url = "https://sandbox.codeswholesale.com/v1/products/ffe2274d-5469-4b0f-b57b-f8d21b09c24c";
 // - code text
 $product = \CodesWholesale\Resource\Product::get($url);
 /**
  * Make an order for this particular product for 15s codes
  */
 $codes = \CodesWholesale\Resource\Order::createBatchOrder($product, array('quantity' => 10));
 /**
  * Go through bought codes
  */
 foreach ($codes as $code) {
     /**
      * There are 3 possible code types returned from CW.
      *
      * Pre Order (when codes are not in stock)
      */
     if ($code->isPreOrder()) {
         // nothing much to do with PreOrdered code - we are working on Post Back functionality,
 /**
  *
  * @param Product $product
  * @param array $options
  * @return CodeList
  */
 public static function createBatchOrder(Product $product, array $options = array())
 {
     return Client::getInstance()->create($product->getBuyHref(), new CodeList(), $options);
 }
/**
 * Helper method to display product details.
 *
 * @param \CodesWholesale\Resource\Product $product
 */
function displayProductDetails(\CodesWholesale\Resource\Product $product)
{
    echo "<b>" . $product->getProductId() . "</b> <br />";
    echo $product->getName() . "(" . $product->getIdentifier() . ")" . "<br />";
    if ($product->getReleaseDate()) {
        echo "Release date: " . $product->getReleaseDate() . "<br />";
    }
    echo "Stock's quantity: " . $product->getStockQuantity() . "<br / >";
    foreach ($product->getLanguages() as $lang) {
        echo $lang . ", ";
    }
    echo " | ";
    foreach ($product->getRegions() as $region) {
        echo $region . ", ";
    }
    echo "<br />";
    foreach ($product->getPrices() as $price) {
        echo $price->value . " from " . $price->from . " to " . ($price->to ? $price->to : "*") . " | ";
    }
    echo "Default price: " . $product->getDefaultPrice();
    echo "<br />";
    foreach ($product->getLinks() as $link) {
        echo "link: " . $link->rel . "<br />";
        echo "href: " . $link->href . "<br />";
    }
    echo "<br />";
}