Accueil 2026 Forums Forum de Support WooCommerce METTRE A JOUR LE STOCK DE PRODUITS WOOCOMMERCE VIA UN FORMULAIRE



Créativité (1)
2 sujets de 1 à 2 (sur un total de 2)
  • Auteur
    Messages
  • #551172
    zambol.by.zambol
    Participant
    Points: 13 pts

    Bonjour, je souhaite augmenter le stock des produits via un formulaire (wpforms), alors j’ai crée un formulaire d’ajout sur le plugin  wpforms ID:  335 et   deux champs : liste déroulante ID: 1 et nombre ID : 2. ensuite j’ai ajouté le code ci dessous dans la feuille  function.php mais lorsque je teste la quantité du stock du produit ne change pas. quelqu’un a une solution merci

     

    add_action( ‘wpforms_process_complete’, ‘wpf_increase_woocommerce_stock’, 10, 4 );
    function wpf_increase_woocommerce_stock( $form_fields, $form_data, $entry_meta, $entry_id ) {

    // CHANGE THIS: Set your specific WPForms Form ID
    $target_form_id = 335;
    if ( (int) $form_data[‘id’] !== $target_form_id ) {
    return;
    }

    // CHANGE THIS: Replace with your exact Field IDs from WPForms
    $product_id_field_id = 1; // Field ID for Product ID
    $quantity_field_id   = 2; // Field ID for Increase Quantity

    $product_id = isset( $form_fields[$product_id_field_id][‘value’] ) ? intval( $form_fields[$product_id_field_id][‘value’] ) : 0;
    $increase_by = isset( $form_fields[$quantity_field_id][‘value’] ) ? intval( $form_fields[$quantity_field_id][‘value’] ) : 0;

    if ( $product_id > 0 && $increase_by > 0 ) {
    // Fetch the WooCommerce product object
    $product = wc_get_product( $product_id );

    if ( $product && $product->managing_stock() ) {
    // Calculate and set the new increased stock quantity
    $current_stock = $product->get_stock_quantity();
    $new_stock     = $current_stock + $increase_by;

    wc_update_product_stock( $product, $new_stock );
    }
    }
    }

    ...
    #551208
    El Condorito
    Participant
    Points: 230 pts

    Si tu es capable d’utiliser les hooks de wp_forms, tu dois tout aussi bien pouvoir t’en passer.

    Cela ajoute de la complexité inutilement.

    Les formulaires existaient bien avant wp_forms et consorts.

    Dans un premier temps le formulaire :

    <form method= »post » action= » »>

    <input type= »number » name= »product_id » placeholder= »ID du produit » required>

    <input type= »number » name= »quantity » min= »1″ value= »1″ required>

    <button type= »submit » name= »add_to_cart_custom »>Ajouter au panier</button>

    </form>

     

    Puis le code associé :

    add_action(‘init’, function() {

    if (isset($_POST[‘update_stock’]) && current_user_can(‘manage_woocommerce’)) {

    $product_id = intval($_POST[‘product_id’]);

    $quantity_to_add = intval($_POST[‘quantity’]);

     

    if ($product_id && $quantity_to_add > 0) {

    $product = wc_get_product($product_id);

     

    if ($product) {

    $current_stock = $product->get_stock_quantity();

    $new_stock = ($current_stock ? $current_stock : 0) + $quantity_to_add;

     

    wc_update_product_stock($product_id, $new_stock);

     

    wp_die(‘Stock mis à jour’, 200);

    }

    }

    }

    });

    Edit:

    Désolé, je me rends compte que le code a mal été interprété lors de la soumission de la réponse, j’espère néanmoins t’avoir mis sur la voie.

     

    • Cette réponse a été modifiée le il y a 6 jours et 7 heures par El Condorito.
    ...
2 sujets de 1 à 2 (sur un total de 2)
  • Vous devez être connecté pour répondre à ce sujet.


Créativité (1)