jjzjj

php - Woocommerce:结帐订单评论字段中的订单摘要(PHP)

coder 2024-04-19 原文

我希望 woocommerce 订单的订单摘要在结账时显示在一个字段中。我可以在字段中放置纯文本,但是当我尝试添加 Hook 时它会抛出错误代码。这是将默认文本添加到字段的代码。位于 functions.php 中:

add_filter( 'woocommerce_checkout_fields' , 'default_values_checkout_fields' );

function default_values_checkout_fields( $fields ) {

$fields['order']['order_comments']['default'] = ' I would like the hook here ';

return $fields;
}

此代码在结账时输出一个表格:

<table class="shop_table woocommerce-checkout-review-order-table">

<tbody>
    <?php
        do_action( 'woocommerce_review_order_before_cart_contents' );

        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 );

            if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_checkout_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
                ?>
                <tr class="<?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); ?>">
                    <td class="product-name">
                        <?php echo apply_filters( 'woocommerce_cart_item_name', $_product->get_title(), $cart_item, $cart_item_key ) . '&nbsp;'; ?>
                                                                            </td>
                </tr>
                <?php
            }
        }

这是一个表格,但如果不是表格,我希望表格中的文本显示在一个字段中。

最佳答案

由于您的问题不是很详细,这里您将获得一个示例,该示例将在订单评论结帐字段中显示所有产品标题:

add_filter( 'woocommerce_checkout_fields' , 'custom_order_comments_checkout_fields' );
function custom_order_comments_checkout_fields( $fields ) {

    if ( !WC()->cart->is_empty()):

    $output = '';
    $count = 0;
    $cart_item_count = WC()->cart->get_cart_contents_count();

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

        $count++;

        // Displaying the product title
        $output .= 'Product title: ' . $cart_item['data']->post->post_title;

        // New line (for next item)
        if($count < $cart_item_count)
            $output .= '
';

    endforeach;

    $fields['order']['order_comments']['default'] = $output;

    endif;

    return $fields;
}

代码进入您活跃的子主题(或主题)的 function.php 文件。或者也可以在任何插件 php 文件中。

此代码将显示在结帐订单评论字段中,如下所示:

Product title: My Product title 1
Product title: My Product title 2
...

You can easily output product quantity and all kind of data that is in the cart object… You have just to clearly define in your question what you want to output and how (without forgetting that this has to be raw data like, as is outputted in a text area field)

代码已经过测试并且可以工作。

关于php - Woocommerce:结帐订单评论字段中的订单摘要(PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41630771/

有关php - Woocommerce:结帐订单评论字段中的订单摘要(PHP)的更多相关文章

  1. ruby-on-rails - 如何验证非模型(甚至非对象)字段 - 2

    我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss

  2. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  3. ruby-on-rails - 在 Rails 和 ActiveRecord 中查询时忽略某些字段 - 2

    我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr

  4. ruby-on-rails - Sphinx - 何时对字段使用 'has' 和 'indexes' - 2

    我几天前在我的ruby​​onrails2.3.2上安装了Sphinx和Thinking-Sphinx,基本搜索效果很好。这意味着,没有任何条件。现在,我想用一些条件过滤搜索。我有公告模型,索引如下所示:define_indexdoindexestitle,:as=>:title,:sortable=>trueindexesdescription,:as=>:description,:sortable=>trueend也许我错了,但我注意到只有当我将:sortable=>true语法添加到这些属性时,我才能将它们用作搜索条件。否则它找不到任何东西。现在,我还在使用acts_as_tag

  5. Ruby - 如何处理子类意外覆盖父类(super class)私有(private)字段的问题? - 2

    假设您编写了一个类Sup,我决定将其扩展为SubSup。我不仅需要了解你发布的接口(interface),还需要了解你的私有(private)字段。见证这次失败:classSupdefinitialize@privateField="fromsup"enddefgetXreturn@privateFieldendendclassSub问题是,解决这个问题的正确方法是什么?看起来子类应该能够使用它想要的任何字段而不会弄乱父类(superclass)。编辑:equivalentexampleinJava返回"fromSup",这也是它应该产生的答案。 最佳答案

  6. ruby-on-rails - 如何为空白字段编写 rspec? [Rails3.1] - 2

    我使用rails3.1+rspec和factorygirl。我对必填字段(validates_presence_of)的验证工作正常。我如何让测试将该事实用作“成功”而不是“失败”规范是:describe"Addanindustrywithnoname"docontext"Unabletocreatearecordwhenthenameisblank"dosubjectdoind=Factory.create(:industry_name_blank)endit{shouldbe_invalid}endend但是我失败了:Failures:1)Addanindustrywithnona

  7. ruby-on-rails - 在具有 ActiveRecord 条件的相关模型中按字段排序 - 2

    我正在尝试按Rails相关模型中的字段进行排序。我研究的所有解决方案都没有解决如果相关模型被另一个参数过滤?元素模型classItem相关模型:classPriority我正在使用where子句检索项目:@items=Item.where('company_id=?andapproved=?',@company.id,true).all我需要按相关表格中的“位置”列进行排序。问题在于,在优先级模型中,一个项目可能会被多家公司列出。因此,这些职位取决于他们拥有的company_id。当我显示项目时,它是针对一个公司的,按公司内的职位排序。完成此任务的正确方法是什么?感谢您的帮助。PS-我

  8. ruby-on-rails - Sunspot:如何对具有不同值的多个字段进行全文查询? - 2

    我想用sunspot重现以下原始solr查询q=exact_term_text:fooORterm_textv:foo*ORalternate_text:bar*但我无法通过标准的太阳黑子界面理解这是否可能以及如何实现,因为看起来:fulltext方法似乎不接受多个文本/搜索字段参数我不知道将什么参数作为第一个参数传递给fulltext,就好像我通过了"foo"或"bar"结果不匹配如果我传递一个空参数,我得到一个q=*:*范围过滤器(例如with(:term).starting_with('foo*')(顾名思义)作为过滤器查询应用,因此不参与评分。似乎可以手动编写字符串(或者可能使

  9. ruby-on-rails - Rails 3 Formtastic。如何让 formtastic 仅显示月份和年份字段而不显示日期? - 2

    我需要formtastic来仅显示月份和年份字段,而不显示日期字段。日期选择器很好,但它显示了整个日历。我不想要日期选择器。f.input:accounting_month,:label=>"会计月份",:as=>:datepicker我只需要月份和年份。 最佳答案 有一种方法可以做到这一点:"Accountingmonth",:order=>[:month,:year]这将自动隐藏“天”输入并为其指定默认值1。 关于ruby-on-rails-Rails3Formtastic。如何让f

  10. ruby-on-rails - 2个用户之间的产品订单 - 2

    我有三个模型:User、Product、Offer以及这些模型之间的关系问题。场景:用户1发布了一个产品用户2可以向用户1发送报价,例如10美元用户1可以接受或拒绝提议我现在的问题是:用户、产品和报价之间的正确关系是什么?我如何处理那些“接受或拒绝”操作?是否有更好的解决方案?用户模型:classUser:productsend产品型号:classProduct:usersend提供模型:classOffer提前致谢:)编辑:我正在使用Rails3.2.8 最佳答案 警告:小小说来了第1部分:设置关联我建议阅读Railsguideo

随机推荐