Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function pm_detect_dynamic_product_attributes( $uri_parts, $request_url, $endpoints ) {
- global $wpdb, $permalink_manager_uris;
- // Make sure that no custom permalink was detected by Permalink Manager
- if ( ! empty( $pm_query['id'] ) ) {
- return $uri_parts;
- }
- // Make sure that $uri_parts variable is set correctly
- if ( empty( $uri_parts['uri'] ) ) {
- return $uri_parts;
- }
- // 1. Separate "product/%product%" from rest of URL
- preg_match( '/^(product\/[^\/]+)\/(.*)/', $uri_parts['uri'], $slugs );
- // 2. Check if any of slugs is a product attribute
- if ( ! empty( $slugs[2] ) ) {
- $found_attributes = array();
- $possible_attributes = explode( "/", $slugs[2] );
- foreach ( $possible_attributes as $slug ) {
- $sql_query = "SELECT t.slug, t.term_id, tt.taxonomy FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} AS tt ON(t.term_id = tt.term_id) WHERE 1=1 AND slug = %s";
- $term = $wpdb->get_row( $wpdb->prepare( $sql_query, array( $slug ) ) );
- if ( ! empty( $term->taxonomy ) ) {
- $found_attributes[ $term->taxonomy ] = $term->slug;
- // OPTIONAL. Keep the below line to preselect the attribute in the add-to-cart form
- $_REQUEST["attribute_{$term->taxonomy}"] = $term->slug;
- }
- }
- // 3. Check if the product URI is a custom permalink
- $product_id = array_search( $slugs[1], $permalink_manager_uris );
- // 4. Change the final query if the post (product) is found
- if ( ! empty( $product_id ) ) {
- $post_object = get_post( $product_id );
- if ( ! empty( $post_object->post_type ) && $post_object->post_type == 'product' ) {
- $uri_parts['uri'] = $slugs[1];
- // You can use the $found_attributes array in your custom code
- }
- }
- }
- return $uri_parts;
- }
- add_filter( 'permalink_manager_detect_uri', 'pm_detect_dynamic_product_attributes', 20, 3 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement