alexanderdejong.com

Wordpress Developer

  • Home
  • Blog
  • Projects
  • Contact
You are here: Home / Wordpress / WP Tutorials

How to connect to Zalando API with PHP and WordPress

Posted on March 3, 2017 Leave a Comment

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' );

Filed Under: API WP Tutorial, Wordpress, WP Tutorials

Recent Posts

  • Migrating a WordPress Website Manually with Phpmyadmin, Search and Replace, DigitalOcean and Runcloud
  • How to add Total Price Calculation to your WooCommerce Product Page with AJAX
  • How to Install Roots Bedrock on a Digital Ocean Droplet running on Serverpilot via SSH
  • Linux SSH Useful Server Commands to Remember: Grep, Permissions and more.
  • How to Install Roots Sage 8.5.1 on a Digital Ocean Droplet running on Serverpilot via SSH

Find more content

Affiliate links
  • cheap shared hosting
  • low priced virtual servers only 5$
Affiliates
  • If someone buys a product then I get a commission.
  • You pay the same price using my affiliate links.
  • I can spend more time making newsletters and tutorials.
  • Thanks for visiting!

Connect with me

Recent Comments

  • Pedro Gómez on Migrating a WordPress Website Manually with Phpmyadmin, Search and Replace, DigitalOcean and Runcloud
  • Tim Delmeire on How to add Total Price Calculation to your WooCommerce Product Page with AJAX
  • Jaehyuk J Yang on How to add Total Price Calculation to your WooCommerce Product Page with AJAX
  • Alexander de Jong on A guide on customising the membership experience with Theme my Login (TML)
  • Judit Sarkany on A guide on customising the membership experience with Theme my Login (TML)

Recent Posts

  • Migrating a WordPress Website Manually with Phpmyadmin, Search and Replace, DigitalOcean and Runcloud
  • How to add Total Price Calculation to your WooCommerce Product Page with AJAX
  • How to Install Roots Bedrock on a Digital Ocean Droplet running on Serverpilot via SSH
  • Linux SSH Useful Server Commands to Remember: Grep, Permissions and more.
  • How to Install Roots Sage 8.5.1 on a Digital Ocean Droplet running on Serverpilot via SSH

Copyright © 2014 | Alexander de Jong | About | Contact | Privacy | Sitemap