Advertisement
mbis

Preselect product variant

Apr 8th, 2020
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.02 KB | None | 0 0
  1. /**
  2.  * Detect product permalinks
  3.  * URL Format: http://shop.com/product_slug/variation_sku
  4.  */
  5. function pm_detect_product_permalinks($request) {
  6.     global $wp, $wpdb;
  7.  
  8.     // Parse the URI
  9.     preg_match('/^(?:([a-z]{2})\/)?([^\/]+)(?:\/([^\/]+))?\/?$/i', $wp->request, $parts);
  10.  
  11.     if(!empty($parts[2])) {
  12.             $new_request = array();
  13.  
  14.             // Get product by slug
  15.             $product = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE post_name = %s AND post_type = %s AND post_status = %s",
  16.                 sanitize_title($parts[2]),
  17.                 'product',
  18.                 'publish'
  19.             ));
  20.  
  21.             // 1. Rewrite request array if product was found
  22.             if(!empty($product->post_name)) {
  23.                 $new_request['name'] = $product->post_name;
  24.                 $new_request['post_type'] = 'product';
  25.                 $new_request['product'] = $product->post_name;
  26.                 $new_request['do_not_redirect'] = 1;
  27.             }
  28.  
  29.             // 2. Get variation by SKU
  30.             if(!empty($parts[3])) {
  31.                 $variation_id = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s",
  32.                     '_sku',
  33.                     sanitize_title($parts[3])
  34.                 ));
  35.  
  36.                 if(is_numeric($variation_id)) {
  37.                     $product_variation = wc_get_product((int) $variation_id);
  38.                     $variation_variations = $product_variation->get_variation_attributes();
  39.  
  40.                     // Preselect attributes
  41.                     if(!empty($variation_variations)) {
  42.                         foreach($variation_variations as $attr_name => $attr_value) {
  43.                             // $attr_name = str_replace('attribute_', '', $attr);
  44.                             $_REQUEST[$attr_name] = $attr_value;
  45.                         }
  46.                     }
  47.                 } else {
  48.                     return $request;
  49.                 }
  50.             }
  51.  
  52.             $request = $new_request;
  53.     }
  54.  
  55.     return $request;
  56. }
  57. add_filter('request', 'pm_detect_product_permalinks', 99);
  58.  
  59. /**
  60.  * Filter product permalinks
  61.  */
  62. function bis_filter_woo_permalinks($url, $element) {
  63.     if(!empty($element->post_type) && ($element->post_type == 'product')) {
  64.         $url = trim(get_option('home'), '/') . "/{$element->post_name}";
  65.     }
  66.  
  67.     return $url;
  68. }
  69. add_filter('post_type_link', 'bis_filter_woo_permalinks', 999, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement