Advertisement
clementp

spreadshirt get price product

Mar 29th, 2018
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.96 KB | None | 0 0
  1.  
  2. /**
  3. * récupére le prix d'un produit
  4. *
  5. * @param string url du produit
  6. *
  7. * @return array les informations du prix
  8. */
  9. function getProductPriceApi($url) {
  10.  
  11.   /////////////////////////////
  12.   // info sur le product
  13.   /////////////////////////
  14.   $url            = htmlspecialchars($url);                                     // url du produit : exemple : https://api.spreadshirt.net/api/v1/shops/ID-SHOP/products/ID-PRODUCT
  15.   $product        = getProductApi($url);                                        // récupére les informations du PRODUCT dans l'API
  16.   $configurations = $product->configurations;                                   // récupére la configuration du PRODUCT
  17.   $idProductType  = $product->productType->attributes()->id;                    // récupére l'id du PRODUCT-TYPE
  18.  
  19.   /////////////////////////////
  20.   // info sur le productType dont le prix
  21.   /////////////////////////
  22.   $productType      = getProductType($idProductType);                           // récupére les informations du PRODUCT-TYPE dans l'API
  23.   $productTypePrice = $productType->price->vatIncluded;                         // récupére le prix du PRODUCT-TYPE
  24.   $productTypePrice = (float) $productTypePrice;                                // déclare le prix du PRODUCT-TYPE dans la variable
  25.  
  26.   // CALCUL TOTAL
  27.   $prixTotal = $productTypePrice;                                               // ajoute le prix du PRODUCT-TYPE au prix total du produit
  28.  
  29.   /////////////////////////////
  30.   // récupére toutes les informations de configuration du PRODUCT
  31.   /////////////////////////
  32.   foreach ($configurations->configuration as $value) {
  33.  
  34.     // PRINT TYPE INFORMATIONS
  35.     $id = $value->printType->attributes()->id;                                  // récupére l'id du type de d'impression
  36.     $infoPrint = getPrintType($id);                                             // récupére les informations de type d'impression
  37.  
  38.     // PRINT TYPE COLOR PRICE INFORMATIONS
  39.     if(!empty($infoPrint->colors)){
  40.       $nbrColor = $value->content->svg->image->attributes()->printColorIds;     // récupére l'attributes *printColorIds* exemple : "18 18 187"
  41.       $nbrColor = array_count_values(explode(' ',$nbrColor));                   // crée un tableau avec les valeurs regroupé exemple : [18 => 2, 187 => 1]
  42.       foreach ($nbrColor as $key => $color) {
  43.         foreach ($infoPrint->colors->color as $c) {                             // foreach sur les id de toutes les couleurs > https://api.spreadshirt.net/api/v1/shops/205909/printTypes/14?apiKey=000
  44.           $idPrintTypeColor = $c->attributes()->id;                             // je récupére l'id de la couleur
  45.           if($key == $idPrintTypeColor){                                        // si un id de couleur est identique a mes couleurs
  46.             // CALCUL TOTAL
  47.             $prixTotal = $prixTotal + ( (float) $c->price->vatIncluded);        // j'ajoute le prix de cette couleur a mon prix total
  48.           }
  49.         }
  50.       }
  51.     }
  52.  
  53.     // PRINT TYPE PRICE
  54.     $printTypePrice = (float) $infoPrint->price->vatIncluded;
  55.  
  56.     // DESIGN PRICE
  57.     $designPrice = (float) getDesignInfoApi($value->designs->design->attributes()->id)->price->vatIncluded;
  58.  
  59.     // CALCUL TOTAL
  60.     $prixTotal = $prixTotal + $printTypePrice + $designPrice;
  61.  
  62.     }
  63.  
  64.     return $prixTotal;
  65. }
  66.  
  67.  
  68.  
  69. /**
  70. * Récupere les infos d'un produit depuis sont url API
  71. *
  72. * @param string url du produit dans l'api
  73. *
  74. * @return array xml objet
  75. */
  76. function getProductApi($url) {
  77.   global $config;
  78.  
  79.   $user_agent = 'my-siteProduct/1.0 (http://www.my-site.fr; mail@test.fr)';
  80.   @ini_set ('user_agent', $user_agent);
  81.  
  82.   $url = $url.'?locale=fr_FR';
  83.  
  84.   $header = array();
  85.   $header[] = createSprdAuthHeader("GET", $url);
  86.   $header[] = "Content-Type: application/xml";
  87.  
  88.   $ch = curl_init ($url);
  89.   curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
  90.   curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  91.   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  92.   curl_setopt($ch, CURLOPT_HEADER, false);
  93.   curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  94.   $data = curl_exec($ch);
  95.   curl_close ($ch);
  96.   $data = new SimpleXMLElement($data);
  97.  
  98.   return $data;
  99. }
  100.  
  101.  
  102.  
  103.  
  104. /**
  105. * Récupere les infos d'un type de produit depuis sont id
  106. *
  107. * @param int le numero id du type de produit
  108. *
  109. * @return array xml objet
  110. */
  111. function getProductType($id) {
  112.   global $config;
  113.  
  114.   $user_agent = 'my-siteProductType/1.0 (http://www.my-site.fr; mail@test.fr)';
  115.   @ini_set ('user_agent', $user_agent);
  116.  
  117.   $url = 'https://api.spreadshirt.net/api/v1/shops/'.$config['idShop'].'/productTypes/'.$id.'?locale=fr_FR';
  118.  
  119.   $header = array();
  120.   $header[] = createSprdAuthHeader("GET", $url);
  121.   $header[] = "Content-Type: application/xml";
  122.  
  123.   $ch = curl_init ($url);
  124.   curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
  125.   curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  126.   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  127.   curl_setopt($ch, CURLOPT_HEADER, false);
  128.   curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  129.   $data = curl_exec($ch);
  130.   curl_close ($ch);
  131.   $data = new SimpleXMLElement($data);
  132.  
  133.   return $data;
  134. }
  135.  
  136.  
  137. /**
  138. * Récupere les infos d'un type d'impression depuis sont id
  139. *
  140. * @param int le numero id du type d'impression
  141. *
  142. * @return array xml objet
  143. */
  144. function getPrintType($id) {
  145.   global $config;
  146.  
  147.   $user_agent = 'my-siteProductType/1.0 (http://www.my-site.fr; mail@test.fr)';
  148.   @ini_set ('user_agent', $user_agent);
  149.  
  150.   $url = 'https://api.spreadshirt.net/api/v1/shops/'.$config['idShop'].'/printTypes/'.$id.'?locale=fr_FR';
  151.  
  152.   $header = array();
  153.   $header[] = createSprdAuthHeader("GET", $url);
  154.   $header[] = "Content-Type: application/xml";
  155.  
  156.   $ch = curl_init ($url);
  157.   curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
  158.   curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  159.   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  160.   curl_setopt($ch, CURLOPT_HEADER, false);
  161.   curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  162.   $data = curl_exec($ch);
  163.   curl_close ($ch);
  164.   $data = new SimpleXMLElement($data);
  165.  
  166.   return $data;
  167.  
  168. }
  169.  
  170.  
  171. /**
  172. * Récupere les infos d'un design
  173. *
  174. * @param int le numero id du type de produit
  175. *
  176. * @return array xml objet
  177. */
  178. function getDesignInfoApi($id) {
  179.   global $config;
  180.  
  181.   $user_agent = 'my-siteProductType/1.0 (http://www.my-site.fr; mail@test.fr)';
  182.   @ini_set ('user_agent', $user_agent);
  183.  
  184.   $url = 'https://api.spreadshirt.net/api/v1/shops/'.$config['idShop'].'/designs/m'.$id.'?locale=fr_FR';
  185.  
  186.   $header = array();
  187.   $header[] = createSprdAuthHeader("GET", $url);
  188.   $header[] = "Content-Type: application/xml";
  189.  
  190.   $ch = curl_init ($url);
  191.   curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
  192.   curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  193.   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  194.   curl_setopt($ch, CURLOPT_HEADER, false);
  195.   curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  196.   $data = curl_exec($ch);
  197.   curl_close ($ch);
  198.   $data = new SimpleXMLElement($data);
  199.  
  200.   return $data;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement