【问题标题】:Woocommerce: help replacing html outputWoocommerce:帮助替换 html 输出
【发布时间】:2015-11-18 09:40:20
【问题描述】:

我的结帐帐单字段和运输字段以 html 格式输出,具有 3 种不同的行对齐方式。首先,最后和广泛。我只想将它们设置为第一个和最后一个 - 以便每个输入彼此相邻。

原文:

<div class="woocommerce-billing-fields">

    <h3>Faktureringsdetaljer</h3>




    <p class="form-row form-row form-row-first validate-required" id="billing_first_name_field"><label for="billing_first_name" class="">Fornavn <abbr class="required" title="påkrevet">*</abbr></label><input type="text" class="input-text " name="billing_first_name" id="billing_first_name" placeholder="" value=""></p>

    <p class="form-row form-row form-row-last validate-required" id="billing_last_name_field"><label for="billing_last_name" class="">Etternavn <abbr class="required" title="påkrevet">*</abbr></label><input type="text" class="input-text " name="billing_last_name" id="billing_last_name" placeholder="" value=""></p><div class="clear"></div>

    <p class="form-row form-row form-row-wide" id="billing_company_field"><label for="billing_company" class="">Navn på firma</label><input type="text" class="input-text " name="billing_company" id="billing_company" placeholder="" value=""></p>

    <p class="form-row form-row form-row-wide address-field validate-required" id="billing_address_1_field"><label for="billing_address_1" class="">Adresse <abbr class="required" title="påkrevet">*</abbr></label><input type="text" class="input-text " name="billing_address_1" id="billing_address_1" placeholder="Gateadresse" value=""></p>

    <p class="form-row form-row form-row-last address-field validate-required validate-postcode" id="billing_postcode_field"><label for="billing_postcode" class="">Postnummer  <abbr class="required" title="påkrevet">*</abbr></label><input type="text" class="input-text " name="billing_postcode" id="billing_postcode" placeholder="Postnummer " value=""></p><div class="clear"></div>

    <p class="form-row form-row form-row-wide address-field validate-required" id="billing_city_field"><label for="billing_city" class="">Sted <abbr class="required" title="påkrevet">*</abbr></label><input type="text" class="input-text " name="billing_city" id="billing_city" placeholder="Sted" value=""></p>

    <p class="form-row form-row form-row-first address-field validate-state" id="billing_state_field" style="display: none"><label for="billing_state" class="">Delstat / Fylke</label><input type="hidden" class="hidden" name="billing_state" id="billing_state" value="" placeholder=""></p>

    <p class="form-row form-row form-row-first validate-required validate-email" id="billing_email_field"><label for="billing_email" class="">Epostadresse <abbr class="required" title="påkrevet">*</abbr></label><input type="email" class="input-text " name="billing_email" id="billing_email" placeholder="" value="kreegah@gmail.com"></p>

    <p class="form-row form-row form-row-last validate-required validate-phone" id="billing_phone_field"><label for="billing_phone" class="">Telefon <abbr class="required" title="påkrevet">*</abbr></label><input type="tel" class="input-text " name="billing_phone" id="billing_phone" placeholder="" value=""></p><div class="clear"></div>


</div>

我想要什么:

<div class="woocommerce-billing-fields">

    <h3>Faktureringsdetaljer</h3>




    <p class="form-row form-row form-row-first validate-required" id="billing_first_name_field"><label for="billing_first_name" class="">Fornavn <abbr class="required" title="påkrevet">*</abbr></label><input type="text" class="input-text " name="billing_first_name" id="billing_first_name" placeholder="" value=""></p>

    <p class="form-row form-row form-row-last validate-required" id="billing_last_name_field"><label for="billing_last_name" class="">Etternavn <abbr class="required" title="påkrevet">*</abbr></label><input type="text" class="input-text " name="billing_last_name" id="billing_last_name" placeholder="" value=""></p>

    <p class="form-row form-row form-row-first" id="billing_company_field"><label for="billing_company" class="">Navn på firma</label><input type="text" class="input-text " name="billing_company" id="billing_company" placeholder="" value=""></p>

    <p class="form-row form-row form-row-last address-field validate-required" id="billing_address_1_field"><label for="billing_address_1" class="">Adresse <abbr class="required" title="påkrevet">*</abbr></label><input type="text" class="input-text " name="billing_address_1" id="billing_address_1" placeholder="Gateadresse" value=""></p>

    <p class="form-row form-row form-row-first address-field validate-required validate-postcode" id="billing_postcode_field"><label for="billing_postcode" class="">Postnummer  <abbr class="required" title="påkrevet">*</abbr></label><input type="text" class="input-text " name="billing_postcode" id="billing_postcode" placeholder="Postnummer " value=""></p>

    <p class="form-row form-row form-row-last address-field validate-required" id="billing_city_field"><label for="billing_city" class="">Sted <abbr class="required" title="påkrevet">*</abbr></label><input type="text" class="input-text " name="billing_city" id="billing_city" placeholder="Sted" value=""></p>

    <p class="form-row form-row form-row-first address-field validate-state" id="billing_state_field" style="display: none"><label for="billing_state" class="">Delstat / Fylke</label><input type="hidden" class="hidden" name="billing_state" id="billing_state" value="" placeholder=""></p>

    <p class="form-row form-row form-row-first validate-required validate-email" id="billing_email_field"><label for="billing_email" class="">Epostadresse <abbr class="required" title="påkrevet">*</abbr></label><input type="email" class="input-text " name="billing_email" id="billing_email" placeholder="" value="kreegah@gmail.com"></p>

    <p class="form-row form-row form-row-last validate-required validate-phone" id="billing_phone_field"><label for="billing_phone" class="">Telefon <abbr class="required" title="påkrevet">*</abbr></label><input type="tel" class="input-text " name="billing_phone" id="billing_phone" placeholder="" value=""></p><div class="clear"></div>


</div>

有人知道我可以在哪里覆盖这些吗?我一直在 woocommerce 文件夹中的大多数 /includes 中上下搜索。一点头绪都没有。

【问题讨论】:

    标签: php html wordpress woocommerce checkout


    【解决方案1】:

    您能具体说明您要更改的内容吗?很难比较和发现差异。

    如果您只想更改某些字段的类,正确的做法是:

    add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
    
    function custom_override_checkout_fields( $fields ) {
    
        $fields['billing']['billing_company']['class'] = array('form-row', 'form-row', 'form-row-first');
        $fields['billing']['billing_address_1']['class'] = array('form-row', 'form-row', 'form-row-first', 'address-field', 'validate-required');
        $fields['billing']['billing_postcode']['class'] = array('form-row', 'form-row', 'form-row-first', 'address-field', 'validate-required');
        $fields['billing']['billing_city']['class'] = array('form-row', 'form-row', 'form-row-last', 'address-field', 'validate-required');
        $fields['billing']['billing_state']['class'] = array('form-row', 'form-row', 'form-row-first', 'address-field', 'validate-state');
        $fields['billing']['billing_email']['class'] = array('form-row', 'form-row', 'form-row-first', 'address-field', 'validate-email');
        $fields['billing']['billing_phone']['class'] = array('form-row', 'form-row', 'form-row-last', 'address-field', 'validate-phone');
        return $fields;
    }
    

    您绝对不应该为此使用 JavaScript。

    【讨论】:

    • 谢谢@Pelmered - 正是我所需要的。一个小问题是&lt;div class="clear"&gt;&lt;/div&gt;,它也在['billing_postcode'] 之后输出。如何删除它?
    • 啊,加$fields['billing']['billing_postcode']['clear'] = ''
    • 要进行更多修改,只需 var_dump $fields 即可了解您应该更改哪些内容以获得所需的更改。当您阅读输出时,它实际上非常简单。
    • 谢谢,解决了@Pelmered。刚刚添加了$fields['billing']['billing_postcode']['clear'] = '';
    • 这是一个非常直接的函数挂钩,因为 $fields 通过过滤器传递,但是,在我自己的情况下,我注意到它只更改了包装字段的 html 元素的类名我的意思是,出于组织目的,这很好用,但是如果我想更改为表单中的所有输入类型设置的各种类怎么办。我正在尝试实现一个 .js 验证插件,该插件需要为标签、文本、单选、复选框、选择和所有类型的表单输入存在这些自定义类。
    【解决方案2】:

    添加这些类似乎是由主题或插件动态添加的,很难追踪根文件以找到添加这些类的原因。

    为了更改类,您需要使用 jQuery 作为最后的手段。幸运的是,每个字段都有 id,因此您可以选择特定元素并更改它们的属性。

    这是一个可能对你有帮助的代码。

    jQuery("#billing_company_field").removeClass( "form-row-wide" ).addClass( "form-row-first" );
    

    上面的代码只会改变一个 div 元素的类。使用,修改和添加其他需要的代码。此代码应在您需要影响更改的页面上排队。

    【讨论】:

    • 是的,这确实是最后的手段。但在这种情况下,很容易以正确的方式进行更改。
    猜你喜欢
    • 2011-11-10
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 2016-01-29
    • 2010-09-28
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多