讯睿CMS问答:怎么判断用户已经购买商品

讯睿CMS问答:怎么判断用户已经购买商品

2025-01-07 XunRuiCMS Sincere

问题:

PHP程序里面

大佬们帮忙看看怎么判断用户已经购买商品???

代码是参考积分购物这个插件,把buy.php直接挪到内容模块下面。

然后在内容模块新建price作为支付字段,复制了一份积分购物的购买记录表重命名作为记录商品购买的记录表

现在想要在内容模块的内容页面获取用户是否支付然后返回不同的内容,大佬帮忙看看该如何写下判断?

{if ?????????????????????????????}
购买展示的内容
{else}
请购买后再查看
{/if}


数据表




buy.php代码

_module_init(APP_DIR); $id = (int)\Phpcmf\Service::L('Input')->get('id'); (!$id) && exit($this->_msg(0, dr_lang('商品ID不能为空'))); !$this->uid && exit($this->_msg(0, '你还没有登录')); $data = $this->content_model->get_row( $id); !$data && $this->_msg(0, dr_lang('内容【id#%s】不存在', $id)); !isset($data['price']) && $this->_msg(0, dr_lang('此模块没有找到price字段')); $data['price_sku'] = dr_string2array($data['price_sku']); if ($data['price_sku']) { $price = []; foreach ($data['price_sku'] as $gid => $v) { if (in_array($gid, $this->member['groupid'])) { $price[] = $v; } } !$price && $this->_msg(0, dr_lang('当前用户组无权限购买')); $score = intval(min($price)); } else { $score = intval($data['price']); } if ($score > 0) { // 扣积分 if ((float)$this->member['score'] <= 0 ) { $this->_msg(0, dr_lang('账户余额不足')); } elseif ($this->member['score'] - $score < 0) { $this->_msg(0, dr_lang('账户可用余额不足')); } // 扣款 \Phpcmf\Service::M('member')->add_score($this->uid, -$score, '积分购物消费<'.$data['title'].'>'); // 打款到作者 \Phpcmf\Service::M('member')->add_score($data['uid'], $score, '积分购物收入<'.$data['title'].'>'); // 记录到日志表 \Phpcmf\Service::M()->table(SITE_ID.'_'.APP_DIR.'_score')->insert([ 'cid' => $id, 'uid' => $this->uid, 'sellid' => $data['uid'], 'price' => $score, 'inputtime' => SYS_TIME, ]); $this->_msg(1, dr_lang('支付成功'), dr_url_prefix($data['url'], APP_DIR, SITE_ID)); } else { $this->_msg(0, dr_lang('当前商品为免费商品,不需要购买')); } } }


答案:

你的代码是入库了一张表,判断这张表是否有记录就可以实现吧?、

{if $member.uid && \Phpcmf\Service::M()->table(SITE_ID.'_'.APP_DIR.'_score')->where('uid', $member.uid)->where('cid', $id)->counts()}

试试这个代码

回复@疾风 非常好的代码 你真的是太帅了!

@疾风:非常感谢!!!!!!!