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
1 Answer
Reset to default 0For 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
版权声明:本文标题:php - I've added SKU to Woocommerce permalinks, but I have small issue 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736283648a1927033.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论