Woocommerce. Add product SKU to url before slug - php

I am trying to add the woocommerce product SKU to the URL of the product. I have followed this guide (http://www.themelocation.com/how-to-add-sku-to-product-url-in-woocommerce/) which works, however I need the SKU to come before the slug.
Like this:
http://test.com/shop/category/sku-product
I have amended the code in the above link to successfully get this order in the wordpress admin area, however as you might predict, it is causing all the products to 404.
How do I go about fixing this? Some kind of htaccess redirect rule?
Here is my amended code:
function custom_meta_permalink( $link, $post ){
$post_meta = get_post_meta( $post->ID, '_sku', true );
if( empty( $post_meta ) || !is_string( $post_meta ) )
$post_meta = '';
$link = str_replace( '!!custom_field_placeholder!!', $post_meta, $link );
return $link;
}
add_filter( 'post_link', '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 ) ) {
$end = substr($link, strrpos($link, '/') + 1);
$beg = substr($link, 0, strrpos( $link, '/' ));
$link = $beg . '/' .$post_meta.'-'.$end;
return $link;
}
}
add_filter( 'post_type_link', 'append_sku_string', 1, 2 );

Related

How to get category/subcategory/post name in url custom post type

I registered custom post type and I registerem custom taxonomy. I added category "Hormann" and I added subcategory "Bramy". I would like to have url in my post "mysite.com/archive_name/category/subacategory/post_name". Please help me.
Edit: I did it writed code in my file functions.php, but my code is not working properly, because displays the link after entering the category "mysite.com/archive_name/category/", but it displays the wrong link after click in the post "mysite.com/archive_name/subacategory/post_name"
add_filter('post_type_link', 'projectcategory_permalink_structure', 10, 4);
function projectcategory_permalink_structure($post_link, $post, $leavename, $sample) {
if (false !== strpos($post_link, '%kategorie_dladomu%')) {
$projectscategory_type_term = get_the_terms($post->ID, 'kategorie_dladomu');
$slug = [];
foreach ( $projectscategory_type_term as $project ){
if ( $project->parent == 0 ) {
array_unshift( $slug, sanitize_title_with_dashes( $project->name ) );
} else {
array_push( $slug, sanitize_title_with_dashes( $project->name ) );
}
if ( ! empty( $slug ) ) {
return str_replace( '%kategorie_dladomu%' , join( '/', $slug ) , $post_link );
}
}
}
return $post_link;
}

Change status of woocommerce products by php function

I have a function which help me to make a redirect of a product if that product doesn't exist anymore in my affiliate xml.
What I want now is to put the product on draft but to redirect his link to a "This products doesnt exist anymore page" - for seo purposes
I didn't try something because I dont know what.
I saw something here and I think my answear is here but i don't know how to apply it:
Change product status if prices are updated in Woocommerce 3
function my_is_post_to_delete( $is_post_to_delete, $post_id, $import ) {
$redirect_url = "https://stackoverflow.com";
if ( $import->id == 72 ) {
$redirects = get_option( '301_redirects', array() );
$redirects = maybe_unserialize( $redirects );
$url_for_post = get_the_permalink( $post_id );
$url = parse_url( $url_for_post );
if ( $url ) {
if ( ! array_key_exists( $url['path'], $redirects ) ) {
$redirects[ $url['path'] ] = $redirect_url;
update_option( '301_redirects', $redirects );
}
}
return false;
}
}
add_filter( 'wp_all_import_is_post_to_delete', 'my_is_post_to_delete', 10, 3 );
I expect that my code to remove the product from my website feed and redirect his link to a page of my website.
For seo purposes I cannot delete the product for all I just need to hide it from my shop but keep the link for google (also the pictures).
You can use WordPress function to update the status of the product where your logic of product doesn't exist anymore in my affiliate xml. exists.
function my_is_post_to_delete( $is_post_to_delete, $post_id, $import ) {
$redirect_url = "https://stackoverflow.com";
if ( $import->id == 72 ) {
/* Start Here*/
$my_product = array(
'ID' => $import->id,
'post_status' => 'draft',
);
wp_update_post( $my_product );
/* End Here*/
$redirects = get_option( '301_redirects', array() );
$redirects = maybe_unserialize( $redirects );
$url_for_post = get_the_permalink( $post_id );
$url = parse_url( $url_for_post );
if ( $url ) {
if ( ! array_key_exists( $url['path'], $redirects ) ) {
$redirects[ $url['path'] ] = $redirect_url;
update_option( '301_redirects', $redirects );
}
}
return false;
}
}
add_filter( 'wp_all_import_is_post_to_delete', 'my_is_post_to_delete', 10, 3 );

