【问题标题】:Add a custom note into add_order_note() function in Woocommerce在 Woocommerce 的 add_order_note() 函数中添加自定义注释
【发布时间】:2018-10-24 09:50:45
【问题描述】:

通过在 Woocommerce 中使用 add add_order_note() 可以添加自定义注释。但就我而言,一旦我从 javascript 中获取值并将其传递给 PHP 变量,预期的输出就不是我想要的。

请看下面的代码:

<script>
        var x=document.getElementById('pickupStore').value;//x=my store

    </script>
    <?

    $store= "<script>document.write(x)</script>";
    echo $store;//it display "my store"
     $order = new WC_Order( $order_id ); 

     // The text for the note
    $note = __($store);

     // Add the note
     $order->add_order_note($note);

     // Save the data
     $order->save();

我运行上面的代码并检查 Woocommerce 管理页面中的 order_notes 部分,它显示"document.write(x)",而不显示“我的商店”。 是add_order_note()函数无法运行脚本吗?

感谢任何帮助。

【问题讨论】:

  • 这不行……你需要使用Ajax,这是唯一的方法,因为php在你的javascript之前执行。
  • @LoicTheAztec 我尝试了 ajax,但无法正常工作。然后我改用cookie,它起作用了。无论如何,谢谢你的回复。
  • 哦,是的,cookie 也是一个不错的选择……您应该发布一个答案,因为它可能对社区有用。

标签: wordpress woocommerce


【解决方案1】:

已解决。 我用cookie来解决这个问题。请参阅下面的代码。

<script>
    var x = "my store";
    document.cookie = "Store = " + x;
</script>

<?php
    $order = new WC_Order( $order_id ); 
    $note = __( $_COOKIE["Store"] );
    $order->add_order_note($note);
    $order->save();
?>

【讨论】:

    猜你喜欢
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多