在 Prestashop 购物车中获取条件为 'new' 的产品

Get products with condition 'new' in Prestashop cart

我这样得到购物车中的产品:

$cart_products = $this->context->cart->getProducts();

我想过滤它们以便只显示条件为 'new' 的产品,所以我这样做:

foreach ($cart_products as $cart_product) {
          if ($cart_product->condition == 'new')
                .....
         }

没用。页面被阻止,我无法点击其中的任何部分,也无法向下或向上滚动。

有人知道怎么解决吗?

condition 字段不包含在 Cart getProducts getProducts 方法检索到的数组中 class。

在使用 condition 字段之前,您应该编辑或覆盖 Cart class 中的 getProducts 方法,并在 "p.`condition`" SQL 查询,像这样:

// Build SELECT
$sql->select('cp.`id_product_attribute`, cp.`id_product`, cp.`quantity` AS cart_quantity, cp.id_shop, pl.`name`, p.`is_virtual`,
                    pl.`description_short`, pl.`available_now`, pl.`available_later`, p.`condition`, product_shop.`id_category_default`, p.`id_supplier`,
                    p.`id_manufacturer`, product_shop.`on_sale`, product_shop.`ecotax`, product_shop.`additional_shipping_cost`,
                    product_shop.`available_for_order`, product_shop.`price`, product_shop.`active`, product_shop.`unity`, product_shop.`unit_price_ratio`,
                    stock.`quantity` AS quantity_available, p.`width`, p.`height`, p.`depth`, stock.`out_of_stock`, p.`weight`,
                    p.`date_add`, p.`date_upd`, IFNULL(stock.quantity, 0) as quantity, pl.`link_rewrite`, cl.`link_rewrite` AS category,
                    CONCAT(LPAD(cp.`id_product`, 10, 0), LPAD(IFNULL(cp.`id_product_attribute`, 0), 10, 0), IFNULL(cp.`id_address_delivery`, 0)) AS unique_id, cp.id_address_delivery,
                    product_shop.advanced_stock_management, ps.product_supplier_reference supplier_reference');

然后如@sadlyblue 所说,您可以使用 condition 字段并仅显示 new 产品。