Advertisement
mbis

SKU WooCommerce Permalinks

Mar 21st, 2024
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.32 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Change permalink for WooCommerce product based on SKU and product name.
  5.  *
  6.  * @param string $url The generated permalink URL.
  7.  * @param WP_Post $post The post object.
  8.  * @return string The modified permalink URL.
  9.  */
  10. function pm_sku_permalinks( $url, $post ) {
  11.     if ( $post->post_type !== 'product' || $post->post_status !== 'publish' ) {
  12.         return $url;
  13.     }
  14.  
  15.     $product_sku  = get_post_meta( $post->ID, SKU_FIELD, true );
  16.     $product_name = $post->post_name;
  17.  
  18.     if ( empty( $product_sku ) || empty( $product_name ) ) {
  19.         return $url;
  20.     }
  21.  
  22.     $url = sprintf( "%s/%s/%s", get_home_url(), sanitize_title( $product_sku ), sanitize_title( $product_name ) );
  23.  
  24.     return user_trailingslashit( $url );
  25. }
  26. add_filter( 'post_type_link', 'pm_sku_permalinks', 999, 2 );
  27.  
  28. /**
  29.  * Detect WooCommerce product based on SKU and product name in the permalink.
  30.  *
  31.  * @param array $query The current query variables.
  32.  *
  33.  * @return array The modified query variables.
  34.  */
  35. function pm_detect_sku_product_permalinks( $query ) {
  36.     global $wpdb, $wp;
  37.  
  38.     // Do not run when Elementor is active
  39.     if ( defined( 'ELEMENTOR_PATH' ) || isset( $_REQUEST['elementor-preview'] ) ) {
  40.         return $query;
  41.     }
  42.  
  43.     $request_path = trim( $wp->request, '/' );
  44.  
  45.     // Check if the request path matches the expected pattern
  46.     if ( preg_match( '/^([^\/]+)\/([^\/]+)$/', $request_path, $matches ) ) {
  47.         $sku_number   = $matches[1];
  48.         $product_name = $matches[2];
  49.  
  50.         // Check if the SKU is assigned to any product
  51.         $sql_query = $wpdb->prepare( "SELECT p.ID FROM {$wpdb->posts} AS p
  52.            INNER JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id AND pm.meta_key = %s
  53.            WHERE p.post_type = 'product' AND p.post_status = 'publish' AND pm.meta_value = %s", SKU_FIELD, $sku_number );
  54.  
  55.         $product_id = $wpdb->get_var( $sql_query );
  56.  
  57.         // If product found, set query variables accordingly
  58.         if ( $product_id && get_post_field( 'post_name', $product_id ) === $product_name ) {
  59.             $query['post_type']       = 'product';
  60.             $query['name']            = $product_name;
  61.             $query['do_not_redirect'] = 1;
  62.  
  63.             // Disable canonical redirect
  64.             remove_action( 'template_redirect', 'wp_old_slug_redirect' );
  65.             remove_action( 'template_redirect', 'redirect_canonical' );
  66.         }
  67.     }
  68.  
  69.     return $query;
  70. }
  71. add_filter( 'request', 'pm_detect_sku_product_permalinks', 9999 );
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement