我创建了一个网站,其中包含使用 woocommerce 的汽车服务订单处理流程。我创建了名为 Car Brand、Car model 和 Car model mapping 的 CPT。并使用 ACF 映射它们。示例:汽车品牌:Maruti、Toyota、MG 等。汽车型号:800、Etios 和 Hector。所以 800 属于 Maruti 品牌等等。
现在我创建了一个 PHP 表单,其中包含汽车品牌、型号和车辆类型等字段来预订服务。
现在,每当我选择汽车品牌时,模型字段都无法从汽车模型帖子类型中获取任何帖子,因此它不显示任何数据。相反,我想显示带有所选汽车品牌的映射模型。前任。如果我选择马鲁蒂,模型字段将只提供与马鲁蒂品牌相关的模型。
我正在提供代码。请帮我解决这个问题。
archive.php(表单存在的地方)
<?php
/**
* The template for displaying archive pages
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/
*
* @package WordPress
* @subpackage Twenty_Seventeen
* @since 1.0
* @version 1.0
*/
WC()->cart->empty_cart();
//Clearing Previous Cart Items
if (intval($_POST['varient_id'])) {
session_start();
$_SESSION['varient_id'] = $_POST['varient_id'];
$_SESSION['car_varient'] = $_POST['car_varient'];
$_SESSION['car_maker'] = $_POST['car_maker'];
$_SESSION['car_model'] = $_POST['car_model'];
//die('posted');
}
get_header();
?>
<div id="main-content" class="site-main clearfix">
<div id="content-wrap">
<div id="site-content" class="site-content clearfix">
<div id="inner-content" class="inner-content-wrap">
<div class="page-content">
<?php
$service_category = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
$service_category_checkout = get_field('direct_checkout', $service_category->taxonomy . '_' . $service_category->term_id);
$enquiry_form = get_field('enquiry_form', $service_category->taxonomy . '_' . $service_category->term_id);
if ($service_category_checkout) {
$direct_checkout = 1;
} else {
$direct_checkout = 0;
}
global $woocommerce;
$cart_url = $woocommerce->cart->get_cart_url();
?>
<input type="hidden" autocomplete="off" id="direct_checkout" value="<?php echo $direct_checkout; ?>">
<input type="hidden" autocomplete="off" id="direct_checkout_link" value="<?php echo $cart_url; ?>">
<div class="row-services">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="wprt-spacer clearfix" data-desktop="83" data-mobi="60" data-smobi="60"></div>
<?php if (have_posts()) : ?>
<div class="wprt-headings style-1 clearfix text-center">
<?php
//the_archive_title( '<h2 class="heading clearfix">', '</h2>' );
//the_archive_description( '<div class="taxonomy-description">', '</div>' );
global $woocommerce;
$term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
if (intval($_POST['varient_id'])) {
session_start();
$_SESSION['car_maker'] = $_POST['car_maker'];
$_SESSION['car_model'] = $_POST['car_model'];
?>
<h2 class="heading clearfix"><?php echo $term->name; ?></h2>
<h3>available for your <?php echo get_the_title($_SESSION['car_maker']) . ' ' . get_the_title($_SESSION['car_model']) . ' ' . $_SESSION['car_varient']; ?> </h3>
<?php
//die('posted');
}
?>
</div><!-- .page-header -->
<?php endif; ?>
</div>
<?php
if (!$enquiry_form) :
if (have_posts()) :
?>
<?php
/* Start the Loop */
while (have_posts()) :
the_post();
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
//get_template_part( 'template-parts/post/content', get_post_format() );
$service_icon = get_field('service_icon');
?>
<div class="col-md-4 service-list-item">
<div class="wprt-icon-box style-1 shamba_box2 clearfix icon-top outline-type grey-outline align-center rounded-100 has-width w100">
<div class="icon-wrap">
<?php if ($service_icon) : ?>
<img src="<?php echo $service_icon; ?>" alt="icon" class="service-icon">
<?php else : ?>
<i class="as-icon-car-wash"></i>
<?php endif ?>
</div>
<h3 class="heading">
<a target="_blank" href="#"><?php the_title(); ?></a>
</h3>
<div class="service-content-wrapper">
<?php the_content(); ?>
</div>
<?php if ($_POST['varient_id']) :
$varient_id = $_POST['varient_id'];
$varient_price = get_varient_price(get_the_ID(), $varient_id);
//var_dump($varient_price);
if ($varient_price) {
$price = $varient_price->price;
$duration = $varient_price->duration;
} else {
$price = 0;
$duration = 0;
}
$get_an_estimate_form = get_field('get_an_estimate_form');
if ($get_an_estimate_form) {
?>
<div class="elm-btn">
<?php
//$add_to_cart_url=get_the_permalink().'?add-to-cart='.get_the_ID();
?>
<a href="#" class="wprt-button accent big btn_add_to_cart" data-price="0" data-duration="<?php echo $duration; ?>" data-service_id="<?php echo get_the_ID(); ?>">GET AN ESTIMATE</a>
</div>
<?php
} else {
?>
<div class="elm-btn">
<div class="price">
<h3>Price:<span class="amount-span"><?php echo $price; ?>/-</span></h3>
</div>
<div class="duration">
<h3>Duration:<span class="duration-span"><?php echo $duration; ?> Hrs.</span></h3>
</div>
<?php
//$add_to_cart_url=get_the_permalink().'?add-to-cart='.get_the_ID();
?>
<a href="#<?php //echo $add_to_cart_url;
?>" class="wprt-button accent big btn_add_to_cart" data-price="<?php echo $price; ?>" data-duration="<?php echo $duration; ?>" data-service_id="<?php echo get_the_ID(); ?>">BOOK NOW</a>
<? php // echo do_shortcode('[add_to_cart id=”'.get_the_ID().'″]');
?>
</div>
<?php } ?>
<?php endif; ?>
</div><!-- /.wprt-icon-box -->
<div class="wprt-spacer clearfix" data-desktop="0" data-mobi="35" data-smobi="35"></div>
</div><!-- /.col-md-3 -->
<?php
endwhile;
wp_reset_postdata();
else :
echo '<div class="col-md-12">';
echo do_shortcode('[contact-form-7 id="1753" title="Service Enquiry Form"]');
echo '</div>';
endif;
else :
echo '<div class="col-md-12">';
echo do_shortcode('[contact-form-7 id="1753" title="Service Enquiry Form"]');
echo '</div>';
endif;
?>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php if (!$_POST['varient_id']) :
?>
<div id="booking_modal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Booking</h4>
</div>
<div class="modal-body">
<p>Please Enter Your Car Details</p>
<?php
$term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
$current_category_link = get_term_link($term);
?>
<form action="<?php echo $current_category_link; ?>" method="post" class="wpcf7-form" novalidate="novalidate">
<input style="display: none;" type="text" name="varient_id" id="varient_id" autocomplete="off" value="">
<div class="contact-page-form">
<p>
<span class="wpcf7-form-control-wrap your-name">
<select name="car_maker" id="car_maker">
<option value="">Make</option>
<?php
$args = array('post_type' => 'carbrands', 'post_status' => 'publish', 'posts_per_page' => -1);
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post();
?>
<option value="<?php echo get_the_ID() ?>"><?php the_title(); ?></option>
<?php
endwhile;
endif;
?>
</select>
</span>
</p>
<p>
<span class="wpcf7-form-control-wrap your-name">
<select name="car_model" id="car_model">
<option value="">Model</option>
</select>
</span>
</p>
<p>
<span class="wpcf7-form-control-wrap your-name">
<select name="car_varient">
<option value="Petrol">Petrol</option>
<option value="Diesel">Diesel</option>
<option value="CNG">CNG</option>
</select>
</span>
</p>
</div>
<input style="display: none;" id="form_btn_book_service" type="submit" value="Book Now" class="wpcf7-form-control wpcf7-submit">
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php
else :
session_start();
$_SESSION['varient_id'] = $_POST['varient_id'];
$_SESSION['car_varient'] = $_POST['car_varient'];
$_SESSION['car_maker'] = $_POST['car_maker'];
$_SESSION['car_model'] = $_POST['car_model'];
?>
<?php
endif; ?>
<?php
get_footer();
函数.php
add_action("wp_ajax_get_car_models", "get_car_models");
add_action("wp_ajax_nopriv_get_car_models", "get_car_models");
function get_car_models()
{
$car_maker_id = $_POST['car_maker'];
$args = array(
'post_type' => 'carmodelmapping',
'post_status' => 'publish',
'posts_per_page' => -1
);
$query = new WP_Query($args);
?>
<option value="">Model</option>
<?php
if ($query->have_posts()) :
while ($query->have_posts()) :
$query->the_post();
$car_brand = get_field('car_brand');
if ($car_brand->ID == $car_maker_id) {
$car_model = get_field('car_model');
?>
<option value="<?php echo $car_model->ID; ?>"><?php echo $car_model->post_title; ?>
</option>
<?php
}
endwhile;
endif;
exit();
}
main.js
$(document).on('change', '#car_maker', function () {
var car_maker_id = $(this).val();
if (car_maker_id == '') {
return false;
}
var dataString = 'action=get_car_models&car_maker_id=' + car_maker_id;
$.ajax({
url: ajax.url,
type: 'POST',
data: dataString,
success: function (data) {
// alert(data);
$("#car_model").html(data);
},
error: function () {
alert('Timeout Error!');
}
});
});