簡體   English   中英

Opencart 3.0:獲取添加到購物車的最后一項的“Product_Id”

[英]Opencart 3.0: Get the 'Product_Id' of the Last Item Added To Cart

我正在使用 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 ,而不是我正在處理的方式? 我的方式似乎不太一致。 謝謝!

我能夠想出將自己的 function 添加到/system/libray/cart/cart.php

我加了這個 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;
    }

我沒有使用$this->cart->getProducts()行從購物車中獲取產品,而是使用我的新 function $this->cart->getProductsInCart()

這工作方式更加一致。 唯一沒有考慮的是有人返回到已添加的產品。 它不將其視為添加的新產品,而只是更新它的數量。

如果有人找到更好的解決方案,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM