如何在模板内复制简编码

发布于 2025-02-04 01:03:50 字数 902 浏览 3 评论 0原文

在wordpress function.php文件中,我有一个短代码,可以在我在view-order.php页面上时显示订单ID。短代码工作正常。

我要做的是找到一个更舒适的选择。因此,我想利用Twig模板,因此我正在进行一些测试。我实际上正在查看订单ID,但是对于我决定查看的每个订单,它都保持不变。

示例:

  • 如果我查看顺序001,则显示ID 001(正确),
  • 显示的ID 001

如果我查看了002订单,则显示(不正确)与快速代码一起 ,我可以正确显示每个顺序的ID。谁能告诉我我在哪里做错?

我已经在My-orders页面和MyDownloads页面上完成了同样的事情,它效果很好,但是由于某种原因,它不使用view-order.php页面。

感谢任何回复,谢谢。

add_shortcode( 'view_order_id' , 'view_order_00' );
function view_order_00(){
 $order_id = absint( get_query_var('view-order') );
 $order = new WC_Order( $order_id );
 return $order->get_id();
}
{% set post = system.get.order_id|php('get_post') %}
{% set order = post.ID|php('wc_get_order') %}
{% for item in order.get_items %}
{% for download in order.get_downloadable_items %}

<div>{{post.ID}}</div>
  
{%endfor%}
{% endfor %}

In wordpress functions.php file I have a shortcode that allows me to display the order ID when I'm on the view-order.php page. The shortcode works fine.

What I am trying to do is find a more comfortable alternative to this. So I would like to take advantage of the Twig template, so I'm doing some testing. I'm actually viewing the order ID, but it stays the same for every order I decide to view.

Example:

  • if I view the order 001, the ID 001 is shown (correct)
  • if I view the order 002, the ID 001 is shown (incorrect)

With the shortcode this does not happen, I can correctly display the ID of each order. Can anyone tell me where I'm doing wrong?

I've already done the same thing with my-orders page and my-downloads page, it worked fine, but for some reason it's not working with view-order.php page.

I appreciate any response, thanks.

add_shortcode( 'view_order_id' , 'view_order_00' );
function view_order_00(){
 $order_id = absint( get_query_var('view-order') );
 $order = new WC_Order( $order_id );
 return $order->get_id();
}
{% set post = system.get.order_id|php('get_post') %}
{% set order = post.ID|php('wc_get_order') %}
{% for item in order.get_items %}
{% for download in order.get_downloadable_items %}

<div>{{post.ID}}</div>
  
{%endfor%}
{% endfor %}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

卸妝后依然美 2025-02-11 01:03:50

我正在使用一个名为e-addons的第三方插件,这为Elementor提供了许多小部件,包括Twig模板。

我联系了插件支持,他们告诉我,我必须将订单ID传递给$ _get以解决问题。此外,所有这些都必须在专用的Elementor页面中完成,而不是在我最初进行的vief-order.php模板中完成。

因此,这是我逐步进行的解决问题的简要说明。我希望这对与我遇到困难的任何人都会有用。

1 。我创建了一个带有Elementor标题为“测试”页面的新页面。

2 。我复制了新创建的页面(测试页面)的永久链接,并将其放入functions.php文件中的过滤器中,此处在下面。

// When you click on view order, this filter allows you to redirect from view-order (original woocommerce endpoint) to a custom page created with elementor.
function filter_woocommerce_get_endpoint_url( $url, $endpoint, $value, $permalink ) {
    
    // Specific endpoint: If in the advanced settings of woocommerce you have changed the endpoint view order, then below you have to replace view-order with the one you have chosen in the advanced settings wi WC
    if ( $endpoint === 'view-order' ) {
    
    // New URL: change test-page with permalink of the page you created earlier. The /?order_id= part should not be removed.
    $url = $permalink . 'test-page/?order_id=' . $value;
} 
return $url;
}
add_filter( 'woocommerce_get_endpoint_url', 'filter_woocommerce_get_endpoint_url', 10, 4 );

3 。使用永久链接设置过滤器后,我们需要使用Elementor修改新页面。进入编辑器后,我选择了e-addons帖子查询小部件并将其拖到页面区域。

步骤3在此处打开,请按照以下步骤正确配置小部件,仅更改下面提到的内容:

转到查询帖子的“查询”选项卡widget

query

  • in QUERY类型 选择post type type
  • 选项&gt;帖子类型选择顺序
  • 在Post Status 中选择任何

查询过滤器

  • in“ by” by

“ 如下所示,如下所示

  • 和修改
  • 选择用户字段

如果您在步骤3中正确执行所有操作,则用户将能够查看订单,否则,如果您做错了什么,您将一无所获。
在这里结束步骤3。

4 。然后转到标题为“内容”的小部件的第一个选项卡。在这里,您必须转到标题为“帖子”项目的菜单项,这是我使用Twig模板玩的地方。因此,我最初在问题中发布的代码现在有效。

{% set post = system.get.order_id|php('get_post') %}
{% set order = post.ID|php('wc_get_order') %}
{% for item in order.get_items %}
{% for download in order.get_downloadable_items %}

<div>{{post.ID}}</div>
  
{%endfor%}
{% endfor %}

现在,当我在订单历史记录页面上并单击“查看订单”时,我将正确重定向到特定的订单页面,例如mywebsite.com/account/test-page?order_ord_id = xxxxxx

I am using a third party plugin called e-addons, this offers many widgets for elementor, including Twig Template.

I contacted plugin support and they told me that I would have to pass the order ID in $ _GET to fix the problem. Furthermore, all this must be done in a dedicated elementor page and not in the view-order.php template as I was doing initially.

So here is a brief explanation of what I did step by step to fix the problem. I hope it will be useful to anyone who has had the same difficulties as me.

1. I created a new page with elementor titled test page. The

2. I copied the permalink of the newly created page (test-page) and put it in a filter in the functions.php file, here it is below.

// When you click on view order, this filter allows you to redirect from view-order (original woocommerce endpoint) to a custom page created with elementor.
function filter_woocommerce_get_endpoint_url( $url, $endpoint, $value, $permalink ) {
    
    // Specific endpoint: If in the advanced settings of woocommerce you have changed the endpoint view order, then below you have to replace view-order with the one you have chosen in the advanced settings wi WC
    if ( $endpoint === 'view-order' ) {
    
    // New URL: change test-page with permalink of the page you created earlier. The /?order_id= part should not be removed.
    $url = $permalink . 'test-page/?order_id=' . $value;
} 
return $url;
}
add_filter( 'woocommerce_get_endpoint_url', 'filter_woocommerce_get_endpoint_url', 10, 4 );

3. After setting the filter with the permalink we need to modify the new page with elementor. Once in the editor I selected the e-Addons Posts Query widget and dragged it to the page area.

Step 3 opens here, follow these steps to configure the widget correctly, only change what is mentioned below:

Go to the Query Tab of the query posts widget

Query

  • In query type select Post Type
  • In options> post type select order
  • in post status select Any

Query Filter

  • in "By" select Meta Key

in Mtakey Filters add new and modify as follows

  • in Post Field custom meta key select _customer_user
  • in Value Type select Numeric
  • in Compare operator select "="
  • in Post Field Value select User Field

If you do everything correctly in step 3, the user will be able to view the orders, otherwise if you do something wrong you get nothing.
Here ends step 3.

4. Then go to the first tab of the widget entitled Content. Here you have to go to the menu item titled Post Items, and this is where I played with the Twig template. So the code I originally posted in my question now works.

{% set post = system.get.order_id|php('get_post') %}
{% set order = post.ID|php('wc_get_order') %}
{% for item in order.get_items %}
{% for download in order.get_downloadable_items %}

<div>{{post.ID}}</div>
  
{%endfor%}
{% endfor %}

Now, when I am on the order history page and click on view order I am redirected correctly to the specific order page which for example is mywebsite.com/account/test-page?order_id=xxxxxx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文