Skip to content

Instantly share code, notes, and snippets.

@clipvui2512
Created July 18, 2020 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clipvui2512/ba26b89302868f2b0a6d944af034d35b to your computer and use it in GitHub Desktop.
Save clipvui2512/ba26b89302868f2b0a6d944af034d35b to your computer and use it in GitHub Desktop.
public function getJsondata($product){
if(count($product['images']) == 0 ){
return null;
}
$json = array(
'options' => array(),
'variants' => array(),
);
foreach ($product['options'] as $option){
$json['options'][] = $option['name'];
}
foreach ($product['variants'] as $variant){
$featured_image = $product['images'][0]['src'];
if(array_key_exists('featured_image',$variant)){
if(is_array($variant['featured_image'] )){
$featured_image = $variant['featured_image']['src'];
}
}
$sku_goc = $variant['sku'] ? $variant['sku'] : $product['id'].$product['created_at'];
if(strtolower($product['vendor']) == 'customcat'){
$sku_goc = 'cc-'.$sku_goc ;
}
$json['variants'][] = array(
'options' => $this->getJsondataOption($json['options'],$variant),
'sku' => $sku_goc ,
'price' => $variant['price'],
'img' => $featured_image,
);
}
return $json;
}
public function getJsondataOption($options,$variant){
$rs = array();
foreach ($options as $index=>$option){
$option_index = $index + 1;
$rs[$index] = html_entity_decode($variant['option'.$option_index] , ENT_QUOTES ) ;
}
return $rs;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment