Custom XML feed

Custom XML feed for WooCommerce VARIABLE products. Create PHP file inside Root of WP, Access that file via filename.php?action=whatever.

<?php
require_once('wp-load.php');

//die;
$query_args = array(
  'post_type' => 'product_variation',
  'post_status' => 'publish',
  'posts_per_page' => '-1',
  'meta_query' => array(
    '0' => array(
      'key' => '_stock',
      'value' => '1',
      'compare' => '>=',
    ),
    'relation' => 'AND',
  ),
);

if (!empty($_GET['action'])) {

  $query_args = array(
    'post_type' => 'product_variation',
    'post_status' => 'publish',
    'posts_per_page' => '4975',

    'meta_query' => array(
      '0' => array(
        'key' => '_stock',
        'value' => '1',
        'compare' => '>=',
      ),
      'relation' => 'AND',
    ),
  );
}
// The Query
$the_query = new WP_Query($query_args);
$xml = new SimpleXMLElement('<xml/>');
// The Loop
if ($the_query->have_posts()) {
  while ($the_query->have_posts()) {
    
    $the_query->the_post();
    $product = wc_get_product(get_the_ID());

    $parent = wc_get_product($product->get_parent_id());
    $track = $xml->addChild('product');
    $track->addChild('SKU', $product->get_sku());
    $track->addChild('ID', $product->get_id());
    $track->addChild('maat', $product->get_attribute('maten'));
    $track->addChild('kleur', $parent->get_attribute('kleuren'));
    $track->addChild('materiaal', $parent->get_attribute('materiaal'));
    $track->addChild('afbeeldingslink', wp_get_attachment_url($product->get_image_id()));
    $afb = $track->addChild('extra_afbeeldingslinks');
    
    $x = 0;
    foreach ($parent->get_gallery_image_ids() as $id) {
      $x++;
      $afb->addChild('afbeelding_' . $x, wp_get_attachment_url($id));
    }

    $track->addChild('titel', $product->get_name());
    $track->addChild('beschrijving', htmlentities($parent->get_description(), ENT_XML1));
    $track->addChild('extra_beschrijving', htmlentities($parent->get_short_description(), ENT_XML1));
    $track->addChild('plateauhoogte', $parent->get_attribute('plateauhoogte'));
    $track->addChild('hakhoogte', $parent->get_attribute('hakhoogte'));
    $track->addChild('type_hak', $parent->get_attribute('haksoort'));
    $track->addChild('Rits', $parent->get_attribute('rits'));
    $track->addChild('Plateau_in_cm', $parent->get_attribute('plateauhoogte'));
    $track->addChild('Neussoort', $parent->get_attribute('neussoort'));
    $track->addChild('prijs', $product->get_regular_price());
    $track->addChild('sale_prijs', $product->get_sale_price());
    $track->addChild('staat', "Nieuw");
    $track->addChild('Beschikbaarheid', "Op voorraad");
    $track->addChild('MPN', $product->get_sku());
  }
  $count = $the_query->post_count;

  Header('Content-type: text/xml');
  print($xml->asXML());

  wp_reset_postdata();
} else {

}
?>

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest
Author picture

SitiWeb is de expert op het gebied van WordPress. gespecialiseerd in het leveren van maatwerk.

Leave a comment

Geef een reactie

Je e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *