【问题标题】:Move customer order notes field in woocommerce在 woocommerce 中移动客户订单备注字段
【发布时间】:2018-09-27 13:03:02
【问题描述】:

大家好,我通过搜索和寻找解决方案来对我在 woocommerce 上的结帐进行一些编辑来管理。

到目前为止,我已经成功了,所以我跳过了购物车/购物篮,直接去结账,并重命名了提交的订单说明。使用下面的功能代码。

我正在努力解决的是如何将订单备注字段移动到订单评论上方。我知道我需要使用这个 woocommerce_checkout_before_order_review 但我不确定如何。有什么帮助会很棒吗?

add_filter('woocommerce_add_to_cart_redirect', 'themeprefix_add_to_cart_redirect');
function themeprefix_add_to_cart_redirect() {
 global $woocommerce;
 $checkout_url = wc_get_checkout_url();
 return $checkout_url;
}

//Add New Pay Button Text
add_filter( 'woocommerce_product_single_add_to_cart_text', 'themeprefix_cart_button_text' ); 
add_filter( 'woocommerce_product_add_to_cart_text', 'themeprefix_cart_button_text' ); 

function themeprefix_cart_button_text() {
 return __( 'Add to cart & go to checkout', 'woocommerce' );
}

// Place this code in your theme's functions.php file

// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
     $fields['order']['order_comments']['required'] = true;
     return $fields;
}

add_filter( 'woocommerce_checkout_fields', 'filter_checkout_fields' );

function filter_checkout_fields( $fields ) {
    $fields['order']['order_comments']['maxlength'] = 160;
    $fields['order']['order_comments']['label'] = '<h2>Personalized Message</h2>';
     $fields['order']['order_comments']['placeholder'] = 'Add message here';
    return $fields;
}

【问题讨论】:

  • 订单注释不能通过钩子删除,因为它们位于 Woocommerce 模板checkout/form-shipping.php 中......所以你应该需要override Woocommerce templates via your active theme 删除从checkout/form-shipping.phpcheckout/review-order.php 的整个代码块......

标签: php wordpress woocommerce


【解决方案1】:

LoicTheAztec 是正确的。订单备注只能通过调整 Woocommerce 模板进行自定义。

在多个站点上,我必须从正确的模板中删除并添加以下代码。这是来自默认的 Woocommerce 3.x 模板。

<?php if ( apply_filters( 'woocommerce_enable_order_notes_field', 'yes' === get_option( 'woocommerce_enable_order_comments', 'yes' ) ) ) : ?>

    <?php if ( ! WC()->cart->needs_shipping() || wc_ship_to_billing_address_only() ) : ?>

        <h3><?php _e( 'Additional information', 'woocommerce' ); ?></h3>

    <?php endif; ?>

    <div class="woocommerce-additional-fields__field-wrapper">
        <?php foreach ( $checkout->get_checkout_fields( 'order' ) as $key => $field ) : ?>
            <?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
        <?php endforeach; ?>
    </div>

<?php endif; ?>

【讨论】:

  • 非常感谢。我刚刚完成了模板覆盖并找到并移动了这段代码。这已将其移至我想要的位置,但我现在有重复项。正如我有两个订单备注字段。
  • 覆盖包含此代码的旧模板并删除覆盖中的代码。它应该可以工作,因为注释字段仅由此代码生成。
  • 我想我做到了。我将 checkout/form-shipping.php 和 checkout/review-order.php 复制到 /wp-content/themes/mytheme/woocommerce/checkout/ 然后我从运输中删除了上述代码并将其添加到审查中。然后该字段显示在我网站上的正确位置,但我有两个相同的字段。
  • 请仔细检查子主题覆盖中的 form-shipping.php。也许添加一些额外的 HTML 以确保它正在被使用。如果没有显示额外的 html,则说明使用了其他模板。
  • 刚刚检查并正在使用覆盖。 link
猜你喜欢
  • 2022-01-20
  • 2018-05-07
  • 2020-09-14
  • 2020-11-14
  • 1970-01-01
  • 2018-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多