Skip to content

Instantly share code, notes, and snippets.

@Tim-Machine
Created October 25, 2013 20:44
Show Gist options
  • Select an option

  • Save Tim-Machine/7161533 to your computer and use it in GitHub Desktop.

Select an option

Save Tim-Machine/7161533 to your computer and use it in GitHub Desktop.
<?php
class utilities {
public function buildUriName($sku,$cat ,$url = false){
$cat_seg = explode(",", $cat);
if(count($cat_seg) <= 1){
return $sku;
}
$catId = $this->getCategory($sku);
return $sku.'_'.$catId;
}
private function getCategory($sku){
$url = Request::url();
$urlData = explode("/",$url);
$cat_slug = $urlData[count($urlData) - 2] ;
//get category info
$category = Category::where('category_slug' , $cat_slug)->first(array('category_id'));
//lets see if this product matches with this sku
$prod = Product::where('sku',$sku)->where('cat_id','LIKE','%'.$category->category_id.'%')->take('1');
// if this is the right product & cat return the cat
if($prod->count() > 0){
return $category->category_id;
}else{// we still need to get the cat, lets hit that db again
$prodfromSku = Product::where('sku',$sku)->first(array('cat_id'));
$catSegments = explode(',', $prodfromSku->cat_id);
return $catSegments[0];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment