4. Februar 2010
bjoern

Ich habe für den Kindermoden-Shop www.kimob.de das WordPress-Plugin “SimpleTags” ein wenig erweitert. Dem Kunden werden jetzt unterhalb des Beitrages analog zu den “Related Posts” die “related Products” angezeigt.

Hierzu werden die im Betrag verwendeten Tags und deren Gewichtung im Beitrag zur Produktsuche herangezogen.
Dem Blog-Besucher werden also passend zum Beitrag einige passenden Produkte angezeigt.

Dies ist so aber auch nur möglich, da sich Shop (xtcommerce) und Blog eine Datenbank teilen.

Um dies zu realisieren habe ich einfach die Funktion für die “related Posts” als Vorlage verwendet und entsprechend angepasst.
Ausgegangen von der Version 1.7.1 des Plugins habe ich folgende Änderungen durchgeführt.

“simple-tags.client.php”

493
494
495
496
if ( $marker === true ) {
     return ( $content . $this->relatedPosts( '', false )  );
}
return $content;

geändert in

493
494
495
496
497
$post_content_for_products = $content;
if ( $marker === true ) {
     return ( $content . $this->relatedProducts( '', false, $post_content_for_products ). $this->relatedPosts( '', false )  );
}
return $content;

und ich habe vor der Funktion “relatedPosts” meine angepasste Funktion eingefügt (bei mir startet es in Zeile 545)

545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
 
	function relatedProducts( $user_args = '', $copyright = false, $post_content_for_products ) {
 
		$defaults = array(
		'number' => 5,
		'order' => 'count-desc',
		'format' => 'list',
		'separator' => '',
		'include_page' => 'true',
		'include_cat' => '',
		'exclude_posts' => '',
		'exclude_tags' => '',
		'post_id' => 0,
		'excerpt_wrap' => 55,
		'limit_days' => 0,
		'min_shared' => 1,
		'title' => __('', 'simpletags'),
		'nopoststext' => __('No related posts.', 'simpletags'),
		'dateformat' => $this->dateformat,
		'xformat' => __('<a href="%post_permalink%" title="%post_title%">%post_title%</a>', 'simpletags')
		);
 
		// Get values in DB
		$defaults['number'] = $this->options['rp_limit_qty'];
		$defaults['order'] = $this->options['rp_order'];
		$defaults['nopoststext'] = 'Keine verwandten Produkte';
		$defaults['title'] = '<h4>Verwandte Produkte</h4>';
 
		if( empty($user_args) ) {
			$user_args = $this->options['rp_adv_usage'];
		}
 
		// Replace old markers by new
		$markers = array('%date%' => '%post_date%', '%permalink%' => '%post_permalink%', '%title%' => '%post_title%', '%commentcount%' => '%post_comment%', '%tagcount%' => '%post_tagcount%', '%postid%' => '%post_id%');
		if (!is_array($user_args)) $user_args = strtr($user_args, $markers);
 
		$args = wp_parse_args( $user_args, $defaults );
		extract($args);
 
		// If empty use default xformat !
		if ( empty($xformat) ) {
			$xformat = $defaults['xformat'];
		}
 
		// Clean memory
		$args = array();
		$defaults = array();
 
		// Get current post data
		$object_id = (int) $post_id;
		if ( $object_id == 0 ) {
			global $post;
			$object_id = (int) $post->ID;
			if ( $object_id == 0 ) {
				return false;
			}
		}
 
			// Get get tags
			$current_tags = get_the_tags( (int) $object_id );
 
			if ( $current_tags === false ) {
				return $this->outputContent( 'st-related-posts', $format, $title, $nopoststext, $copyright );
			}
 
			// Number - Limit
			$number = (int) $number;
			if ( $number == 0 ) {
				$number = 5;
			} elseif( $number > 50 ) {
				$number = 50;
			}
			$limit_sql = 'LIMIT 0, '.$number;
			unset($number);
 
			// Order tags before output (count-asc/count-desc/date-asc/date-desc/name-asc/name-desc/random)
			$order_by = '';
			$order = strtolower($order);
			switch ( $order ) {
				case 'count-asc':
					$order_by = 'counter ASC, p.post_title DESC';
					break;
				case 'random':
					$order_by = 'RAND()';
					break;
				case 'date-asc':
					$order_by = 'p.post_date ASC';
					break;
				case 'date-desc':
					$order_by = 'p.post_date DESC';
					break;
				case 'name-asc':
					$order_by = 'p.post_title ASC';
					break;
				case 'name-desc':
					$order_by = 'p.post_title DESC';
					break;
				default: // count-desc
				$order_by = 'counter DESC, p.post_title DESC';
				break;
			}
 
			unset($limit_days);
			unset($include_page);
			unset($exclude_posts);
			unset($exclude_tags);
 
			// SQL Tags list
			$tag_list = '';
			foreach ( (array) $current_tags as $tag ) {
				$tag_list .= '"'.(int) $tag->term_id.'", ';
			}
 
			// If empty return no posts text
			if ( empty($tag_list) ) {
				return $this->outputContent( 'st-related-posts', $format, $title, $nopoststext, $copyright );
			}
 
			// Remove latest ", "
			$tag_list = substr($tag_list, 0, strlen($tag_list) - 2);
 
			global $wpdb;
 
			// Group Concat only for MySQL > 4.1 and check if post_relatedtags is used by xformat...
			$select_gp_concat = '';
			if ( version_compare(mysql_get_server_info(), '4.1.0', '>=') && ( strpos($xformat,'%post_relatedtags%') || $min_shared > 1 ) ) {
				$select_gp_concat = ', GROUP_CONCAT(tt.term_id) as terms_id';
			} else {
				$xformat = str_replace('%post_relatedtags%', '', $xformat); // Group Concat only for MySQL > 4.1, remove related tags
			}
			$terms_array = $wpdb->get_results("select name from wp_terms t, wp_term_taxonomy tt where t.term_id = tt.term_id and tt.taxonomy = 'post_tag' and t.term_id in (".$tag_list.") order by tt.count limit 0,1");
 
			$str_count_act = 0;
			$str_count_high = 0;
			foreach ( $terms_array as $term ) {
				$str_count_act = substr_count(strip_tags($post_content_for_products), $term->name);
				if ($str_count_act == $str_count_high) {
					$terms .= $term->name."' '";
					$str_count_high = $str_count_act;
				}
				if ($str_count_act > $str_count_high) {
					$terms = $term->name;
					$str_count_high = $str_count_act;
				}
			}
 
			// manufacturer_id
			// Hier sollte jeder für sich selber entscheiden bzw anpassen.
			if (strpos(strtolower(' '.$terms),'reima') > 0) {
				$manufacturer = "and p.manufacturers_id = '2' ";
				$terms = trim(str_replace('reima','',strtolower($terms)));
				}
			if (strpos(strtolower(' '.$terms),'jeep') > 0) {
				$manufacturer = "and p.manufacturers_id = '8' ";
				$terms = trim(str_replace('jeep','',strtolower($terms)));
				}
			if (strpos(strtolower(' '.$terms),'first') > 0) {
				$manufacturer = "and p.manufacturers_id = '6' ";
				$terms = trim(str_replace('first','',strtolower($terms)));
				}
			if (strpos(strtolower(' '.$terms),'modas') > 0) {
				$manufacturer = "and p.manufacturers_id = '5' ";
				$terms = trim(str_replace('modas','',strtolower($terms)));
				}
			if (strpos(strtolower(' '.$terms),'lemmi') > 0) {
				$manufacturer = "and p.manufacturers_id = '1' ";
				$terms = trim(str_replace('lemmi','',strtolower($terms)));
				}
			if (strpos(strtolower(' '.$terms),'roxy') > 0) {
				$manufacturer = "and p.manufacturers_id = '10' ";
				$terms = trim(str_replace('roxy','',strtolower($terms)));
				}
			if (strpos(strtolower(' '.$terms),'quiksilver') > 0) {
				$manufacturer = "and p.manufacturers_id = '11' ";
				$terms = trim(str_replace('quiksilver','',strtolower($terms)));
				}
 
			$searchstring = $terms;
			//fulltext-string
			$fulltext = "and MATCH ( pd.products_name,pd.products_description,pd.products_meta_description) AGAINST ('".$searchstring."')";
			$sorting = "ORDER BY ".substr($fulltext,4)." desc";
 
			if ($terms != '' || $manufacturer != '') {
				$listing_sql = "select DISTINCT pd.products_name
								from products_description pd,
									  manufacturers m,
									  products_to_categories p2c,
									  products p 
										  where p.products_status = '1'
										  and p.manufacturers_id = m.manufacturers_id ".$manufacturer."
										  and p.products_id = p2c.products_id
										  and pd.products_id = p2c.products_id
										  ".$fulltext."
										  ".$fsk_lock."
										  and pd.language_id = '2' ".$sorting." LIMIT 0,4";
 
			// Posts: title, comments_count, date, permalink, post_id, counter
			$results = $wpdb->get_results($listing_sql);
			}
 
		if ( $format == 'object' || $format == 'array' ) {
			return $results;
		} elseif ( $results === false || empty($results) ) {
			return $this->outputContent( 'st-related-posts', $format, $title, $nopoststext, $copyright );
		}
 
		if ( empty($dateformat) ) {
			$dateformat = $this->dateformat;
		}
 
		$output = array();
 
		// Replace placeholders
		foreach ( (array) $results as $result ) {
			if ( ( $min_shared > 1 && ( count(explode(',', $result->terms_id)) < $min_shared ) ) || !is_object($result) ) {
				continue;
			}
 
			$element_loop = $xformat;
			$post_title = apply_filters( 'the_title', $result->products_name );
			$element_loop = str_replace('%post_date%', mysql2date($dateformat, $result->post_date), $element_loop);
			$element_loop = str_replace('%post_permalink%', 'http://www.kimob.de/product_info.php?products_id='.$result->products_id, $element_loop);
			$element_loop = str_replace('%post_title%', $post_title, $element_loop);
			$element_loop = str_replace('%post_title_attribute%', wp_specialchars(strip_tags($post_title)), $element_loop);
			$element_loop = str_replace('%post_comment%', $result->comment_count, $element_loop);
			$element_loop = str_replace('%post_tagcount%', $result->counter, $element_loop);
			$element_loop = str_replace('%post_id%', $result->ID, $element_loop);
			$element_loop = str_replace('%post_relatedtags%', $this->getTagsFromID($result->terms_id), $element_loop);
			$element_loop = str_replace('%post_excerpt%', $this->getExcerptPost( $result->post_excerpt, $result->post_content, $result->post_password, $excerpt_wrap ), $element_loop);
			$output[] = $element_loop;
		}
 
		// Clean memory
		$results = array();
		unset($results, $result);
		return $this->outputContent( 'st-related-posts', $format, $title, $output, $copyright, $separator );
	}

Der Code bzw die Anpassung mag nicht unbedingt schlank sein und nicht ganz sauber sein, aber er funktioniert sehr gut.

Kommentieren