krokedil / briqpay-for-woocommerce

0 stars 3 forks source link

BUG: Updating order in wp-admin overwrites stored _billing_org_nr #46

Open vilhelmjosander opened 1 month ago

vilhelmjosander commented 1 month ago

In class-briqpay-order-management.php:33 in version 1.7.0 you have the following action:

add_action('woocommerce_admin_order_data_after_billing_address', array($this, 'add_order_org_number_field'));

/**
     * Shows the Organization Number for the order.
     *
     * @param WC_Order $order The WooCommerce order.
     * @return void
     */
    public function add_order_org_number_field($order)
    {
        ?>
        <div class="order_data_column" style="clear:both; float:none; width:100%;">
            <div class="edit_address">
                <?php
                woocommerce_wp_text_input(
                    array(
                        'id'            => '_billing_org_nr',
                        'label'         => __('Billing Organization Number', 'briqpay-for-woocommerce'),
                        'wrapper_class' => '_billing_company_field',
                    )
                );
                ?>
            </div>
        </div>
        <?php
    }

Since you are not outputing the value of _billing_org_nr stored when an order is placed (in the callback from Briqpay) this will overwrite the stored _billing_org_nr on the order with an empty value when you just update the order (after manually changing status as an example). This happens here:


        add_action('woocommerce_process_shop_order_meta', array($this, 'save_org_nr_to_order'), 45, 2);

/**
     * Saves the Billing org number.
     *
     * @param int $post_id WordPress post id.
     * @return void
     */
    public function save_org_nr_to_order($post_id)
    {
        $org_number = filter_input(INPUT_POST, '_billing_org_nr', FILTER_SANITIZE_SPECIAL_CHARS);
        $order      = wc_get_order( $post_id );
        $order->update_meta_data( '_billing_org_nr', $org_number );
        $order->save();
    }

The $org_number will always be empty here UNLESS you actually edit the order details and manually enter it which you usually dont when just changing the status.

This will be fixed by just adding the following line to the add_order_org_number_field-method:

public function add_order_org_number_field($order)
    {
        ?>
        <div class="order_data_column" style="clear:both; float:none; width:100%;">
            <div class="edit_address">
                <?php
                woocommerce_wp_text_input(
                    array(
                        'id'            => '_billing_org_nr',
                        'label'         => __('Billing Organization Number', 'briqpay-for-woocommerce'),
                        'wrapper_class' => '_billing_company_field',
                        'value' => $order->get_meta('_billing_org_nr') // <---- ADDED
                    )
                );
                ?>
            </div>
        </div>
        <?php
    }
MichaelBengtsson commented 1 week ago

Thank you for this @vilhelmjosander ! We have just merged a pull request with this code into our development branch, and it will be released with the next version.