【问题标题】:Switch fields order in Easy Digital Downloads checkout在 Easy Digital Downloads 结账时切换字段顺序
【发布时间】:2021-01-09 09:48:52
【问题描述】:

我想通过我的functions.php文件中的sn-p来切换下面代码中'Billing City'和'Billing ZIP'字段的顺序。我已经做了好几个小时了,但我做错了。这是 easy-digital-downloads/includes/checkout/template.php 文件中的代码,非常感谢您的帮助:

<?php
do_action( 'edd_after_cc_fields' );

echo ob_get_clean();
}
add_action( 'edd_cc_form', 'edd_get_cc_form' );

/**
 * Outputs the default credit card address fields
 *
 * @since 1.0
 * @return void
 */
function edd_default_cc_address_fields() {

    $logged_in = is_user_logged_in();
    $customer  = EDD()->session->get( 'customer' );
    $customer  = wp_parse_args( $customer, array( 'address' => array(
        'line1'   => '',
        'line2'   => '',
        'city'    => '',
        'zip'     => '',
        'state'   => '',
        'country' => ''
    ) ) );

    $customer['address'] = array_map( 'sanitize_text_field', $customer['address'] );

    if( $logged_in ) {

        $user_address = get_user_meta( get_current_user_id(), '_edd_user_address', true );

        foreach( $customer['address'] as $key => $field ) {

            if ( empty( $field ) && ! empty( $user_address[ $key ] ) ) {
                $customer['address'][ $key ] = $user_address[ $key ];
            } else {
                $customer['address'][ $key ] = '';
            }

        }

    }

    /**
     * Billing Address Details.
     *
     * Allows filtering the customer address details that will be pre-populated on the checkout form.
     *
     * @since 2.8
     *
     * @param array $address The customer address.
     * @param array $customer The customer data from the session
     */
    $customer['address'] = apply_filters( 'edd_checkout_billing_details_address', $customer['address'], $customer );

    ob_start(); ?>
    <fieldset id="edd_cc_address" class="cc-address">
        <legend><?php _e( 'Billing Details', 'easy-digital-downloads' ); ?></legend>
        <?php do_action( 'edd_cc_billing_top' ); ?>
        <p id="edd-card-address-wrap">
            <label for="card_address" class="edd-label">
                <?php _e( 'Billing Address', 'easy-digital-downloads' ); ?>
                <?php if( edd_field_is_required( 'card_address' ) ) { ?>
                    <span class="edd-required-indicator">*</span>
                <?php } ?>
            </label>
            <span class="edd-description"><?php _e( 'The primary billing address for your credit card.', 'easy-digital-downloads' ); ?></span>
            <input type="text" id="card_address" name="card_address" class="card-address edd-input<?php if( edd_field_is_required( 'card_address' ) ) { echo ' required'; } ?>" placeholder="<?php _e( 'Address line 1', 'easy-digital-downloads' ); ?>" value="<?php echo $customer['address']['line1']; ?>"<?php if( edd_field_is_required( 'card_address' ) ) {  echo ' required '; } ?>/>
        </p>
        <p id="edd-card-address-2-wrap">
            <label for="card_address_2" class="edd-label">
                <?php _e( 'Billing Address Line 2 (optional)', 'easy-digital-downloads' ); ?>
                <?php if( edd_field_is_required( 'card_address_2' ) ) { ?>
                    <span class="edd-required-indicator">*</span>
                <?php } ?>
            </label>
            <span class="edd-description"><?php _e( 'The suite, apt no, PO box, etc, associated with your billing address.', 'easy-digital-downloads' ); ?></span>
            <input type="text" id="card_address_2" name="card_address_2" class="card-address-2 edd-input<?php if( edd_field_is_required( 'card_address_2' ) ) { echo ' required'; } ?>" placeholder="<?php _e( 'Address line 2', 'easy-digital-downloads' ); ?>" value="<?php echo $customer['address']['line2']; ?>"<?php if( edd_field_is_required( 'card_address_2' ) ) {  echo ' required '; } ?>/>
        </p>
        <p id="edd-card-city-wrap">
            <label for="card_city" class="edd-label">
                <?php _e( 'Billing City', 'easy-digital-downloads' ); ?>
                <?php if( edd_field_is_required( 'card_city' ) ) { ?>
                    <span class="edd-required-indicator">*</span>
                <?php } ?>
            </label>
            <span class="edd-description"><?php _e( 'The city for your billing address.', 'easy-digital-downloads' ); ?></span>
            <input type="text" id="card_city" name="card_city" class="card-city edd-input<?php if( edd_field_is_required( 'card_city' ) ) { echo ' required'; } ?>" placeholder="<?php _e( 'City', 'easy-digital-downloads' ); ?>" value="<?php echo $customer['address']['city']; ?>"<?php if( edd_field_is_required( 'card_city' ) ) {  echo ' required '; } ?>/>
        </p>
        <p id="edd-card-zip-wrap">
            <label for="card_zip" class="edd-label">
                <?php _e( 'Billing Zip / Postal Code', 'easy-digital-downloads' ); ?>
                <?php if( edd_field_is_required( 'card_zip' ) ) { ?>
                    <span class="edd-required-indicator">*</span>
                <?php } ?>
            </label>
            <span class="edd-description"><?php _e( 'The zip or postal code for your billing address.', 'easy-digital-downloads' ); ?></span>
            <input type="text" size="4" id="card_zip" name="card_zip" class="card-zip edd-input<?php if( edd_field_is_required( 'card_zip' ) ) { echo ' required'; } ?>" placeholder="<?php _e( 'Zip / Postal Code', 'easy-digital-downloads' ); ?>" value="<?php echo $customer['address']['zip']; ?>"<?php if( edd_field_is_required( 'card_zip' ) ) {  echo ' required '; } ?>/>
        </p>
        <p id="edd-card-country-wrap">
            <label for="billing_country" class="edd-label">
                <?php _e( 'Billing Country', 'easy-digital-downloads' ); ?>
                <?php if( edd_field_is_required( 'billing_country' ) ) { ?>
                    <span class="edd-required-indicator">*</span>
                <?php } ?>
            </label>
            <span class="edd-description"><?php _e( 'The country for your billing address.', 'easy-digital-downloads' ); ?></span>
            <select name="billing_country" id="billing_country" data-nonce="<?php echo wp_create_nonce( 'edd-country-field-nonce' ); ?>" class="billing_country edd-select<?php if( edd_field_is_required( 'billing_country' ) ) { echo ' required'; } ?>"<?php if( edd_field_is_required( 'billing_country' ) ) {  echo ' required '; } ?>>
                <?php

                $selected_country = edd_get_shop_country();

                if( ! empty( $customer['address']['country'] ) && '*' !== $customer['address']['country'] ) {
                    $selected_country = $customer['address']['country'];
                }

                $countries = edd_get_country_list();
                foreach( $countries as $country_code => $country ) {
                  echo '<option value="' . esc_attr( $country_code ) . '"' . selected( $country_code, $selected_country, false ) . '>' . $country . '</option>';
                }
                ?>
            </select>
        </p>
        <p id="edd-card-state-wrap">
            <label for="card_state" class="edd-label">
                <?php _e( 'Billing State / Province', 'easy-digital-downloads' ); ?>
                <?php if( edd_field_is_required( 'card_state' ) ) { ?>
                    <span class="edd-required-indicator">*</span>
                <?php } ?>
            </label>
            <span class="edd-description"><?php _e( 'The state or province for your billing address.', 'easy-digital-downloads' ); ?></span>
            <?php
            $selected_state = edd_get_shop_state();
            $states         = edd_get_shop_states( $selected_country );

            if( ! empty( $customer['address']['state'] ) ) {
                $selected_state = $customer['address']['state'];
            }

            if( ! empty( $states ) ) : ?>
            <select name="card_state" id="card_state" class="card_state edd-select<?php if( edd_field_is_required( 'card_state' ) ) { echo ' required'; } ?>">
                <?php
                    foreach( $states as $state_code => $state ) {
                        echo '<option value="' . $state_code . '"' . selected( $state_code, $selected_state, false ) . '>' . $state . '</option>';
                    }
                ?>
            </select>
            <?php else : ?>
            <?php $customer_state = ! empty( $customer['address']['state'] ) ? $customer['address']['state'] : ''; ?>
            <input type="text" size="6" name="card_state" id="card_state" class="card_state edd-input" value="<?php echo esc_attr( $customer_state ); ?>" placeholder="<?php _e( 'State / Province', 'easy-digital-downloads' ); ?>"/>
            <?php endif; ?>
        </p>
        <?php do_action( 'edd_cc_billing_bottom' ); ?>
        <?php wp_nonce_field( 'edd-checkout-address-fields', 'edd-checkout-address-fields-nonce', false, true ); ?>
    </fieldset>
    <?php
    echo ob_get_clean();
}
add_action( 'edd_after_cc_fields', 'edd_default_cc_address_fields' );

【问题讨论】:

  • 只是为了更好地理解它:我不知道那个插件,但如果那是一个模板文件,不应该通过将它复制到您的主题来替换它,以便您可以随意编辑它吗?如果覆盖有效,则交换顺序应该很容易,只需剪切+粘贴代码即可。编辑:见这里 (docs.easydigitaldownloads.com/article/…)
  • 感谢您的帮助!这仅适用于 easy-digital-downloads/templates 下的 checkout_cart.php 文件,不适用于 easy-digital-downloads/includes/checkout/...下的此文件。

标签: php wordpress easy-digital-downloads


【解决方案1】:

好的,根据您的 cmets,我想说实现这一目标的最简单(不是最干净)的方法是包含一小块 JS 来移动 DIV

$("#edd-card-zip-wrap").insertBefore("#edd-card-city-wrap");

应该够了:)

编辑: 将此代码粘贴到主题的functions.php中

add_action( 'wp_enqueue_scripts', 'enqueue_my_script' );
function enqueue_my_script() {
    wp_enqueue_script( 'my-custom-script', get_stylesheet_directory_uri() . '/my-script.js', array( "jquery" ), false, true );
}

这将搜索位于主题根文件夹中的名为“my-script.js”的文件(与functions.php文件相同) 该文件内容可能是这样的:

使用固定等待编辑:

$("document").ready(function() {
    var waitThisTime = 1000; // Wait this ms. 1000ms = 1sec
    setTimeout(function(){
        $("#edd-card-zip-wrap").insertBefore("#edd-card-city-wrap");
    },waitThisTime);
});

【讨论】:

  • 对不起,直接在 SO 里面写得很快,遗漏了一些括号。立即尝试:)
  • 好的,有一个快速解决方法,将所有“$”替换为“jQuery”
  • 现在一切都清楚了。您的字段出现在页面加载之后(这意味着它们由某种 Javascript 代码呈现)。所以我给你的 sn-p 不能工作,因为当文档准备好时(参见 document.ready(...)),字段不存在。如果字段存在,我的代码实际上可以工作的证据是,一旦页面加载,您就可以打开开发人员工具(如果您使用的是 Google Chrome,则只需右键单击页面的任何区域,然后单击上下文菜单中的“检查” )。您将在开发工具中找到一个控制台选项卡,粘贴代码并查看它是否有效 :)
  • 有不同的可能性,但它们都取决于页面中的 JS 如何加载结帐字段。您在技术上需要做的是让我粘贴的代码“等待”,直到页面中出现这些字段。如果呈现它们的代码制作精良,那么它可能没有任何 hack,否则我可以提出一种 hack-ish 方式来做到这一点,但这不会那么好、清晰且制作精良。更像是“最后一次尝试”
  • 我更新了我的答案,但这只是一个例子,它甚至可能有效,但不是 100% 准确。您可以选择任意时间量(例如甚至 2 秒),但这对于某些人来说可能太多了,然后才真正看到正在执行的代码,或者对于其他人来说太短了。它实际上取决于 2 个因素,渲染字段以从服务器获取响应的 AJAX 代码的速度以及需要执行 JS 操作的客户端设备的速度
【解决方案2】:

您可以使用以下代码 sn-p 来做到这一点。

add_filter( 'woocommerce_checkout_fields', 'ro_postcode_city_interchange' );
 
function ro_postcode_city_interchange( $checkout_fields ) {
    $checkout_fields['billing']['billing_postcode']['priority'] = 70;
    $checkout_fields['billing']['billing_city']['priority'] = 90;
    return $checkout_fields;
}

此代码截断器将交换顺序,因为所有字段都有其优先级,因此在这里您必须将city 的优先级设置为postcodecity 的优先级设置为zipcode

经过测试并且运行良好。

【讨论】:

  • 您好,这个网上商店在 Easy Digital Downloads 上运行,而不是在 Woocommerce 上运行。您可以自行检查:访问 leden.stefaanoyen.be/downloads/klanten-kiezen-jou-promo,单击按钮并检查“Factuuradres - plaats”和“Factuuradres - 邮政编码”字段。它们应该是相反的......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 2019-09-21
  • 2021-11-11
  • 1970-01-01
相关资源
最近更新 更多