OPENCART 3.0:获取' product_id'最后添加到购物车
我正在与OpenCart 3.0合作,在添加商品时向用户展示类似的项目,以设置产品交叉销售。我以为我已经在购物车中看到了一系列产品的图案,将最后一件的购物车放到最后。因此,我的php看起来像这样:
$this->load->model('catalog/product');
$related_product_id = $products;
$related_product_id = end($related_product_id);
$related_product_id = $related_product_id['product_id'];
$related = $this->model_catalog_product->getProductRelated($related_product_id);
foreach ($related as $related) {
//..do something
}
在上面代码中使用end()
方法将采用最后一个key => value
(product_id),所以我可以使用它在页面上显示产品。
这工作了一段时间,直到我注意到它并不总是准确的。如果有人在购物车中添加了一些东西,它不会将其放置在数组末尾,而由于某些原因,其他某些产品也不会在数组结束时出现。
是否有另一种方法可以比我正在使用的方式获得product_id
?我的方式似乎并不一致。谢谢!
I'm working with Opencart 3.0 to set up a product cross-sell to display similar items to the user when they add something to the cart. I thought I had seen a pattern with the array of products in the cart that put the last item of cart at the end. So my php looked something like this:
$this->load->model('catalog/product');
$related_product_id = $products;
$related_product_id = end($related_product_id);
$related_product_id = $related_product_id['product_id'];
$related = $this->model_catalog_product->getProductRelated($related_product_id);
foreach ($related as $related) {
//..do something
}
Using the end()
method in the above code would take the very last Key=>Value
(Product_id) in the array so I could use it to display the product on the page.
This worked for a while until I noticed that it was not always accurate. If someone added something already in the cart, it wouldn't place it at the end of the array, and also other certain products for some reason didn't show up at the end of the array.
Is there another way to get the Product_Id
of the last item a user added to the cart than the way I'm going about it? My way doesn't seem very consistent. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够弄清楚它将自己的功能添加到
/system/libray/cart/cart.php
我添加了此功能
,而不是使用
$ this-> cart-> getProducts()
行从购物车中获取产品,我正在使用我的新功能$ this-> cart-&gtproductuctsincart()
。这可以更加一致。它唯一没有考虑到有人回到已经添加的产品时。它不将其视为添加的新产品,而只是将其数量更新。
让我知道是否有人找到了更好的解决方案。
I was able to figured it out adding my own function to
/system/libray/cart/cart.php
I added this function
And instead of using the
$this->cart->getProducts()
line to get products from the cart, I'm using my new function$this->cart->getProductsInCart()
.This works way more consistent. The only thing it doesn't take into consideration is when someone goes back to a product already added. It doesn't count it as a new product added, but instead just updates it quantity.
Let me know if someone finds a better solution.