JavaScript for WordPress › Forums › Gutenberg Development › Get post id › Reply To: Get post id
September 12, 2018 at 3:22 pm
#57831
chrisdavies71
Participant
Here is the ajax.php file:
<?php
require_once ( './class.php' );
define ('AJAX_URL', $_SERVER['PHP_SELF']);
function render_breadcrumb( $links=array() )
{
$html = '<div class="row mb-3">';
$html .= '<div class="col-md-11">';
$html .= '<nav aria-label="breadcrumb">';
$html .= '<ol class="breadcrumb">';
$html .= '<li class="breadcrumb-item"><a href="/">Home</a></li>';
$html .= '<li class="breadcrumb-item"><a href="#" onclick="ajax_load(\'query=product_groups\')">Hire & Sales</a></li>';
foreach ($links as $query=>$name)
{
$html .= '<li class="breadcrumb-item"><a href="#" onclick="ajax_load(\''.$query.'\')">'.$name.'</a></li>';
}
//$html .= '<li class="breadcrumb-item active" aria-current="page">Data</li>';
$html .= '</ol>';
$html .= '</nav>';
$html .= '</div>';
$html .= '<div class="col-md-1">';
$html .= '<button type="button" class="btn btn-primary" onclick="window.history.go(-1);">Back</button>';
$html .= '</div>';
$html .= '</div>';
return $html;
}
switch( $_GET['query'] )
{
// Product Groups (Categories) Page
case 'product_groups':
//$objCurrentRmsApi = new current_rms_api( $_GET['postid']);
$objCurrentRmsApi = new current_rms_api();
$product_groups = $objCurrentRmsApi->getProductGroups();
$html = render_breadcrumb();
$html .= '<div class="row">';
foreach( $product_groups as $product_group )
{
$html .= '<div id="productlist" class="col-md-4">';
$html .= '<div class="card">';
if (isset ($product_group->icon->url) ) {
$html .= '<img class="card-img-top" style="padding:1.25rem" src="'. $product_group->icon->url . '">';
}
$html .= '<div class="card-body">';
$html .= '<h5 class="card-title">' . $product_group->name . '</h5>';
$html .= '<p class="card-text">' . $product_group->description . '</p>';
$html .= '<a href="#" style="cursor:pointer" onclick="ajax_load(\'query=products&name=' . urlencode($product_group->name) . '\');" class="btn btn-primary">View Products</a>';
$html .= '</div>';
$html .= '</div>';
$html .= '</div>';
}
$html .= '</div>';
echo $html;
break;
// Products in each Product Group (Category)
case 'products':
$objCurrentRmsApi = new current_rms_api();
$products = $objCurrentRmsApi->getProducts( $_GET['name'] );
//$tags = $objCurrentRmsApi->getTags();
$tags=array();
foreach ($products as $product)
{
foreach ($product->tag_list as $tag)
{
if (!in_array($tag, $tags)) {
$tags[]=$tag;
}
}
}
sort($tags);
$html = render_breadcrumb(array('query=products&name='.urlencode($_GET['name'])=>$_GET['name']));
$counter = 0;
$html .= '<ul class="nav nav-tabs flex-column flex-sm-row nav-fill" id="myTab" role="tablist">';
foreach ($tags as $tag)
{
$html .= '<li class="flex-sm-fill text-center nav-item">';
$html .= '<a class="nav-link' . ($counter == 0 ? ' active' : '') . '" id="' . preg_replace('/\s+/', '_', $tag) . '_tab" data-toggle="tab" href="#' . preg_replace('/\s+/', '_', $tag) . '" role="tab" aria-controls"' . preg_replace('/\s+/', '_', $tag) . '" aria-selected="' . ($counter == 0 ? 'true' : 'false') .'">' . $tag . '</a>';
$html .= '</li>';
$counter++;
}
$html .= '</ul>';
$html .= '<div class="tab-content mt-4" id="myTabContent">';
$counter2 = 0;
foreach ($tags as $tag)
{
$html .= '<div class="tab-pane fade' . ($counter2 == 0 ? ' show active' : '') . '" id="' . preg_replace('/\s+/', '_', $tag) . '" role="tabpanel" aria-labelledby="' . preg_replace('/\s+/', '_', $tag) . '_tab">';
$html .= '<div class="row">';
foreach( $products as $product )
{
if (in_array($tag, $product->tag_list))
{
$html .= '<div class="col-md-4">';
$html .= '<div class="card">';
if (isset ($product->icon->url) )
{
$html .= '<img class="card-img-top p-3" src="'. $product->icon->url . '">';
} //end product icon isset
$html .= '<div class="card-body">';
$html .= '<h5 class="card-title">' . $product->name . '</h5>';
$html .= '<a href="#" style="cursor:pointer" onclick="ajax_load(\'query=product&id=' . urlencode($product->id) . '\');" class="btn btn-primary">View Product</a>';
//$html .= $product_tag;
$html .= '</div>'; //end card-body
$html .= '</div>'; //end card
$html .= '</div>'; //end col
} //end product_tag foreach
}// end product foreach
$html .= '</div>'; //end row
$html .= '</div>'; //end tab-pane
$counter2++;
}// end tags foreach
$html .= '</div>'; //end tab-content
echo $html;
break;
// Individual Product Page
case 'product':
$objCurrentRmsApi = new current_rms_api();
$product = $objCurrentRmsApi->getProduct( $_GET['id'] );
$accessories = $objCurrentRmsApi->getAccessories( $_GET['id'] );
$price = $objCurrentRmsApi->getProductPrice( $_GET['id'] );
$rates = $objCurrentRmsApi->getRates( $price->rate_definition_id );
$labels = array('1_day'=>'Daily', 'weekend'=>'Weekend', 'week'=>'Weekly', 'subs_day'=>'Additional Days');
$html = render_breadcrumb(array('query=products&name='.urlencode($product->product_group->name)=>$product->product_group->name,'query=product&id='.$_GET['id']=>$product->name));
$html .= '<div class="row">';
$html .= '<div class="col-md-6 item-photo">';
if (isset ($product->icon->url) ) {
$html .= '<img style="max-width:100%;" src="' . $product->icon->url . '" />';
}
$html .= '</div>';
$html .= '<div class="col-md-6" style="border:0px solid gray">';
$html .= '<h1>' . $product->name . '</h1>';
$html .= '<p>' . $product->description . '</p>';
$html .= 'Supplied with the following accessories:';
$html .= '<ul class="list-group list-group-flush mb-3">';
foreach( $accessories as $accessory )
{
$html .= '<li class="list-group-item border-0">' . $accessory->related_name . '</li>';
}
$html .= '</ul>';
$html .= '<table class="table">';
$html .= '<thead class="thead-light">';
$html .= '<tr>';
$html .= '<th scope="col">Hire Period</th>';
$html .= '<th scope="col">Cost</th>';
$html .= '</tr>';
$html .= '</thead>';
$html .= '<tbody>';
foreach ( $rates->rate_engine->rate_engine->config->charging_periods as $code=>$rate) {
$html .= '<tr><th>' . $labels[$code] . '</th><td>£' . number_format($price->price * $rate->default_value / 100,2) .'</td></tr>';
}
$html .= '</tbody>';
$html .= '</table>';
//$html .= '<!-- Hire and Sale Buttons -->';
//$html .= '<div class="section" style="padding-bottom:20px;">';
//$html .= '<button class="btn btn-success">Agregar al carro</button>';
//$html .= '<button class="btn btn-success">Agregar al carro</button>';
//$html .= '</div>';
$html .= '</div>';
echo $html;
break;
default:
echo 'Invalid query';
}
?>