这里的第一个问题!
我有一个自定义的wordpress网站,其中我有包括折扣/优惠券代码的结账功能。
我已经做了以下操作来测试代码:-我在数据库中设置了三个优惠券代码-所有三个优惠券代码都在数据库中,具有正确的信息
当我尝试输入优惠券时,代码仅应用上次输入优惠券的优惠券规则。相反,它应该通过提取与$couponcode
的用户输入相匹配的优惠券代码
我知道我分配$couponID
的方式有问题,但我不知道如何正确执行。
注意:我知道:cooupon_title
的拼写错误。然而,它是以这种方式在数据库中设置的,所以这不是错误。
<?php
$couponCode = '';
$couponMessage = '';
$discountAmount = 0;
$couponId = 0;
if(isset($_POST['couponCode']) && !empty($_POST['couponCode'])) {
$couponCode = $_POST['couponCode']; }
if(!empty($couponCode)) {
$args = array('posts_per_page' => 1,'offset' => 0,'category' => '', 'category_name' => '', 'orderby' => 'date','order' => 'DESC','include' => '', 'exclude' => '','meta_key' => '','meta_value' => '','post_type' => 'coupons','post_mime_type' => '','title' => $couponCode,'post_status' => 'publish','suppress_filters' => 'true');
$couponDbData = get_posts($args);
$couponResultData = '';
if(isset($couponDbData[0]) && !empty($couponDbData[0])) {
$couponResultData = $couponDbData[0];
} else {
$couponMessage = 'Invalid coupon code';
}
if(isset($couponResultData->ID) && !empty($couponResultData->ID))
$couponId = $couponResultData->ID;
if(!empty($couponId)) {
$expiry_condition = get_post_meta($couponId,'expiry_condition',true);
$expiry_value = get_post_meta($couponId,'expiry_value',true);
$no_of_use = get_post_meta($couponId,'no_of_use',true);
$appyCoupon = false;
if($expiry_condition=='use') {
if($expiry_value > $no_of_use) {
$appyCoupon = true;
} else {
$couponMessage = 'Coupon has expired.';
}
} else {
$couponExpireTime = strtotime($expiry_value);
$currentTime = time();
if($couponExpireTime > $currentTime) {
$appyCoupon = true;
} else {
$couponMessage = 'Coupon has expired.';
}
}
if($appyCoupon) {
$cooupon_title = get_post_meta($couponId,'cooupon_title',true);
$coupon_type = get_post_meta($couponId,'coupon_type',true);
$coupon_value = get_post_meta($couponId,'coupon_value',true);
if($coupon_type=='$') {
$discountAmount = $coupon_value;
} else {
$discountAmount = ($orderAmount*$coupon_value)/100;
}
$grandTotal -= $discountAmount;
}
}
}
这是我的测试输出:
if($appyCoupon) { ?>
<div class="col-sm-12 col-md-12 makeOrderDetailsrow">
<label class="col-sm-4 col-md-4">Discount <?php echo '( Apply coupon '.$couponCode.' from the following sources '.$couponId.' '.$cooupon_title.' '.$coupon_value.' '.$coupon_type.')'; ?></label>
<span class="col-sm-8 col-md-8">- $<?php echo (number_format($discountAmount,2)); ?></span>
</div>
"来自以下来源"之后的输出显示代码正在提取DB中的最后一个条目,而不是与$couponCode
匹配的条目。有什么建议吗?感谢
根据codex,get_posts的args不包括"title",https://codex.wordpress.org/Template_Tags/get_posts.
Wordpress具有按标题get_page_by_title( $page_title, $output, $post_type );
查询帖子的功能http://codex.wordpress.org/Function_Reference/get_page_by_title
顺便说一句,您不必在get_posts查询中包括$args的整个列表,只需将那些您想从默认值更改的$args包括在内。例如,
$args = array(
'post_type' => 'coupon',
);
将覆盖'post_type' => 'post'
的默认值,而不更改'posts_per_page' => 5
。