【问题标题】:Get the item just added to cart获取刚刚添加到购物车的商品
【发布时间】:2019-06-11 21:37:29
【问题描述】:

当我商店中的用户将商品添加到他的购物车时,他会被重定向到购物车页面,其中会显示一条绿色消息,显示刚刚添加了哪些商品(ajax 已禁用)。

我想根据添加的产品显示不同的信息,但我不知道如何获取产品的 id。

不能只使用购物车中的最后一项,因为这将始终显示自定义消息,即使不是刚刚添加产品。

不能使用 wc_add_to_cart_message 过滤器,因为我需要在消息上方显示信息。

这是怎么做到的?

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    我自己找到了一个快速而肮脏的解决方案:

    function wc_add_to_cart_message_filter($message, $product_id = null) {
        $message = 'Extra field.</div><div class="woocommerce-message" role="alert">' . $message;
        return $message;
    }
    add_filter ( 'wc_add_to_cart_message', 'wc_add_to_cart_message_filter', 10, 2 );
    

    我只是拆分了 div 元素并在上面创建了另一个元素,我可以在其中访问产品 ID。

    然后通过 CSS 和 :first-child 选择器完成其余的工作...

    【讨论】:

      猜你喜欢
      • 2020-01-14
      • 2016-12-19
      • 2015-07-26
      • 2015-03-31
      • 2018-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多