【问题标题】:Add a custom text to items in Woocommerce order received (thank you) page将自定义文本添加到 Woocommerce 订单收到(谢谢)页面中的项目
【发布时间】:2018-01-06 12:09:48
【问题描述】:

我有自定义批量折扣功能,我调用了两个钩子:

  • 第一个编辑购物车中商品价格的人:woocommerce_cart_item_price,
  • 在总计之前计算价格的第二个:woocommerce_before_calculate_totals

现在,我想在收到的订单页面中为每件商品添加折扣文本“(10% 折扣)”

有没有什么钩子可以帮助我在收到订单页面之前实现与价格类似的事情?

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce orders


    【解决方案1】:

    您可以在{your theme}\woocommerce\order\order-details-item.php 文件中进行更改。如果你还没有复制到你的主题/woocommerce 目录,copy it

    您还可以使用钩子woocommerce_order_item_meta_startwoocommerce_order_item_meta_end,您可以在上述文件中找到它们。

    【讨论】:

    • 自定义模板是这里的理想解决方案。但是,我会在 if( is_wc_endpoint_url( 'order-received' ) ) { } 块中添加自定义,因此我的更改仅显示在 thankyou 页面上,否则 order-details-item.php 中的更改也会显示 view-orderorder-tracking 页面中的更改。
    • 感谢所有提示。我将以这种方式工作,我还设法使用 woocommerce_order_item_meta_start 实现了批量折扣功能。我有一些问题如何在商品价格旁边显示折扣文本。是否可以格式化价格或以某种方式在商品价格旁边添加 html?
    • @JanGale,不客气!你当然可以。最好将其添加到提到的 (order-details-item.php) 模板文件中。只需在包含在您想要的 HTML 中的 <?php echo $order->get_formatted_line_subtotal( $item ); ?> 行之后添加文本。它会显示在商品价格旁边。
    【解决方案2】:

    您可以使用 woocommerce_order_item_name 过滤器挂钩中的自定义函数将自定义文本“(10%折扣)”添加到每个项目标题:

    add_filter( 'woocommerce_order_item_name', 'custom_orders_items_names', 10, 2 );
    function custom_orders_items_names( $item_name, $item ) {
        // Only in thankyou "Order-received" page
        if(is_wc_endpoint_url( 'order-received' ))
            $item_name .= ' ' . __('(10% discount)', 'woocommerce');
        return $item_name;
    }
    

    代码进入您的活动子主题(或活动主题)的任何 php 文件中

    经过测试并且有效。

    【讨论】:

    • 虽然这个解决方案可以工作,但它不能正确控制文本的位置和格式。这完全可以通过自定义 order-details-item.php 模板来实现,如 Awag 的回答所述。使用模板还可以使其可维护,因为将来很容易找到所做的更改。
    猜你喜欢
    • 2017-01-21
    • 2021-06-12
    • 2021-10-16
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-18
    • 2019-01-04
    相关资源
    最近更新 更多