WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
Custom permalink with attributes? (10 posts)

  1. MarceFX
    Member
    Posted 1 year ago #

    Hi,

    I'd like to ger this kind of custom permalink for the products URL:

    site.com/attribute1/attribute2/post-title

    Any idea about how to get it done?

    Thank you

    http://wordpress.org/extend/plugins/woocommerce/

  2. MarceFX
    Member
    Posted 1 year ago #

    So far, I've been able to get the proper permalink structure with this piece of code:

    add_action('init', 'custom_init');
    add_filter('post_type_link', 'story_permalink', 10, 3);
    
    function custom_init(){
        $story = array(
        'query_var' => true,
        'rewrite' => false,
        );
        $artist = array(
        'query_var' => true,
        'rewrite' => true
        );
        $writer = array(
            'query_var' => true,
            'rewrite' => true
        );  
    
        global $wp_rewrite;
        $story_structure = '%pa_dispositivo%/%pa_provincia%/%product%/';
        $wp_rewrite->add_rewrite_tag("%product%", '([^/]+)', "story=");
        $wp_rewrite->add_permastruct('product', $story_structure, false);
    }
    
    function story_permalink($permalink, $post_id, $leavename){
        $post = get_post($post_id);
    
        $rewritecode = array(
        '%pa_dispositivo%',
        '%pa_provincia%',
        $leavename? '' : '%postname%',
        $leavename? '' : '%pagename%',
        );
    
        if('' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft'))){
    
            if (strpos($permalink, '%pa_dispositivo%') !== FALSE){
            $terms = wp_get_object_terms($post->ID, 'pa_dispositivo');
            if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $artist = $terms[0]->slug;
            else $artist = 'unassigned-pa_dispositivo';
            }
    
        if (strpos($permalink, '%pa_provincia%') !== FALSE){
            $terms = wp_get_object_terms($post->ID, 'pa_provincia');
            if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $writer = $terms[0]->slug;
            else $writer = 'unassigned-pa_provincia';
        }           
    
        $rewritereplace = array(
            $artist,
            $writer,
            $post->post_name,
            $post->post_name,
        );
        $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
        }
        else{
        }
        return $permalink;
    }

    But when I click on a product, I get a product listing page, instead of the actua single post page. Any ideas?

    Thanks

  3. Hauthorn
    Member
    Posted 1 year ago #

    I'm looking for a way to change the "/product/" part of the URL. Where did you find the code above?

  4. MarceFX
    Member
    Posted 1 year ago #

    If I remember properly... you just need to go to the pages settings on Woocommerce:

    yoursite.com/wp-admin/admin.php?page=woocommerce_settings&tab=pages

    Choose a page (previously created) with the name you need :-)

    Cheers!

  5. Hauthorn
    Member
    Posted 1 year ago #

    @MarceFX: That involves prepending the url with the shop-page url. Then I would have to modify the URL for the shop-page as well - which isn't what I need.

    I should clarify that I'd prefer to remove /product/, not just modify it.

  6. MarceFX
    Member
    Posted 1 year ago #

    Oops, sorry! Have you tried not choosing any page instead?

  7. Hauthorn
    Member
    Posted 1 year ago #

    Again, that would result in not having a shop-page, which isn't ideal.

  8. MarceFX
    Member
    Posted 1 year ago #

    Sorry, but I don't get it. You don't want to modify the name (product) and you don't want to remove it either, so what kind of URL structure are you looking for?

  9. Hauthorn
    Member
    Posted 1 year ago #

    I want to edit the URL structure without touching the shop-page url.

    I'd prefer to remove the /product/ from the URL, but if that isn't possible, I'd shorten it to something like /p/.

    This can be done by modifying the URL for the shop-page in the WooCommerce settings, but this is not desired.

    I basically need to write som rewrite rules (I guess), but I have no idea how to do this in WordPress.

  10. MarceFX
    Member
    Posted 1 year ago #

    I know what you mean. I guess is what I did with the code above, but let me tell you that it does create some conflicts. Woocommerce needs to distinguish between shop, categories, attributes, custom post type and singles posts.

    I had to remove the code because this URL structure was not working:

    site.com/attribute1/attribute2/product-title

    I started to get 404 when accessing attributes and some other weird stuff. Sorry I can provide a better explanation/source, but I think I read something about that on the Woocommerce Github site. What I ended up doing was using the regular product category: site.com/shop/category/post-title

    I just changed "shop" into another fancier name.

    Good luck!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic