Here is a quick way to integrate Zalando API into your WordPress website.
Add Zalando PHP Wrapper from Github using Composer and BxSlider for Zalando Product Images
I used BXslider as a product image slider in this code and I added the ZalandoPHP wrapper from Github by Chris Schalenborgh with Composer. You can also manually add it to your project but I find that Composer is the way to go because it’s much easier to maintain your packages.
Use Ajax to fetch the product images
The product images in this code were only updated after each update of the window. I recommend you use AJAX to fix that problem.
I was about to add that but then we decided to stop using the Zalando integration in this project because of an alternative that fitted better with the business objective.
Hopefully this code is useful for someone out there! Have fun!
<?php /** * Created by Alexander on 9/15/2016. */ use ZalandoPHP\ZalandoPHP; use ZalandoPHP\Configuration\GenericConfiguration; use ZalandoPHP\Operations\Articles; // use ZalandoPHP\Operations\Filters; function atp_zalando_api_products($attr) { //$articles->setCategory('baby-accessoires,baby-accessoires-promo,babys-schoenen,babys-schoenen-promo,babyslaapzak,babyslaapzak-promo,babyslofjes,babyslofjes-promo,kinderkleding-baby-ondergoed-rompers,kinderkleding-baby-ondergoed-rompers-promo,luxe-baby-accessoires'); //$articles->setCategory('babys-schoenen-promo'); //$articles->setCategory('mens-lightweight-running-shoes-sale'); // Develop Wrapper so that Price Range is included //$articles->setPrice(9-60); //$articles->setCategory('sportschuhe-herren-sale'); //$articles->setColor('red,green'); //$articles->setBrand('adidas'); $conf = new GenericConfiguration(); try { $conf ->setLocale('de-de') ->setClientName('zalando-php-wrapper') ->setResponseType('array') ->setTimeout(12) ->setConnectionTimeout(45); } catch (\Exception $e) { echo $e->getMessage(); } $zalandoPHP = new ZalandoPHP($conf); $shoes = new Articles(); $shoes->setPage(1); $shoes->setPageSize(5); $shoes->setCategory('laufschuhe-herren'); $shirts = new Articles(); $shirts->setPage(1); $shirts->setPageSize(5); $shirts->setCategory('sports-herren-shirts'); $shorts = new Articles(); $shorts->setPage(2); $shorts->setPageSize(5); $shorts->setCategory('sporthosen-herren'); extract( shortcode_atts( array( 'type' => shoes //default price ), $attr ) ); if($type === 'shoes') { $formattedResponse = $zalandoPHP->runOperation($shoes); } if($type === 'shirts') { $formattedResponse = $zalandoPHP->runOperation($shirts); } if($type === 'shorts') { $formattedResponse = $zalandoPHP->runOperation($shorts); } foreach($formattedResponse['content'] as $content => $products) { // echo ' <div class="row"> '; echo ' <div class="col-md-6"> '; // Loop throgh the product images echo ' <div class="col-md-4"> '; // Change BX Slider so that it loads in Modal echo ' <ul class="bxslider col-md-4">'; foreach ($products['media']['images'] as $product) { $image = $product['smallHdUrl']; echo ' <li><img src="'. $image .'"></li> '; } echo '</ul> '; echo '</div> '; // Loop through the product name echo ' <div class="col-md-8"> '; $name = $products['name']; echo ' <h4>' . $name .'</h4> '; // Loop through the product sale price and original price $i = 1; foreach ($products['units'] as $product) { $salePrice = $product['price']['formatted']; $originalPrice = $product['originalPrice']['formatted']; if ( $originalPrice == $salePrice) { echo '<span style="color:green;font-size:20px;">' . $salePrice .'</span>'; if($i == 1) break; } else { echo '<span style="color:green;font-size:20px;">' . $salePrice .'</span>'; echo '<span style="color:red;text-decoration: line-through;">' . $originalPrice .'</span>'; if($i == 1) break; } } // Loop through the product sizes echo '<select name="type">'; foreach ($products['units'] as $product) { $stock = $product['stock']; $size = $product['size']; echo '<option value="'. $size .'"'.(strcmp($option,$type)==0?' selected="selected"':'').'>Size: '. $size .'Stock:' . $stock .'</option>'; } echo '</select>'; echo '<a href="#" class="button btn">Add to Cart</a>'; echo '</div> '; echo '</div> '; // echo '</div> '; } // echo ' <pre>'; // print_r($formattedResponse[content][0][units][0]); // print_r($formattedResponse); // print_r($formattedResponse[content][0][units][0]); // print_r($formattedResponse[content][0][units][0][price][formatted]); // print_r($formattedResponse[content][0][media][images]); // echo '</pre> '; } add_shortcode( 'zalando', 'atp_zalando_api_products' );
Recent Comments