Woocommerce 订单详细信息页面自定义字段更新

问题描述 投票:0回答:1

我是 wp 和 php 的新手,但我想要实现的是在管理站点上,当我在更新自定义字段后更新它时,我想通过访问 meta_is 和新更新的 meta_value 来触发我的自定义函数。

截图

这就是我对“woocommerce_process_shop_order_meta”感到厌倦的地方:

function update_vonalkod( $meta_id, $post_id, $meta_key, $meta_value ){
    global $wpdb;

    // Fetch the new value from postmeta table
    $new_value = $meta_value;

    // Fetch existing data from wp_vonalkod table
    $existing_data = $wpdb->get_row(
        $wpdb->prepare(
            "SELECT * FROM {$wpdb->prefix}vonalkod WHERE metaValueID = %d",
            $meta_id
        )
    );
    
    error_log('Existing Data: ' . print_r($existing_data, true));
    
    // Check if data exists
    if ($existing_data) {
        // Update existing data
            $wpdb->update(
            $wpdb->prefix . 'vonalkod',
            array('vonalkod' => $new_value),
            array('metaValueID' => $meta_id), // Condition to identify the row
            array('%s'), // Format for the new value
            array('%d') // Format for the condition
        );
    } else {
        // Throw a notification
        wp_die('Row with metaValueID ' . $meta_id . ' does not exist in wp_vonalkod table.');
    }
}

add_action( 'woocommerce_process_shop_order_meta', 'update_vonalkod', 10, 4 );

然而,我没有成功

wordpress woocommerce hook
1个回答
0
投票

我认为您正在使用

woocommerce_process_shop_order_meta
操作挂钩来更新数据库表中的自定义字段数据通常是正确的,请尝试使用以下代码。

function update_vonalkod( $meta_id, $post_id, $meta_key, $meta_value ) {
    global $wpdb;

    // Define the table name with prefix
    $table_name = $wpdb->prefix . 'vonalkod';

    // Check if the updated meta key matches the target key
    if ( $meta_key === 'your_custom_meta_key' ) {
        // Sanitize meta ID and meta value
        $meta_id = absint( $meta_id );
        $new_value = sanitize_text_field( $meta_value );

        // Update the custom field in the database table
        $wpdb->update(
            $table_name,
            array( 'vonalkod' => $new_value ),
            array( 'metaValueID' => $meta_id ),
            array( '%s' ),
            array( '%d' )
        );

        // Check if the update was successful
        if ( $wpdb->last_error ) {
            // Log the error
            error_log( 'Database error: ' . $wpdb->last_error );
            // Display an error message to the user
            add_action( 'admin_notices', function() {
                echo '<div class="error"><p>Failed to update custom field.</p></div>';
            } );
            // Exit early to prevent further execution
            return;
        }

        // Custom function to execute after updating the custom field
        your_custom_function( $meta_id, $new_value );

        // Optionally, you can add a success message
        add_action( 'admin_notices', function() {
            echo '<div class="updated"><p>Custom field updated successfully.</p></div>';
        } );
    }
}

add_action( 'woocommerce_process_shop_order_meta', 'update_vonalkod', 10, 4 );

function your_custom_function( $meta_id, $new_value ) {
    // Your custom function logic here
}

将“

your_custom_meta_key
”替换为您要定位的实际元键

© www.soinside.com 2019 - 2024. All rights reserved.