Skip to content

Instantly share code, notes, and snippets.

@jonathandavis
Created May 13, 2013 21:41
Show Gist options
  • Save jonathandavis/5571766 to your computer and use it in GitHub Desktop.
Save jonathandavis/5571766 to your computer and use it in GitHub Desktop.
class RelatedProducts extends SmartCollection {
static $_slug = "related";
static $_menu = false;
var $product = false;
function smart ($options=array()) {
$this->slug = self::$_slug;
$where = array();
$scope = array();
$Product = ShoppProduct();
$Order = ShoppOrder();
$Cart = $Order->Cart;
// Use the current product is available
if (!empty($Product->id))
$this->product = ShoppProduct();
// Or load a product specified
if (isset($options['product'])) {
if ($options['product'] == "recent-cartitem") // Use most recently added item in the cart
$this->product = new Product($Cart->Added->product);
elseif (preg_match('/^[\d+]$/',$options['product'])) // Load by specified id
$this->product = new Product($options['product']);
else $this->product = new Product($options['product'],'slug'); // Load by specified slug
}
if (isset($options['tagged'])) {
$tagged = new ProductTag($options['tagged'],'name');
if (!empty($tagged->id)) $scope[] = $tagged->id;
$name = $tagged->name;
$slug = $tagged->slug;
}
if (!empty($this->product->id)) {
$name = $this->product->name;
$slug = $this->product->slug;
$where = array("p.id != {$this->product->id}");
// Load the product's tags if they are not available
if (empty($this->product->tags))
$this->product->load_data(array('tags'));
if (!$scope) $scope = array_keys($this->product->tags);
print_r($scope);
}
if (empty($scope)) return false;
$this->name = __("Products related to","Shopp")." "".stripslashes($name).""";
$this->uri = urlencode($slug);
$this->controls = false;
global $wpdb;
$joins[$wpdb->term_relationships] = "INNER JOIN $wpdb->term_relationships AS tr ON (p.ID=tr.object_id)";
$joins[$wpdb->term_taxonomy] = "INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id=tt.term_taxonomy_id";
$where[] = "tt.term_id IN (".join(',',$scope).")";
$columns = 'COUNT(p.ID) AS score';
$groupby = 'p.ID';
$order = 'score DESC';
$this->loading = compact('columns','joins','where','groupby','order');
if (isset($options['order'])) $this->loading['order'] = $options['order'];
if (isset($options['controls']) && value_is_true($options['controls']))
unset($this->controls);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment