WC_Product_Variation::get_permalink( array|null $item_object = null )

Wrapper for get_permalink. Adds this variations attributes to the URL.


Description Description


Parameters Parameters

$item_object

(Optional) item array If a cart or order item is passed, we can get a link containing the exact attributes selected for the variation, rather than the default attributes.

Default value: null


Top ↑

Return Return

(string)


Top ↑

Source Source

File: includes/class-wc-product-variation.php

	public function get_permalink( $item_object = null ) {
		$url = get_permalink( $this->get_parent_id() );

		if ( ! empty( $item_object['variation'] ) ) {
			$data = $item_object['variation'];
		} elseif ( ! empty( $item_object['item_meta_array'] ) ) {
			$data_keys   = array_map( 'wc_variation_attribute_name', wp_list_pluck( $item_object['item_meta_array'], 'key' ) );
			$data_values = wp_list_pluck( $item_object['item_meta_array'], 'value' );
			$data        = array_intersect_key( array_combine( $data_keys, $data_values ), $this->get_variation_attributes() );
		} else {
			$data = $this->get_variation_attributes();
		}

		$data = array_filter( $data, 'wc_array_filter_default_attributes' );

		if ( empty( $data ) ) {
			return $url;
		}

		// Filter and encode keys and values so this is not broken by add_query_arg.
		$data = array_map( 'urlencode', $data );
		$keys = array_map( 'urlencode', array_keys( $data ) );

		return add_query_arg( array_combine( $keys, $data ), $url );
	}


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.