【问题标题】:Output related products on Woocommerce Cart Page在 Woocommerce 购物车页面上输出相关产品
【发布时间】:2014-04-20 00:31:23
【问题描述】:

我希望在我的 woocommerce 购物车页面上输出相关产品。

woocommerce_related_products()功能在查看单个产品时完美运行。

但是在 shopping-cart.php 上,使用这个函数会返回错误:

Fatal error: Call to a member function get_related() on a non-object in /woocommerce/single-product/related.php

我尝试在产品循环中包含该函数:

foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

    $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
    $product_id   = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );

    if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters('woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
        woocommerce_related_products();
    }
}

这产生了同样的错误。

如果当前有几种产品存在问题,是否可以这样做?我很乐意从购物车中随机挑选一个产品,并据此输出建议。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    问题在于 woocommerce_related_products() 应该在循环中使用(这是 WordPress 特定的术语)。该函数包括 /woocommerce/single-product/related.php 模板,WooCommerce 在该模板中尝试访问全局变量 $product$woocommerce_loop,这些变量未在您尝试执行函数的位置定义。

    我建议您打开文件/woocommerce/single-product/related.php,查看如何检索相关产品并编写更多自定义代码以使相关产品在循环外显示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      • 1970-01-01
      • 2018-08-15
      • 2020-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多