Viewing 14 replies - 1 through 14 (of 14 total)
  • Plugin Contributor titodevera

    (@titodevera)

    It’s posible.

    1) Add this code to your functions.php

    function pwb_brand_permalink($permalink, $post, $leavename) {
     if (strpos($permalink, '%pwb-brand%') === FALSE) return $permalink;
    
     $product_brands = wp_get_object_terms($post->ID, 'pwb-brand');
    
     $slug = 'brand'; //default
    
     if ( ! empty( $product_brands ) ) {
    	 if ( ! is_wp_error( $product_brands ) ) {
    		 foreach( $product_brands as $brand ) {
    			 $slug = $brand->slug;
    		 }
    	 }
     }
    
     return str_replace('%pwb-brand%', $slug , $permalink);
    }
    
    include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
    if(is_plugin_active('woocommerce/woocommerce.php') && is_plugin_active('perfect-woocommerce-brands/main.php')){
    	add_filter('post_link', 'pwb_brand_permalink', 10, 3);
    	add_filter('post_type_link', 'pwb_brand_permalink', 10, 3);
    }

    2) Go to ‘Settings > Permalinks’ and set the custom base (for ‘Product Permalinks’):
    /products/%pwb-brand%
    Don′t forget to save changes.

    ??

    Thread Starter Jason Wong

    (@eljkmw)

    Many thanks, titodevera.

    Won’t it be better to replace is_plugin_active(‘woocommerce/woocommerce.php’) and is_plugin_active(‘perfect-woocommerce-brands/main.php’) with class_exists( ‘WooCommerce’ ) and class_exists( ‘Perfect_Woocommerce_Brands’ ) respectively?

    I’m yet to try out your solution. Will update here again soon.

    Thread Starter Jason Wong

    (@eljkmw)

    All is working well, except that class_exists( ‘Perfect_Woocommerce_Brands’ ) isn’t.
    Looks like I’ve to stick to your intended codes instead. My bad …

    By the way, I noticed when a product doesn’t have a brand assigned, its URL will be domain-name/products/brand/product-name. How do I make into, domain-name/products/product-name, which removes ‘brand’ from the URL?

    Thread Starter Jason Wong

    (@eljkmw)

    Also, so far I noticed that sub-brand (or child brand) doesn’t appear to work with your plugin.

    For example, I added “Brand A” as a parent brand, and “Brand B” as its child brand, the product URL only shows its parent brand, i.e., domain-name/products/Brand-A/product-name. Ideally, it should be domain-name/products/Brand-A/Brand-B/product-name.

    Plugin Contributor titodevera

    (@titodevera)

    Many thanks, titodevera.

    Won’t it be better to replace is_plugin_active(‘woocommerce/woocommerce.php’) and is_plugin_active(‘perfect-woocommerce-brands/main.php’) with class_exists( ‘WooCommerce’ ) and class_exists( ‘Perfect_Woocommerce_Brands’ ) respectively?

    I’m yet to try out your solution. Will update here again soon.

    From my point of view is more reliable to use is_plugin_active.

    PWB uses php namespaces. If you want to check if a PWB class exists you should indicate the namespace before the name of the class. Something like this:
    class_exists( '\Perfect_Woocommerce_Brands\Perfect_Woocommerce_Brands' );

    Thread Starter Jason Wong

    (@eljkmw)

    No wonder class_exists didn’t work in this case. Thank you for the lesson. (^_^)

    Anyway, any ideas on how to make the permalink recognise both parent brand, and child brand (like I’ve mentioned above) …?

    Plugin Contributor titodevera

    (@titodevera)

    For example, I added “Brand A” as a parent brand, and “Brand B” as its child brand, the product URL only shows its parent brand, i.e., domain-name/products/Brand-A/product-name. Ideally, it should be domain-name/products/Brand-A/Brand-B/product-name.

    Try this code:

    function pwb_brand_permalink($permalink, $post, $leavename) {
      if (strpos($permalink, '%pwb-brand%') === FALSE) return $permalink;
    
      $product_brands = wp_get_object_terms($post->ID, 'pwb-brand');
    
      $slug = 'brand'; //default
    
      if ( ! empty( $product_brands ) ) {
     	 if ( ! is_wp_error( $product_brands ) ) {
    
    		 $first_brand = key( array_slice( $product_brands, 0, 1, true ) );
    		 $last_brand = key( array_slice( $product_brands, -1, 1, true ) );
    
    		 if ( $first_brand === $last_brand ) {
    			 //only one brand
    			 $slug = $product_brands[$first_brand]->slug;
    		 } else {
    			 //more than one brand
    			 $slug = '';
    			 foreach ($product_brands as $key => $brand) {
    				 $slug .= $brand->slug;
    				 if($key != $last_brand){
    					 $slug .= '/';
    				 }
    			 }
    		 }
    
     	 }
      }
    
      return str_replace('%pwb-brand%', $slug , $permalink);
     }
    
     include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
     if(is_plugin_active('woocommerce/woocommerce.php') && is_plugin_active('perfect-woocommerce-brands/main.php')){
     	add_filter('post_link', 'pwb_brand_permalink', 10, 3);
     	add_filter('post_type_link', 'pwb_brand_permalink', 10, 3);
     }

    Plugin Contributor titodevera

    (@titodevera)

    By the way, I noticed when a product doesn’t have a brand assigned, its URL will be domain-name/products/brand/product-name. How do I make into, domain-name/products/product-name, which removes ‘brand’ from the URL?

    It is not posible remove it, but you can customize it as you want. Change this line: $slug = 'brand'; //default in the code above

    Thread Starter Jason Wong

    (@eljkmw)

    Many thanks, titodevera … You ROCK !!

    It is not posible remove it, but you can customize it as you want. Change this line: $slug = ‘brand’; //default in the code above

    Is it possible to find out if a Product has a Brand assigned to it? If that’s possible, that function could be inserted into an IF statement, which can either leave the $slug empty or not.

    Plugin Contributor titodevera

    (@titodevera)

    Yes. It is posible find out if a product has a brand assigned or not… But if you leave the empty $slug, the products which have no brands assigned will throw a 404 error on his single page.

    Thread Starter Jason Wong

    (@eljkmw)

    Thanks for the explanation. Anyway, I’ve just uploaded your new code into functions.php, and I noticed that the permalink has the parent-brand succeeding its child-brand. e.g.,

    domain-name/products/brands/child-brand/parent-brand/product-name

    I guess that this FOR loop is indexing the brands in reverse.

    foreach ($product_brands as $key => $brand) {
    	$slug .= $brand->slug;
    	if($key != $last_brand){
    		$slug .= '/';
    	}
    }

    Thread Starter Jason Wong

    (@eljkmw)

    I just noticed something odd with your suggested code above. It will capture one Parent Brand only.

    $first_brand = key( array_slice( $product_brands, 0, 1, true ) );
    $last_brand = key( array_slice( $product_brands, -1, 1, true ) );

    This is presumed that the first index within $product_brands is a Parent Brand, while the rest are its Child Brands.

    What if there were multiple Parent and Child Brands, and they weren’t in any order?

    Your current plugin works fine with just Parent Brands alone. But to include Child Brands will be a big leap forward.

    Thread Starter Jason Wong

    (@eljkmw)

    By the way, I’ve added another screenshot into here, which reveals the Sub (or Child) Brand’s URL.

    This is exactly the link in the Single Product page that I’ve been looking for. Unfortunately, I noticed that it’s ignore “/products/” from the Product Permalink Custom Base that I’d set. Strange, isn’t it?

    Thread Starter Jason Wong

    (@eljkmw)

    I went sniffing around your plugin’s source code, and found in the classes folder the file class-perfect-woocommerce-brands.php that contains the action_woocommerce_single_product_summary function. In it has the following line of code.
    $brand_link = get_term_link ( $brand->term_id, 'pwb-brand' );

    Can this be used to alter your suggested functions.php code above? At least, it is displaying the format that I hoped for, except that it’s ignored the /product/ from the Product Permalink Custom Base.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Change of Permalink’ is closed to new replies.