WordPress at Your Fingertips
function is not described

WC_Product_CSV_Exporter::prepare_attributes_for_export() protected WC 3.1.0

Export attributes data.

{} It's a method of the class: WC_Product_CSV_Exporter{}

No Hooks.

Return

null. Nothing.

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.

Code of WC_Product_CSV_Exporter::prepare_attributes_for_export() WC 5.9.0

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 ] = wc_attribute_label( $attribute->get_name(), $product );

					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 ] = wc_attribute_label( $attribute_name, $product );

					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 ) ? str_replace( ',', '\\,', $option_term->name ) : str_replace( ',', '\\,', $attribute );
						$row[ 'attributes:taxonomy' . $i ] = 1;
					} else {
						$row[ 'attributes:value' . $i ]    = str_replace( ',', '\\,', $attribute );
						$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++;
			}
		}
	}
}