Automattic\WooCommerce\Blocks\Domain\Services
CheckoutLink::get_product_options_url
Get the product page URL where the shopper can choose the attributes missing from a checkout link.
Attributes set by the variation, or passed in the link, are added to the URL so they are preselected.
Метод класса: CheckoutLink{}
Хуков нет.
Возвращает
string. The product page URL, or an empty string if the product page is not available.
Использование
// private - только в коде основоного (родительского) класса $result = $this->get_product_options_url( $product, $variation );
- $product(WC_Product) (обязательный)
- The variable product or variation from the checkout link.
- $variation(array[]) (обязательный)
- Variation attributes parsed from the checkout link.
Код CheckoutLink::get_product_options_url() CheckoutLink::get product options url WC 11.3.0
private function get_product_options_url( \WC_Product $product, array $variation ) {
$is_variation = $product instanceof \WC_Product_Variation;
$parent_product = $is_variation ? wc_get_product( $product->get_parent_id() ) : $product;
if ( ! $parent_product || ProductStatus::PUBLISH !== $parent_product->get_status() || ProductStatus::PUBLISH !== $product->get_status() ) {
return '';
}
$selected = $is_variation ? array_filter( $product->get_variation_attributes(), 'wc_array_filter_default_attributes' ) : [];
$requested = wp_list_pluck( $variation, 'value', 'attribute' );
foreach ( $parent_product->get_attributes() as $attribute ) {
if ( ! $attribute->get_variation() ) {
continue;
}
$attribute_name = $attribute->get_name();
$query_key = wc_variation_attribute_name( $attribute_name );
foreach ( [ $query_key, $attribute_name, strtolower( wc_attribute_label( $attribute_name, $parent_product ) ) ] as $requested_key ) {
if ( isset( $requested[ $requested_key ] ) && '' !== $requested[ $requested_key ] ) {
$selected[ $query_key ] = $attribute->is_taxonomy() ? sanitize_title( $requested[ $requested_key ] ) : $requested[ $requested_key ];
break;
}
}
}
// Encode keys and values so add_query_arg() keeps them intact, matching WC_Product_Variation::get_permalink().
return add_query_arg(
array_combine( array_map( 'urlencode', array_keys( $selected ) ), array_map( 'urlencode', $selected ) ),
$parent_product->get_permalink()
);
}