@sgrinko

Как добавить id в структуру URL товара в woocommerce?

Нужна помощь в решении задачи
Мне нужно к ссылке товара добавить и сам id этого же товара
чтоб получилось вот такое site.ru/имя_товара_id_товара/
может кто знает как правильно решить данный вопрос?
при всем при том что заменить нужно так все ссылки всех товаров )

пробовал использовать вот такой код. но результат не удовлетворяет. добавляется id не ко всем товарам. а к которым добавляет то не все работают по новой ссылке. на некоторых измененных ссылках выдает ошибку 404
if (!class_exists('PIDP4WC')) {
	final class PIDP4WC {

		protected static $_instance = null;
		public static function instance() {
			if (is_null(self::$_instance)) {
				self::$_instance = new self();
			}
			return self::$_instance;
		}
		public function __clone() {}
		public function __wakeup() {}
		public function __construct() {

			add_action('after_switch_theme', 'flush_rewrite_rules');
			register_deactivation_hook(__FILE__, 'flush_rewrite_rules');
			register_activation_hook(__FILE__, array($this, 'flush_rewrites'));
			add_action('init', array(__CLASS__, 'rewrites_init'));
			add_filter('post_type_link', array(__CLASS__, 'change_post_type_link'), 1, 3);
			if (!is_admin()) {
				add_filter('user_trailingslashit', array(__CLASS__, 'untrailingslash_single'), 10, 2);
			}
		}

		static public function untrailingslash_single($url, $type) {
			if ('single' === $type) {
				return untrailingslashit($url);
			}
			return trailingslashit($url);
		}

		static public function flush_rewrites() {
			self::rewrites_init();
			flush_rewrite_rules();
		}
		/*

			based on http://wordpress.stackexchange.com/questions/33551/how-to-rewrite-uri-of-custom-post-type/33555#33555
		*/
		static public function change_post_type_link($link, $post = 0, $leavename = '') {
			/*if ($post->post_type == 'product') {
				$link = home_url('product/'. $post->post_name .'id'. $post->ID );
				return $link;
			} else {
				return untrailingslashit($link);
			}*/
			if ( $post->post_type == 'product' ){
       				 return home_url( 'product/'. $post->post_name .'id' . $post->ID );
   			 } else {
     				   return $link;
 			   }
		}
		static public function rewrites_init() {
			/*add_rewrite_rule('^product/([0-9]+)?', 'index.php?post_type=product&p=$matches[1]', 'top');*/
add_rewrite_rule(
        'product/([0-9]+)?$',
        'index.php?post_type=product&p=$matches[1]',
        'top' );
		}

//add_action( 'init', 'wpse33551_rewrites_init' );


	} /*//CLASS*/
	$GLOBALS['PIDP4WC'] = PIDP4WC::instance();
}
  • Вопрос задан
  • 381 просмотр
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы