admin管理员组

文章数量:1122832

I code number 1 in functions.php and number 2 in .htaccess to get product permalink based on SKU.

Something like this: xyz/product-title/#sku How could I remove "#" from the permalink (in front of the SKU)?

When I remove it manually from code number 1 without changing code number 2, the links will become correct but they go to the 404 page.

Also, what should I do if I wanted to change my URLs to something like this: xyz/sku/product-title/

Code 1

"function jpb_custom_meta_permalink( $link, $post ){
$post_meta = get_post_meta( $post->ID, '<insert your meta key here>', true );
if( empty( $post_meta ) || !is_string( $post_meta ) )
   $post_meta = '<insert your default value (could be an empty string) here>';
$link = str_replace( '!!custom_field_placeholder!!', $post_meta, $link );
return $link;}
add_filter( 'post_link', 'jpb_custom_meta_permalink', 10, 2 );
function append_sku_string( $link, $post ) {
$post_meta = get_post_meta( $post->ID, '_sku', true );
     if ( 'product' == get_post_type( $post ) ) {
       $link = $link . '#' .$post_meta;
       return $link;}
}
add_filter( 'post_type_link', 'append_sku_string', 1, 2 );"

Code 2

"<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase / 

    RewriteRule ^whisky/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ ?sku=$1&product=$2 [NC,L]
    </IfModule>"

I code number 1 in functions.php and number 2 in .htaccess to get product permalink based on SKU.

Something like this: xyz.com/product-title/#sku How could I remove "#" from the permalink (in front of the SKU)?

When I remove it manually from code number 1 without changing code number 2, the links will become correct but they go to the 404 page.

Also, what should I do if I wanted to change my URLs to something like this: xyz.com/sku/product-title/

Code 1

"function jpb_custom_meta_permalink( $link, $post ){
$post_meta = get_post_meta( $post->ID, '<insert your meta key here>', true );
if( empty( $post_meta ) || !is_string( $post_meta ) )
   $post_meta = '<insert your default value (could be an empty string) here>';
$link = str_replace( '!!custom_field_placeholder!!', $post_meta, $link );
return $link;}
add_filter( 'post_link', 'jpb_custom_meta_permalink', 10, 2 );
function append_sku_string( $link, $post ) {
$post_meta = get_post_meta( $post->ID, '_sku', true );
     if ( 'product' == get_post_type( $post ) ) {
       $link = $link . '#' .$post_meta;
       return $link;}
}
add_filter( 'post_type_link', 'append_sku_string', 1, 2 );"

Code 2

"<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase / 

    RewriteRule ^whisky/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ ?sku=$1&product=$2 [NC,L]
    </IfModule>"
Share Improve this question edited Oct 3, 2024 at 8:29 Umesh Singh 1491 silver badge8 bronze badges asked Oct 3, 2024 at 7:30 AbdolazizAbdolaziz 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

For this I suggest you to use the modified code in the given steps:

Step 1: You need to replace your code given in step 1 with this code.

function jpb_custom_meta_permalink( $link, $post ) {
    $post_meta = get_post_meta( $post->ID, '<insert your meta key here>', true );
    if ( empty( $post_meta ) || !is_string( $post_meta ) ) {
        $post_meta = '<insert your default value (could be an empty string) here>';
    }
    $link = str_replace( '!!custom_field_placeholder!!', $post_meta, $link );
    return $link;
}

add_filter( 'post_link', 'jpb_custom_meta_permalink', 10, 2 );

function append_sku_string( $link, $post ) {
    $post_meta = get_post_meta( $post->ID, '_sku', true );
    if ( 'product' == get_post_type( $post ) ) {
        // Here we are removing '#' and appending SKU before the product title.
        $link = str_replace('/product/', "/{$post_meta}/product/", $link);
        return $link;
    }
}

add_filter( 'post_type_link', 'append_sku_string', 10, 2 );

Step 2: In this step you need to use the given code in .htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # This is the rewrite rule for SKU before the product title.
    RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ index.php?sku=$1&product=$2 [NC,L]
</IfModule>

Step 3: We need additional code to confirm that WooCommerce recognizes the SKU correctly.

add_action('pre_get_posts', 'custom_rewrite_product_query');

function custom_rewrite_product_query($query) {
    if (!is_admin() && $query->is_main_query() && $query->is_product()) {
        if (isset($_GET['sku'])) {
            // We are using this SKU to find the product.
            $sku = sanitize_text_field($_GET['sku']);
            $query->set('meta_query', array(
                array(
                    'key'     => '_sku',
                    'value'   => $sku,
                    'compare' => '='
                )
            ));
        }
    }
}

本文标签: phpI39ve added SKU to Woocommerce permalinksbut I have small issue