News:

Support the VirtueMart project and become a member

Main Menu

Show (Sold out) when an option iss sold out

Started by Kuubs, June 22, 2023, 19:44:00 PM

Previous topic - Next topic

Kuubs

Hi,
I have a multi variat product, and i am trying to show (Sold out) in the text of the dropdown values so that people can instantly see whether or not the product is sold out (without having to click on it)

I'm trying to do that in the code, but i cannot figure out how to. do it.

case 'C':
$html = '';

$dropdowns = array();

if(isset($customfield->options->{$product->virtuemart_product_id})){
$productSelection = $customfield->options->{$product->virtuemart_product_id};
} else {
$productSelection = false;
}
$stockhandle = VmConfig::get('stockhandle_products', false) && $product->product_stockhandle ? $product->product_stockhandle : VmConfig::get('stockhandle','none');

$q = 'SELECT `virtuemart_product_id` FROM #__virtuemart_products WHERE product_parent_id = "'.$customfield->virtuemart_product_id.'" and ( published = "1" ';
if($stockhandle == 'disableit_children'){
$q .= ' AND (`product_in_stock` - `product_ordered`) > "0" ';
}
$q .= ');';
$db = JFactory::getDbo();
$db->setQuery($q);
$avail = $db->loadColumn();
if(!in_array($customfield->virtuemart_product_id,$avail)){
array_unshift($avail,$customfield->virtuemart_product_id);
}

foreach($customfield->options as $product_id=>$variants){
static $counter = 0;
if(!in_array($product_id,$avail)){
//vmdebug('$customfield->options Product to ignore, continue ',$product_id);
continue;
}

foreach($variants as $k => $variant){

if(!isset($dropdowns[$k]) or !is_array($dropdowns[$k])) $dropdowns[$k] = array();
if(!in_array($variant,$dropdowns[$k])  ){

if($k==0 or !$productSelection){
$dropdowns[$k][] = $variant;
} else{
if($productSelection[$k-1] == $variants[$k-1]) {
$break = false;
for( $h = 1; $h<=$k; $h++ ) {
if($productSelection[$h - 1] != $variants[$h - 1]) {
$break = true;
}
}
if(!$break) {
$dropdowns[$k][] = $variant;
}
}
}
}
}
}

$class = 'vm-chzn-select';
$selectType = 'select.genericlist';

if(!empty($customfield->selectType)){
$selectType = 'select.radiolist';
$class = '';
$dom = '';
} else {
vmJsApi::chosenDropDowns();
$dom = 'select';
}

$attribs = array('class'=>$class.' cvselection no-vm-bind','style'=>'min-width:70px;');

$view = 'productdetails';
$attribs['data-reload'] = '1';
if(VmConfig::get ('jdynupdate', TRUE)){
$view = vRequest::getCmd('view','productdetails');
if($view == 'productdetails' or ($customfield->browseajax and $view == 'category')){
$attribs['data-dynamic-update'] = '1';
unset($attribs['data-reload']);
} else {
$view = 'productdetails';
}
}

foreach($customfield->selectoptions as $k => $soption){
$html .= '<div class="custom_field_C_container custfield ">';

$options = array();
$selected = false;
if(isset($dropdowns[$k])){
foreach($dropdowns[$k] as $i=> $elem){

$elem = trim((string)$elem);
$text = $elem;

if($soption->clabel!='' and in_array($soption->voption,VirtueMartModelCustomfields::$dimensions) ){
$rd = $soption->clabel;
if(is_numeric($rd) and is_numeric($elem)){
$text = number_format(round((float)$elem,(int)$rd),$rd);
}
//vmdebug('($dropdowns[$k] in DIMENSION value = '.$elem.' r='.$rd.' '.$text);
} else if  ($soption->voption === 'clabels' and $soption->clabel!='') {
$text = vmText::_($elem);
}

if(empty($elem)){
$text = vmText::_('COM_VIRTUEMART_LIST_EMPTY_OPTION');
}
$o = new stdClass();
$o->value = $elem;
$o->text = $text;
$options[] = $o;

if($productSelection and $productSelection[$k] == $elem){
$selected = $elem;
}
}
}


if(empty($selected)){
$product->orderable=false;
}
//$idTagK = $idTag.'cvard'.$k;
if($customfield->showlabels){
if( in_array($soption->voption,VirtueMartModelCustomfields::$dimensions) ){
$soption->slabel = vmText::_('COM_VIRTUEMART_'.strtoupper($soption->voption));
} else if(!empty($soption->clabel) and !in_array($soption->voption,VirtueMartModelCustomfields::$dimensions) ){
$soption->slabel = vmText::_($soption->clabel);
}
if(isset($soption->slabel)){
$html .= '<label>'.$soption->slabel.'</label>';
}

}
                        // echo "<pre>";
                        // print_r($soption);
                        // echo "</pre>";
$html .= '<div class="selecthold">';
$idTagK = '['; //Joomla workaround to get a list without id
$attribs['data-cvsel'] = 'field' . $customfield->virtuemart_customfield_id ;
$fname = $fieldname.'['.$k.']';
$html .= JHtml::_ ($selectType, $options, $fname, $attribs , "value", "text", $selected,$idTagK);
$html .= '</div>';
if($soption->slabel == "Maat"){
$html .= '<div class="moreinfo"><span class="btn-text"><i class="far fa-info-circle"></i> Wat is mijn maat?</span><div class="utlg"><div>MATENUITLEG</div></div></div>';
}

$html .= '</div>';
}

$Itemid = vRequest::getInt('Itemid',''); // '&Itemid=127';
if(!empty($Itemid)){
$Itemid = '&Itemid='.$Itemid;
}

//create array for js
$jsArray = array();

$url = '';

foreach($customfield->options as $product_id=>$variants){

if(!in_array($product_id,$avail)){continue;}

$url = JRoute::_('index.php?option=com_virtuemart&view='.$view.'&virtuemart_category_id=' . $virtuemart_category_id . '&virtuemart_product_id='.$product_id.$Itemid,false);
$jsArray[] = '["'.$url.'","'.implode('","',$variants).'"]';
}

vmJsApi::addJScript('cvfind');

$BrowserNewState =  '';
if($view != 'productdetails'){
$BrowserNewState = 'Virtuemart.setBrowserState = false;';
}

$jsVariants = implode(',',$jsArray);

$selector = $dom."[data-cvsel=\"".$attribs['data-cvsel']."\"]";
$hash = md5($selector);
$j = "jQuery(document).ready(function($) {
".$BrowserNewState."
$('".$selector."').off('change',Virtuemart.cvFind);
$('".$selector."').on('change', { variants:[".$jsVariants."] },Virtuemart.cvFind);
});";
vmJsApi::addJScript('cvselvars'.$hash,$j,true,false,false,$hash);

//Now we need just the JS to reload the correct product
$customfield->display = $html;

break;


The urls are built after the dropdowns are built, so i'm splutting hairs how to check whether or not the product of that option has a stock > 0.

Kuubs

For the time being I added the $q .= ' AND (`product_in_stock` - `product_ordered`) > "0" '; outside of the $stockhandle == if statement so that if the stock is below 0 it will just not display.

Kuubs

I have found a solution using Javascript to alter the fields based on the stock amount of the product. If anybody is interested in the code, I'd love to share it!