WooCommerce Ajouter un article à la commande avec la méta de commande d'article

Oct 30 2020

Je souhaite supprimer un élément WooComerce existant et y ajouter x nouveaux éléments WooComerce. Cela dépend de la hauteur de la quantité $ item->.

Mon code ressemble à ceci:


add_action( 'woocommerce_thankyou', 'my_change_status_function', 10, 1 );

function my_change_status_function( $order_id ) {

    $order = new WC_Order( $order_id );
    foreach ( $order->get_items() as $item_id => $item ) { if ( $item->get_quantity() > 1 ) {
            wc_delete_order_item( $item_id ); for ( $i = 1; $i <= $item->get_quantity(); $i++ ) { $new_item_ids = woocommerce_add_order_item(
                    $order_id, array( 'order_item_name' => $item->get_name() . '_' . $i, 'order_item_type' => 'line_item', ) ); if ( $new_item_ids ) {

                    foreach ( $metavalues as $key => $value ) { wc_add_order_item_meta( $new_item_ids, $key, $value );
                    }
                }
            }
        }
    }
}

Le point où je suis un peu désespéré est ici:


if ( $new_item_ids ) { foreach ( $metavalues as $key => $value ) { <-- I dont know where i get the $metavalues wc_add_order_item_meta( $new_item_ids, $key, $value );
    }
}

J'ai déjà essayé ce post de Stackoverflow:

WooCommerce Ajouter un article à la commande

Quelqu'un pourrait-il m'aider?

Merci d'avance

Réponses

2 LoicTheAztec Oct 30 2020 at 05:19

Depuis WooCommerce 3, votre code est obsolète et peut être simplifié à l'aide de certaines WC_Abstract_Orderméthodes. Vous devriez également mieux utiliser le woocommerce_checkout_order_createdhook à la place, qui est déclenché après la création de la commande:

add_action( 'woocommerce_checkout_order_created', 'separate_order_items_by_quantity' );
function separate_order_items_by_quantity( $order ) { $items_processed = false; // Initializing

    // Loop through order items
    foreach ( $order->get_items() as $item_id => $item ) { $quantity = $item->get_quantity(); // Get quantity $product  = $item->get_product(); // Get the WC_Product Object // When item has quatity more than 1, separete items by quantity if ( $item->get_quantity() > 1 ) {
            $order->remove_item( $item_id ); // Remove item
            $items_processed = true; // Flag it // Loop through quantity for ( $i = 1; $i <= $quantity; $i++ ) { // Add the same productb (separated) n times the quantity $new_item_id = $order->add_product( $product, 1, array(
                    '_custom_index' => $i, // (required) to have separated products 'name' => $product->get_name() . ' ' . $i, // (optional) change product name appending the index 'Some meta key' => 'some meta value', // (optional) Here you can set a custom meta key and its value (custom order item meta data) … To hide it from customer the metakey should start with an underscore. ) ); } } } if( $items_processed ) {
        $order->apply_changes(); // Merge changes with data and clear $order->save(); // Save data
    }
}

Le code va dans le fichier functions.php du thème enfant actif (ou thème actif). Testé et fonctionne.