Support

Account

Home Forums General Issues add acf value to product permalink woocommerce

Unread

add acf value to product permalink woocommerce

  • Hello,

    i try to add values to woocommerce product permalink.

    my code :

    //add rewrite rules 
    function product_add_rewrite_rules() {
    	global $wp_rewrite;
        
    	$wp_rewrite->add_rewrite_tag('%product%', '([^/]+)', 'product=');
    	$wp_rewrite->add_rewrite_tag('%subtitle%', '([^/]+)', 'subtitle=');
    	$wp_rewrite->add_rewrite_tag('%artiste%', '([^/]+)', 'artiste=');
    	$wp_rewrite->add_rewrite_tag('%numero_de_page%', '([^/]+)', 'numero_de_page=');
    	$wp_rewrite->add_permastruct('product', '/catalogue/%artiste%-%product%-%subtitle%-page-%numero_de_page%/', false);
        
        $wp_rewrite->flush_rules();
    }
    add_action('init', 'product_add_rewrite_rules', 10, 0);
    
    // replace the rwrite tag by the content
    function product_permalinks($permalink, $post, $leavename) {
    	$post_id = $post->ID;
    
    	if ($post->post_type == 'product') {
    		$subtitle = sanitize_title(get_field('subtitle', $post_id));
    		$artiste = get_field('artiste', $post_id);
            $artistesurname = sanitize_title($artiste->name);
    		$pagenumber = sanitize_title(get_field('numero_de_page', $post_id));
    		if ($artiste) { 
    			$permalink = str_replace('%artiste%', $artistesurname, $permalink);
    		} else {
    			$permalink = str_replace('%artiste%', 0, $permalink);
    		}
            if ($subtitle) { 
    			$permalink = str_replace('%subtitle%', $subtitle, $permalink);
    		} else {
    			$permalink = str_replace('%subtitle%', 0, $permalink);
    		}
            if ($pagenumber) { 
    			$permalink = str_replace('%numero_de_page%', $pagenumber, $permalink);
    		} else {
    			$permalink = str_replace('%numero_de_page%', 0, $permalink);
    		}
    	}
    
    	return $permalink;
    }
    add_filter('post_type_link', 'product_permalinks', 10, 3);
    

    In the backend it’s showing the change :
    permalink display in the backend

    But in frontend i get a 404. I tried on an other custom post and i get the same.
    Anyone had already the same problem ?

    Thanks in advance,

    Simon

Viewing 1 post (of 1 total)

The topic ‘add acf value to product permalink woocommerce’ is closed to new replies.