Wordpress Category for pages

I'm having a problem with receiving the value from %category% in $wp_rewrite->page_structure :
function custom_page_rules() {
global $wp_rewrite;
$wp_rewrite->page_structure = $wp_rewrite->root .'/%category%/%pagename%';
//flush_rewrite_rules();
}
add_action( 'init', 'custom_page_rules',1 );
How can I receive the %category% in a $wp_rewrite->page_structure?
Now it returns:
http://example.com/%category%/the-page-slug/
Instead:
http://example.com/my-cat-slug/the-page-slug/
Note Categories for pages are working well:
http://example.com/my-cat-slug/
returns all pages from this category.
A solution could be:
function rudr_post_permalink( $url, $post ){
if( !is_object( $post ) )
$post = get_post( $post_id );
$replace = $post->post_name;
/* We should use a post ID to make a replacement. It is required if you use urf-8 characters in your URLs */
if( $post->ID == 1 )
$replace = 'hello-planet';
if( $post->ID == 12 )
$replace = 'Contacts';
$url = str_replace($post->post_name, $replace, $url );
return $url;
}
add_filter( 'post_link', 'rudr_post_permalink', 'edit_files', 2 );
add_filter( 'page_link', 'rudr_post_permalink', 'edit_files', 2 );
add_filter( 'post_type_link', 'rudr_post_permalink', 'edit_files', 2 );
Which is explained here
You can change the if( $post->ID == 1 ) to anything you need.
I have changed the whole code to add categories in the URL's when it are pages.
Regards, Danny

How to display Woocommerce product price by ID with correct Commas?

I was trying to display the price of a product on a custom page through a short code.
I found this thread: How to display Woocommerce product price by ID number on a custom page?
With this CODE:
function so_30165014_price_shortcode_callback( $atts ) {
$atts = shortcode_atts( array(
'id' => null,
), $atts, 'bartag' );
$html = '';
if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
$_product = wc_get_product( $atts['id'] );
$html = "price = " . $_product->get_price();
}
return $html;
}
add_shortcode( 'woocommerce_price', 'so_30165014_price_shortcode_callback' );
Shortcode: [woocommerce_price id="99"]
I've implemented the code and got it working the only thing now is that the price is not being displayed right it's not registering the commas.
For example I have a price that's being displayed as $13564.34
When it should be displayed as $13,564.34
It's also doing the same for $1371.43
When it should be displayed as $1,371.43
The function number_format() may help. For example:
1578.47 would change to: 1,578.47 after
number_format(1578.47, 2, '.', ',');
http://php.net/manual/en/function.number-format.php
GOT IT WORKING NOW.
CODE:
function so_30165014_price_shortcode_callback( $atts ) {
$atts = shortcode_atts( array(
'id' => null,
), $atts, 'bartag' );
$html = '';
if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
$_product = wc_get_product( $atts['id'] );
$number = number_format($_product->get_price(), 2, '.', ',');
$html = "$" . $number;
}
return $html;
}
add_shortcode( 'woocommerce_price', 'so_30165014_price_shortcode_callback' );
SHORTCODE:
[woocommerce_price id="99"]

Woocommerce Add to Cart redirect to parent category

I've been trying this for days, I just found this code:
//This is to redirect add to cart button to restaurant page add_filter
('add_to_cart_redirect', 'redirect_to_previousCat');
function redirect_to_previousCat( $url ) {if ( isset( $_POST['add-to-cart'] ) )
{
$product_id = (int) apply_filters('woocommerce_add_to_cart_product_id', $_POST['add-to-cart'] );
$terms = get_the_terms( $product_id, 'product_cat' );
foreach($terms as $term) {
$product_cat_slug = $term->slug;
break;
}
if( $product_cat_slug ){
$url = user_trailingslashit(get_permalink() . '/restaurant/' . $product_cat_slug );
}
}
return $url;
}
my goal was to redirect the add to cart button to its
/restaurant/parent-category-slug
but what the code does was redirect it to the /restaurant/product-category/
I really need some help here.
I believe you are missing the get_term_link() function.
Therefore, you'd modify your code to this:
if( $product_cat_slug ){
$url = get_term_link( $product_cat_slug, 'product_cat' );
}

Categories