Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * récupére le prix d'un produit
- *
- * @param string url du produit
- *
- * @return array les informations du prix
- */
- function getProductPriceApi($url) {
- /////////////////////////////
- // info sur le product
- /////////////////////////
- $url = htmlspecialchars($url); // url du produit : exemple : https://api.spreadshirt.net/api/v1/shops/ID-SHOP/products/ID-PRODUCT
- $product = getProductApi($url); // récupére les informations du PRODUCT dans l'API
- $configurations = $product->configurations; // récupére la configuration du PRODUCT
- $idProductType = $product->productType->attributes()->id; // récupére l'id du PRODUCT-TYPE
- /////////////////////////////
- // info sur le productType dont le prix
- /////////////////////////
- $productType = getProductType($idProductType); // récupére les informations du PRODUCT-TYPE dans l'API
- $productTypePrice = $productType->price->vatIncluded; // récupére le prix du PRODUCT-TYPE
- $productTypePrice = (float) $productTypePrice; // déclare le prix du PRODUCT-TYPE dans la variable
- // CALCUL TOTAL
- $prixTotal = $productTypePrice; // ajoute le prix du PRODUCT-TYPE au prix total du produit
- /////////////////////////////
- // récupére toutes les informations de configuration du PRODUCT
- /////////////////////////
- foreach ($configurations->configuration as $value) {
- // PRINT TYPE INFORMATIONS
- $id = $value->printType->attributes()->id; // récupére l'id du type de d'impression
- $infoPrint = getPrintType($id); // récupére les informations de type d'impression
- // PRINT TYPE COLOR PRICE INFORMATIONS
- if(!empty($infoPrint->colors)){
- $nbrColor = $value->content->svg->image->attributes()->printColorIds; // récupére l'attributes *printColorIds* exemple : "18 18 187"
- $nbrColor = array_count_values(explode(' ',$nbrColor)); // crée un tableau avec les valeurs regroupé exemple : [18 => 2, 187 => 1]
- foreach ($nbrColor as $key => $color) {
- 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
- $idPrintTypeColor = $c->attributes()->id; // je récupére l'id de la couleur
- if($key == $idPrintTypeColor){ // si un id de couleur est identique a mes couleurs
- // CALCUL TOTAL
- $prixTotal = $prixTotal + ( (float) $c->price->vatIncluded); // j'ajoute le prix de cette couleur a mon prix total
- }
- }
- }
- }
- // PRINT TYPE PRICE
- $printTypePrice = (float) $infoPrint->price->vatIncluded;
- // DESIGN PRICE
- $designPrice = (float) getDesignInfoApi($value->designs->design->attributes()->id)->price->vatIncluded;
- // CALCUL TOTAL
- $prixTotal = $prixTotal + $printTypePrice + $designPrice;
- }
- return $prixTotal;
- }
- /**
- * Récupere les infos d'un produit depuis sont url API
- *
- * @param string url du produit dans l'api
- *
- * @return array xml objet
- */
- function getProductApi($url) {
- global $config;
- @ini_set ('user_agent', $user_agent);
- $url = $url.'?locale=fr_FR';
- $header = array();
- $header[] = createSprdAuthHeader("GET", $url);
- $header[] = "Content-Type: application/xml";
- $ch = curl_init ($url);
- curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
- curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HEADER, false);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- $data = curl_exec($ch);
- curl_close ($ch);
- $data = new SimpleXMLElement($data);
- return $data;
- }
- /**
- * Récupere les infos d'un type de produit depuis sont id
- *
- * @param int le numero id du type de produit
- *
- * @return array xml objet
- */
- function getProductType($id) {
- global $config;
- @ini_set ('user_agent', $user_agent);
- $url = 'https://api.spreadshirt.net/api/v1/shops/'.$config['idShop'].'/productTypes/'.$id.'?locale=fr_FR';
- $header = array();
- $header[] = createSprdAuthHeader("GET", $url);
- $header[] = "Content-Type: application/xml";
- $ch = curl_init ($url);
- curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
- curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HEADER, false);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- $data = curl_exec($ch);
- curl_close ($ch);
- $data = new SimpleXMLElement($data);
- return $data;
- }
- /**
- * Récupere les infos d'un type d'impression depuis sont id
- *
- * @param int le numero id du type d'impression
- *
- * @return array xml objet
- */
- function getPrintType($id) {
- global $config;
- @ini_set ('user_agent', $user_agent);
- $url = 'https://api.spreadshirt.net/api/v1/shops/'.$config['idShop'].'/printTypes/'.$id.'?locale=fr_FR';
- $header = array();
- $header[] = createSprdAuthHeader("GET", $url);
- $header[] = "Content-Type: application/xml";
- $ch = curl_init ($url);
- curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
- curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HEADER, false);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- $data = curl_exec($ch);
- curl_close ($ch);
- $data = new SimpleXMLElement($data);
- return $data;
- }
- /**
- * Récupere les infos d'un design
- *
- * @param int le numero id du type de produit
- *
- * @return array xml objet
- */
- function getDesignInfoApi($id) {
- global $config;
- @ini_set ('user_agent', $user_agent);
- $url = 'https://api.spreadshirt.net/api/v1/shops/'.$config['idShop'].'/designs/m'.$id.'?locale=fr_FR';
- $header = array();
- $header[] = createSprdAuthHeader("GET", $url);
- $header[] = "Content-Type: application/xml";
- $ch = curl_init ($url);
- curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
- curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HEADER, false);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- $data = curl_exec($ch);
- curl_close ($ch);
- $data = new SimpleXMLElement($data);
- return $data;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement