• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WooCommerce a2z

WooCommerce a2z

WooCommerce

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / WC_API_XML_Handler::format_product_data() – Adjust the product data to handle options for attributes without a named child element and other fields that have no named child elements (e.g. categories = array( ‘cat1’, ‘cat2’ ) )

You appear to be a bot. Output may be restricted

Description

Adjust the product data to handle options for attributes without a named child element and other fields that have no named child elements (e.g. categories = array( 'cat1', 'cat2' ) )

Note that the parent product data for variations is also adjusted in the same manner as needed

Usage

$array = WC_API_XML_Handler::format_product_data( $data );

Parameters

$data
( array ) required –

Returns

array

Source

File name: woocommerce/includes/legacy/api/v1/class-wc-api-xml-handler.php


Lines:

1 to 80 of 80
  public function format_product_data( $data ) {

    // handle attribute values
    if ( ! empty( $data['attributes'] ) ) {

      foreach ( $data['attributes'] as $attribute_key => $attribute ) {

        if ( ! empty( $attribute['options'] ) && is_array( $attribute['options'] ) ) {

          foreach ( $attribute['options'] as $option_key => $option ) {

            unset( $data['attributes'][ $attribute_key ]['options'][ $option_key ] );

            $data['attributes'][ $attribute_key ]['options']['option'][] = array( $option );
          }
        }
      }
    }

    // simple arrays are fine for JSON, but XML requires a child element name, so this adjusts the data
    // array to define a child element name for each field
    $fields_to_fix = array(
      'related_ids'    => 'related_id',
      'upsell_ids'     => 'upsell_id',
      'cross_sell_ids' => 'cross_sell_id',
      'categories'     => 'category',
      'tags'           => 'tag',
    );

    foreach ( $fields_to_fix as $parent_field_name => $child_field_name ) {

      if ( ! empty( $data[ $parent_field_name ] ) ) {

        foreach ( $data[ $parent_field_name ] as $field_key => $field ) {

          unset( $data[ $parent_field_name ][ $field_key ] );

          $data[ $parent_field_name ][ $child_field_name ][] = array( $field );
        }
      }
    }

    // handle adjusting the parent product for variations
    if ( ! empty( $data['parent'] ) ) {

      // attributes
      if ( ! empty( $data['parent']['attributes'] ) ) {

        foreach ( $data['parent']['attributes'] as $attribute_key => $attribute ) {

          if ( ! empty( $attribute['options'] ) && is_array( $attribute['options'] ) ) {

            foreach ( $attribute['options'] as $option_key => $option ) {

              unset( $data['parent']['attributes'][ $attribute_key ]['options'][ $option_key ] );

              $data['parent']['attributes'][ $attribute_key ]['options']['option'][] = array( $option );
            }
          }
        }
      }

      // fields
      foreach ( $fields_to_fix as $parent_field_name => $child_field_name ) {

        if ( ! empty( $data['parent'][ $parent_field_name ] ) ) {

          foreach ( $data['parent'][ $parent_field_name ] as $field_key => $field ) {

            unset( $data['parent'][ $parent_field_name ][ $field_key ] );

            $data['parent'][ $parent_field_name ][ $child_field_name ][] = array( $field );
          }
        }
      }
    }

    return $data;
  }
 

 View on GitHub View on Trac

Published: 26th November 2019 | Last updated: 10th December 2020

Primary Sidebar

Information

Function name: WC_API_XML_Handler::format_product_data
Class ref: WC_API_XML_Handler
Plugin ref: WooCommerce
Version: 4.8.0
Sourcefile: includes/legacy/api/v1/class-wc-api-xml-handler.php
File ref: includes/legacy/api/v1/class-wc-api-xml-handler.php
Deprecated?: No
API Letters: A,F,H,W,X

Footer

WooCommerce a2z
WooCommerce a2z
WooCommerce
WordPress 5.6
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  woocommerce.wp-a2z.org
© Copyright WooCommerce a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites