Advertisement
mbis

Add SKU to product permalinks

Aug 8th, 2022
833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.28 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. Plugin Name: Add SKU to Product Permalinks
  5. Plugin URI: http://maciejbis.net
  6. Description: A plugin that adjusts the functionality of this website
  7. Version: 1.0
  8. Author: Maciej Bis
  9. Author URI: http://www.maciejbis.net
  10. License: GPL2
  11. */
  12.  
  13. define('SKU_FIELD', '_sku');
  14.  
  15. function pm_sku_in_woo_permalinks($url, $post) {
  16.     global $permalink_manager_uris;
  17.  
  18.     if(!empty($post->post_type) && ($post->post_type == 'product') && ($post->post_status == 'publish')) {
  19.         $product_sku = get_post_meta($post->ID, SKU_FIELD, true);
  20.  
  21.         if(!empty($product_sku)) {
  22.             $product_sku = sanitize_title($product_sku);
  23.  
  24.             $url = trim(get_option('home'), '/') . "/shop/{$product_sku}";
  25.             $url = user_trailingslashit($url);
  26.         }
  27.     }
  28.  
  29.     return $url;
  30. }
  31. add_filter('post_type_link', 'pm_sku_in_woo_permalinks', 999, 2);
  32.  
  33. function pm_detect_sku_woo_permalinks($query) {
  34.     global $wpdb, $pm_query, $wp;
  35.  
  36.     // Do not run when Elementor is opened
  37.     if((!empty($_REQUEST['action']) && strpos($_REQUEST['action'], 'elementor') !== false) || isset($_REQUEST['elementor-preview'])) {
  38.         return $query;
  39.     }
  40.  
  41.     if(!empty($wp->request) && strpos($wp->request, 'shop/') !== false) {
  42.         $sku_number = basename($wp->request);
  43.  
  44.         // 2. Check if the slug is assigned to any post item
  45.         $sql_query = $wpdb->prepare("SELECT p.* FROM {$wpdb->posts} AS p JOIN {$wpdb->postmeta} AS pm ON pm.post_id = p.ID AND pm.meta_key = %s WHERE 1=1 AND meta_value = %s AND post_type = %s AND post_status = 'publish'", array(SKU_FIELD, $sku_number, 'product'));
  46.         $post = $wpdb->get_row($sql_query);
  47.  
  48.         // 3. Filter the query if post was found
  49.         if(!empty($post->post_name)) {
  50.             // $page = (!empty($query['page'])) ? $query['page'] : 1;
  51.             $new_query = array(
  52.                 'post_type' => $post->post_type,
  53.                 $post->post_type => $post->post_name,
  54.                 'name' => $post->post_name,
  55.                 // 'page' => $page,
  56.                 'do_not_redirect' => 1,
  57.             );
  58.  
  59.             // 4. Disable canonical redirect
  60.             remove_action('template_redirect', 'wp_old_slug_redirect');
  61.             remove_action('template_redirect', 'redirect_canonical');
  62.             add_filter('wpml_is_redirected', '__return_false', 99, 2);
  63.             add_filter('pll_check_canonical_url', '__return_false', 99, 2);
  64.  
  65.             return $new_query;
  66.         }
  67.     }
  68.  
  69.     return $query;
  70. }
  71. add_filter('request', 'pm_detect_sku_woo_permalinks', 9999);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement