更改WooCommerce结帐管理器中其他结帐字段的顺序

时间:2018-08-23 15:58:32

标签: php wordpress woocommerce checkout custom-taxonomy

在Woocommerce中,我想更改使用WooCommerce checkout manager plugin进行的其他结帐字段顺序(以编程方式)。这是我实际上拥有的:

enter image description here

我需要在“格式”字段之后的“每箱卷数”字段中更改顺序。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

WooCommerce结帐管理器插件不允许重新排序其他字段,并且该数据保存在他的某些选项设置中。

因此,方法是仅更改一次插件设置。

  

要运行以下代码,只需浏览网站的任何页面。
  您将只运行一次,然后将其删除。

代码:

function wccs_change_fields_order(){
    $wccs_settings = get_option( 'wccs_settings' );

    // Get the array of additional fields
    $additional_fields = $wccs_settings['buttons'];

    // SETTINGS: Here set your 2 product attribute Names
    $format    = 'Format';
    $rp_carton = 'Rolls per Carton';

    // FIRST Loop Get the array key for 'Rolls per Carton' field label
    foreach( $additional_fields as $key => $field ){
        if( $field['label'] == $rp_carton )
            $key_rp_carton = $key;
    }

    // Save 'Rolls per Carton' data field in a variable
    $field_rp_carton = $additional_fields[$key_rp_carton];

    // Remove 'Rolls per Carton' data field from the fields array
    unset($additional_fields[$key_rp_carton]);

    // Initializing variables
    $new_additional_fields = array();
    $sorting_index = 1;

    // SECOND Loop reordering fields
    foreach( $additional_fields as $key => $field ){
        // Update "order" argument
        $field['order'] = $sorting_index;

        // Add the current field to a new array
        $new_additional_fields[] = $field;

        $sorting_index++; // Increment sorting index

        // When we reach 'Format' data field
        if( $field['label'] == $format ){
            // Set a new position to 'Rolls per Carton' attribute
            $field_rp_carton['order'] = $sorting_index;

            // Add 'Rolls per Carton' field data in the new array
            $new_additional_fields[] = $field_rp_carton;

            $sorting_index++; // Increment sorting index
        }
    }
    // Set back the reordered additional fields in the main array
    $wccs_settings['buttons'] = $new_additional_fields;

    // Update the new changed options (Save)
    update_option( 'wccs_settings', $wccs_settings, 'yes' );
}

wccs_change_fields_order();

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。

相关问题