8768 sujets

Développement web côté serveur, CMS

je souhaite enregistrer les commandes par unité de ligne de commande.
exemple: si une ligne commande a quantité = 5 , porduit id = 121
je l'a décompose en 5 ligne de commandes chacune ayant quantité = 1 et produit id 121.


add_action('woocommerce_checkout_update_order_meta', 'customise_checkout_field_update_order_meta');
function customise_checkout_field_update_order_meta($order_id)
{   $array_nom = $_POST['nom_client'];
    $array_type = $_POST['type_identifiant'];
    $array_ident = $_POST['identifiant_client'];

    $order = wc_get_order($order_id);
    $order_items = $order->get_items();

    foreach($order_items as $order_item_id => $order_item){
        $order_item_id = sanitize_text_field($order_item_id);
        $order_item_name = sanitize_text_field($order_item->get_name());
            $cart = WC()->session->cart;
            foreach($cart as $cart_id => $cart_item){
                $cart_product_id = $cart_item['product_id'];
                $cart_quantity = sanitize_text_field($cart_item['quantity']);
                while($cart_quantity >= 1){
                    $product = wc_get_product($cart_item['product_id']);
                    $item = new WC_Order_Item_Product($order_item_id+1);
                    $item->set_props(array(
                        '_qty' => $cart_item['quantity'],
                        'name' => $product ? $product->get_name() : '', 
                        '_tax_class' => $product ? $product->get_tax_class() : '', 
                        '_product_id' => $product ? $product->is_type('variation') ? $product->get_parent_id() : $product->get_id() : 0, 
                        '_variation_id' => $product && $product->is_type('variation') ? $product->get_id() : 0, 
                        'variation' => $cart_item['variation'], 
                        '_line_subtotal' => $cart_item['line_subtotal'], 
                        '_line_total' => $cart_item['line_total'], 
                        '_line_subtotal_tax' => $cart_item['line_subtotal_tax'], 
                        '_line_total_tax' => $cart_item['line_tax'], 
                        '_line_tax' => $cart_item['line_tax'],
                        '_line_tax_data' => $cart_item['line_tax_data']
                    ));
                     $item->set_backorder_meta();
                    // Set this to pass to legacy actions.
                    $item->legacy_values = $cart_item;
                    $item->legacy_cart_item_key = $cart_item_key;
                    //$order->add_item($item);
                    wc_save_order_items($order_id,$item);
                    $cart_quantity--;
                }
            }
    }

}

Résultat : SyntaxError: JSON.parse: unexpected character at line 4 column 1 of the JSON data