我正在开发一个停留在这个地方的电子商务网站我有这个错误的SOF帖子,但对我帮助不大。目的是根据输入的优惠券代码向客户提供折扣。任何人都可以帮助我解决这个问题。
<?php
/**
* Consider and apply voucher codes
* Types of voucher code
* - = FIXED AMOUNT OFF
* % = PERCENTAGE OFF
* s = SET NEW SHIPPING COST
* @param voucherCode String
* @return void
*/
if(isset($_POST["submit"])){
$code=$_POST["code"];
$coupon=new coupon();
$coupon->Vouchers($code);
}
class coupon {
function Vouchers( $voucherCode )
{
//The voucher code value is checked to ensure it is not empty
// (as per point 1)
if( $voucherCode != '' )
{
// we need the date, to see if the voucher has expired
$cts = date('Y-m-d H:i:s');
$voucherCode=$this->registry;
$voucher_sql = "SELECT *, if('{$cts}' > expiry, 1, 0)
AS expired FROM code
WHERE code='{$voucherCode}' LIMIT 1";
$voucher_sql=$this->registry;
$this->registry->getObject('code')->executeQuery( $voucher_sql );
if( $this->registry->getObject('code')->numRows() == 0 )
{
$this->voucher_notice = 'Sorry, the voucher code you entered
is invalid';
}
else
{
$voucher = $this->registry->getObject('code')->getRows();
if( $voucher['active'] == 1 )
{
if( $voucher['expired'] == 1 )
{
$this->voucher_notice = 'Sorry, this voucher has
expired';
return false;
}
else
{
// check to see there are some vouchers, and customer
// has enough in their basket (points 3 and 4)
if( $voucher['num_vouchers'] != 0 )
{
if( $this->cost >= $voucher['min_cost'] )
{
$this->discountCode = $voucherCode;
$this->discountCodeId = $voucher['id'];
// If the discount operation is a percentage, then
// the discount value is applied to the basket cost
// to calculate that percentage. This amount is then
// deducted from the order cost, giving a "discount
// value"% discount from the order.
if( $voucher['operation'] == '%' )
{
$this->cost = $this->
cost - (($this->cost)/100)*$voucher['amount'];
$this->voucher_notice = 'A '
. $voucher['amount']
. '% discount has been applied to your order';
return true;
// If the discount operation is a subtraction, then
// the discount amount from the discount code is
// deducted from the order cost, and the order cost
// is updated to reflect this.
}
elseif( $voucher['operation'] == '-' )
{
$this->cost =
$this->cost - $voucher['amount'];
$this->voucher_notice = 'A discount of £'
. $voucher['amount']
. ' has been applied to your order';
return true;
// Finally, if the discount operation is set to s
// then, we set the shipping cost to the discount
// value. This could allow us to set free shipping,
// or just reduce shipping costs.
}
elseif( $voucher['operation'] == 's' )
{
$this->shipping_cost = $voucher['amount'];
$this->voucher_notice = 'Your orders shipping cost
has been reduced to £'
. $voucher['amount'];
return true;
}
}
else
{
$this->voucher_notice = 'Sorry, your order total is
not enough for your order to qualify for this
discount code';
return false;
}
}
else
{
$this->voucher_notice = 'Sorry, this was a limited
edition voucher code, there are no more instances
of that code left';
return false;
}
}
}
else
{
$this->voucher_notice = 'Sorry, the vocuher code you
entered is no longer active';
return false;
}
}
}
}
}
?>
现在运行此代码我得到了 致命错误:在
中的非对象上调用成员函数getObject()这是我的html表单
<form method="post" action="a.php">
Enter The Coupon Code:<br />
<input name="code[]" type="text" size="10" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
答案 0 :(得分:0)
看起来错误是
$this->registry->getObject
现在没有任何地方显示注册表被注入,这就是问题所在。我假设你正在使用一个框架,并期望这个类是容器感知的,但事实并非如此。因此,注入注册表,或在构造函数中实例化它。