Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter MarceFX

    (@marcefx)

    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

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

    Thread Starter MarceFX

    (@marcefx)

    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!

    @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.

    Thread Starter MarceFX

    (@marcefx)

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

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

    Thread Starter MarceFX

    (@marcefx)

    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?

    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.

    Thread Starter MarceFX

    (@marcefx)

    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!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘[Plugin: WooCommerce – excelling eCommerce] Custom permalink with attributes?’ is closed to new replies.