OPENCART 3.0:获取' product_id'最后添加到购物车

发布于 2025-01-22 21:55:55 字数 728 浏览 4 评论 0原文

我正在与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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人生百味 2025-01-29 21:55:55

我能够弄清楚它将自己的功能添加到/system/libray/cart/cart.php

我添加了此功能

public function getProductsInCart() {
        $product_data = array();

        $cart_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "cart WHERE api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' AND customer_id = '" . (int)$this->customer->getId() . "' AND session_id = '" . $this->db->escape($this->session->getId()) . "' ORDER BY date_added ASC");

        foreach ($cart_query->rows as $cart) {
                
                $product_data[] = array(
                    'cart_id'         => $cart['cart_id'],
                    'product_id'      => $cart['product_id'],
                    'date_added'      => $cart['date_added'],
                );
        
        }

        return $product_data;
    }

,而不是使用$ 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

public function getProductsInCart() {
        $product_data = array();

        $cart_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "cart WHERE api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' AND customer_id = '" . (int)$this->customer->getId() . "' AND session_id = '" . $this->db->escape($this->session->getId()) . "' ORDER BY date_added ASC");

        foreach ($cart_query->rows as $cart) {
                
                $product_data[] = array(
                    'cart_id'         => $cart['cart_id'],
                    'product_id'      => $cart['product_id'],
                    'date_added'      => $cart['date_added'],
                );
        
        }

        return $product_data;
    }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文