WC_Product_CSV_Exporter::prepare_attributes_for_export()protectedWC 3.1.0

Export attributes data.

Method of the class: WC_Product_CSV_Exporter{}

No Hooks.

Return

null. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->prepare_attributes_for_export( $product, $row );
$product(WC_Product) (required)
Product being exported.
$row(array) (required) (passed by reference — &)
Row being exported.

Changelog

Since 3.1.0 Introduced.

WC_Product_CSV_Exporter::prepare_attributes_for_export() code WC 9.4.2

protected function prepare_attributes_for_export( $product, &$row ) {
	if ( $this->is_column_exporting( 'attributes' ) ) {
		$attributes         = $product->get_attributes();
		$default_attributes = $product->get_default_attributes();

		if ( count( $attributes ) ) {
			$i = 1;
			foreach ( $attributes as $attribute_name => $attribute ) {
				/* translators: %s: attribute number */
				$this->column_names[ 'attributes:name' . $i ] = sprintf( __( 'Attribute %d name', 'woocommerce' ), $i );
				/* translators: %s: attribute number */
				$this->column_names[ 'attributes:value' . $i ] = sprintf( __( 'Attribute %d value(s)', 'woocommerce' ), $i );
				/* translators: %s: attribute number */
				$this->column_names[ 'attributes:visible' . $i ] = sprintf( __( 'Attribute %d visible', 'woocommerce' ), $i );
				/* translators: %s: attribute number */
				$this->column_names[ 'attributes:taxonomy' . $i ] = sprintf( __( 'Attribute %d global', 'woocommerce' ), $i );

				if ( is_a( $attribute, 'WC_Product_Attribute' ) ) {
					$row[ 'attributes:name' . $i ] = html_entity_decode( wc_attribute_label( $attribute->get_name(), $product ), ENT_QUOTES );

					if ( $attribute->is_taxonomy() ) {
						$terms  = $attribute->get_terms();
						$values = array();

						foreach ( $terms as $term ) {
							$values[] = $term->name;
						}

						$row[ 'attributes:value' . $i ]    = $this->implode_values( $values );
						$row[ 'attributes:taxonomy' . $i ] = 1;
					} else {
						$row[ 'attributes:value' . $i ]    = $this->implode_values( $attribute->get_options() );
						$row[ 'attributes:taxonomy' . $i ] = 0;
					}

					$row[ 'attributes:visible' . $i ] = $attribute->get_visible();
				} else {
					$row[ 'attributes:name' . $i ] = html_entity_decode( wc_attribute_label( $attribute_name, $product ), ENT_QUOTES );

					if ( 0 === strpos( $attribute_name, 'pa_' ) ) {
						$option_term = get_term_by( 'slug', $attribute, $attribute_name ); // @codingStandardsIgnoreLine.
						$row[ 'attributes:value' . $i ]    = $option_term && ! is_wp_error( $option_term ) ? html_entity_decode( str_replace( ',', '\\,', $option_term->name ), ENT_QUOTES ) : html_entity_decode( str_replace( ',', '\\,', $attribute ), ENT_QUOTES );
						$row[ 'attributes:taxonomy' . $i ] = 1;
					} else {
						$row[ 'attributes:value' . $i ]    = html_entity_decode( str_replace( ',', '\\,', $attribute ), ENT_QUOTES );
						$row[ 'attributes:taxonomy' . $i ] = 0;
					}

					$row[ 'attributes:visible' . $i ] = '';
				}

				if ( $product->is_type( 'variable' ) && isset( $default_attributes[ sanitize_title( $attribute_name ) ] ) ) {
					/* translators: %s: attribute number */
					$this->column_names[ 'attributes:default' . $i ] = sprintf( __( 'Attribute %d default', 'woocommerce' ), $i );
					$default_value                                   = $default_attributes[ sanitize_title( $attribute_name ) ];

					if ( 0 === strpos( $attribute_name, 'pa_' ) ) {
						$option_term = get_term_by( 'slug', $default_value, $attribute_name ); // @codingStandardsIgnoreLine.
						$row[ 'attributes:default' . $i ] = $option_term && ! is_wp_error( $option_term ) ? $option_term->name : $default_value;
					} else {
						$row[ 'attributes:default' . $i ] = $default_value;
					}
				}
				$i++;
			}
		}
	}
}