Support

Account

Home Forums General Issues update_field(‘location’, $new_location, $product_id);

Unread

update_field(‘location’, $new_location, $product_id);

  • I’m struggling getting a field value updated using a plugin.

    Can anyone tell me what i’m doing wrong when calling to the update_field function?

    Thanks

    THis is the simple plugin code

    // Adding option to admin panel
    add_action(‘admin_menu’, ‘ple_add_admin_menu’);

    function ple_add_admin_menu() {
    add_menu_page(
    ‘Product Location Editor’,
    ‘Location Editor’,
    ‘manage_woocommerce’,
    ‘product-location-editor’,
    ‘ple_admin_page_content’,
    ‘dashicons-location-alt’,
    20
    );
    }

    // Function to create the admin page
    function ple_admin_page_content() {
    ?>
    <div class=”wrap”>
    <h1>Change Location of Product</h1>
    <form method=”post” action=””>
    <label for=”product_search”>Search product by SKU or description:</label><br>
    <input type=”text” name=”product_search” id=”product_search” placeholder=”input SKU o description” required>
    <input type=”submit” value=”Search Product” class=”button button-primary”>
    </form>

    <?php
    if (!empty($_POST[‘product_search’])) {
    ple_search_and_update_product(sanitize_text_field($_POST[‘product_search’]));
    }
    ?>
    </div>
    <?php
    }

    // Function to search the product
    function ple_search_and_update_product($search_term) {
    // Buscar productos por SKU o descripción
    $args = array(
    ‘post_type’ => ‘product’,
    ‘s’ => $search_term,
    ‘posts_per_page’ => 10,
    );

    $products = new WP_Query($args);

    if ($products->have_posts()) {
    echo ‘<h2>Resultados de la búsqueda:</h2>’;
    while ($products->have_posts()) {
    $products->the_post();
    $product_id = get_the_ID();
    $product_name = get_the_title();
    $location = get_field(‘location’, $product_id);

    echo ‘<form method=”post” action=””>’;
    echo ‘<p>‘ . esc_html($product_name) . ‘</p>’;
    echo ‘<input type=”hidden” name=”product_id” value=”‘ . esc_attr($product_id) . ‘”>’;
    echo ‘<label for=”new_location”>Location actual: ‘ . esc_html($location) . ‘</label><br>’;
    echo ‘<input type=”text” name=”new_location” placeholder=”Nuevo Location” required>’;
    echo ‘<input type=”submit” value=”Actualizar Location” class=”button button-primary”>’;
    echo ‘</form>’;
    echo ‘<hr>’;
    }
    wp_reset_postdata();
    } else {
    echo ‘<p>The search did not provide results.</p>’;
    }
    }

    // Function to update field value “location”
    add_action(‘admin_post_ple_update_location’, ‘ple_update_location’);

    function ple_update_location() {
    if (!empty($_POST[‘product_id’]) && !empty($_POST[‘new_location’])) {
    $product_id = intval($_POST[‘product_id’]);
    $new_location = sanitize_text_field($_POST[‘new_location’]);

    // Update sentence
    update_field(‘location’, $new_location, $product_id);
    //update_post_meta($product_id, ‘location’, $new_location);

    wp_redirect(admin_url(‘admin.php?page=product-location-editor&updated=true’));
    exit;
    }
    }

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.