2016-04-19 16 views
19

Sto riscontrando il momento più difficile per la creazione di un ordine in WooCommerce. Sto usando il codice qui sotto ed è DOES creare un ordine, MA non posso ottenere informazioni sui clienti O elementi di linea di prodotti aggiunti all'ordine. Il nuovo ordine che viene creato è semplicemente Guest senza articoli, informazioni utente, ecc.Creazione di un nuovo ordine in Woocommerce a livello di programmazione

Il problema sembra essere che una volta creato l'oggetto ordine, non riesce quando si tenta di aggiungere dati all'ordine.

function create_vip_order() { 

    global $woocommerce; 

    $address = array(
     'first_name' => '111Joe', 
     'last_name' => 'Conlin', 
     'company' => 'Speed Society', 
     'email'  => '[email protected]', 
     'phone'  => '760-555-1212', 
     'address_1' => '123 Main st.', 
     'address_2' => '104', 
     'city'  => 'San Diego', 
     'state'  => 'Ca', 
     'postcode' => '92121', 
     'country' => 'US' 
); 

    // Now we create the order 
    $order = wc_create_order(); 

    // The add_product() function below is located in /plugins/woocommerce/includes/abstracts/abstract_wc_order.php 
    $order->add_product(get_product('275962'), 1); // This is an existing SIMPLE product 
    $order->set_address($address, 'billing'); 
    // 
    $order->calculate_totals(); 
    $order->update_status("Completed", 'Imported order', TRUE); 

} 

add_action('woocommerce_init', 'create_vip_order'); 

Qui è l'errore che sto ottenendo in miei log:

[19-Apr-2016 21:16:38 UTC] PHP Fatal error: Uncaught Error: Call to a member function add_product() on boolean in /Users/joe/Sites/speedsociety-2/wp-content/themes/ss/lib/contests/order.php:107 
Stack trace: 
#0 /Users/joe/Sites/speedsociety-2/wp-includes/plugin.php(525): create_vip_order('') 
#1 /Users/joe/Sites/speedsociety-2/wp-content/plugins/woocommerce/woocommerce.php(330): do_action('woocommerce_ini...') 
#2 /Users/joe/Sites/speedsociety-2/wp-includes/plugin.php(525): WooCommerce->init('') 
#3 /Users/joe/Sites/speedsociety-2/wp-settings.php(392): do_action('init') 
#4 /Users/joe/Sites/speedsociety-2/wp-config.php(67): require_once('/Users/joe/Site...') 
#5 /Users/joe/Sites/speedsociety-2/wp-load.php(37): require_once('/Users/joe/Site...') 
#6 /Users/joe/Sites/speedsociety-2/wp-admin/admin.php(31): require_once('/Users/joe/Site...') 
#7 /Users/joe/Sites/speedsociety-2/wp-admin/edit.php(10): require_once('/Users/joe/Site...') 
#8 {main} 
    thrown in /Users/joe/Sites/speedsociety-2/wp-content/themes/ss/lib/contests/order.php on line 107 

Qualsiasi aiuto su questo sarebbe molto apprezzato!

+0

Senza vedere la funzione add_product possiamo solo immaginare, ma è probabile che sia il problema. – bucketman

+0

È una funzione di WooCommerce e ora ho incluso il percorso completo di questa funzione nel codice. Dopo aver cercato questo, mi è sembrato molto comune nella maggior parte degli esempi che ho trovato usare $ order-> add_product, $ order-> set_address, ecc. Se c'è un altro modo in cui dovrei farlo, per favore fammelo sapere. –

+0

sembra che la funzione restituisca false quando $ product o $ item_id non esistono. Potresti voler verificare se stai passando variabili valide.Quando si lavora con gli oggetti si consiglia di eseguire il codice nei blocchi try/catch in modo che possano rilevare gli errori sebbene non conosca la quantità di errori che gestiscono le classi woocommerce. – bucketman

risposta

17

Il problema è nel tuo hook di azione. Usa il seguente hook:

add_action('woocommerce_checkout_process', 'create_vip_order'); 

function create_vip_order() { 

    global $woocommerce; 

    $address = array(
     'first_name' => '111Joe', 
     'last_name' => 'Conlin', 
     'company' => 'Speed Society', 
     'email'  => '[email protected]', 
     'phone'  => '760-555-1212', 
     'address_1' => '123 Main st.', 
     'address_2' => '104', 
     'city'  => 'San Diego', 
     'state'  => 'Ca', 
     'postcode' => '92121', 
     'country' => 'US' 
); 

    // Now we create the order 
    $order = wc_create_order(); 

    // The add_product() function below is located in /plugins/woocommerce/includes/abstracts/abstract_wc_order.php 
    $order->add_product(get_product('275962'), 1); // This is an existing SIMPLE product 
    $order->set_address($address, 'billing'); 
    // 
    $order->calculate_totals(); 
    $order->update_status("Completed", 'Imported order', TRUE); 
} 

Verificare che l'ID prodotto indicato sia presente nel sistema.

+1

Prolem era in agguato in woocommerce_init che molte persone hanno indicato, ma ti ho assegnato la taglia per il primo a fornire una "risposta" completa. Grazie per l'aiuto! –

+0

Felice che la mia risposta ti abbia aiutato. Grazie. :) –

3

In realtà non sono riuscito a capire il tuo problema ma fornendoti un'altra alternativa, questo potrebbe aiutarti.

ho aggiunto i prodotti in $woocommerce->cart prima e quindi assegnare che i dati della spesa a nuovo ordine creato in questo modo:

// Per prodotto semplice

$woocommerce->cart->add_to_cart($product_id, $quantity); 

// Per prodotto variabile

$woocommerce->cart->add_to_cart($product_id, $quantity, $variationID, $attr_array); 

    $order_data = array(
     'status' => apply_filters('woocommerce_default_order_status', 'processing'), 
     'customer_id' => $user_id 
    ); 
    $new_order = wc_create_order($order_data); 
    foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) { 
      $item_id = $new_order->add_product(
        $values['data'], $values['quantity'], array(
       'variation' => $values['variation'], 
       'totals' => array(
        'subtotal' => $values['line_subtotal'], 
        'subtotal_tax' => $values['line_subtotal_tax'], 
        'total' => $values['line_total'], 
        'tax' => $values['line_tax'], 
        'tax_data' => $values['line_tax_data'] // Since 2.2 
       ) 
        ) 
      ); 
     } 
    $new_order->set_address($address, 'billing'); 
    $new_order->set_address($address, 'shipping'); 
+0

@ Maha..ho provato questo codice, pur ottenendo ancora errori di funzione membro. Errore irreversibile: Errore non rilevato: chiamata a una funzione membro add_to_cart() su null in/Users/joe ... ' –

+0

Hai dichiarato' $ woocommerce' globale con il mio codice? –

+0

Sì, dichiarato $ woocommerce e continuo a ricevere l'errore. Ho provato questo anche su una nuova build con lo stesso risultato, quindi ho ancora un problema nel codice che sto usando. –

1

quasi avuto, add_action('woocommerce_init', 'create_vip_order'); woocommerce_init è troppo presto, è necessario modificare il vostro gancio ad almeno init, i vostri errori

[19-Apr-2016 21:16:38 UTC] PHP Fatal error: Uncaught Error: Call to a member function add_product() on boolean in /Users/joe/Sites/speedsociety-2/wp-content/themes/ss/lib/contests/order.php:107 
Stack trace: 
#0 /Users/joe/Sites/speedsociety-2/wp-includes/plugin.php(525): create_vip_order('') 
#1 /Users/joe/Sites/speedsociety-2/wp-content/plugins/woocommerce/woocommerce.php(330): do_action('woocommerce_ini...') 
#2 /Users/joe/Sites/speedsociety-2/wp-includes/plugin.php(525): WooCommerce->init('') 
#3 /Users/joe/Sites/speedsociety-2/wp-settings.php(392): do_action('init') 
#4 /Users/joe/Sites/speedsociety-2/wp-config.php(67): require_once('/Users/joe/Site...') 
#5 /Users/joe/Sites/speedsociety-2/wp-load.php(37): require_once('/Users/joe/Site...') 
#6 /Users/joe/Sites/speedsociety-2/wp-admin/admin.php(31): require_once('/Users/joe/Site...') 
#7 /Users/joe/Sites/speedsociety-2/wp-admin/edit.php(10): require_once('/Users/joe/Site...') 
#8 {main} 
    thrown in /Users/joe/Sites/speedsociety-2/wp-content/themes/ss/lib/contests/order.php on line 107 

mostrano che sta accadendo la variabile $ ordine è stato restituito false e quindi non è possibile utilizzare $order->add_product

ecco il mio codice di lavoro

function create_vip_order() { 

    global $woocommerce; 

    $address = array(
     'first_name' => '111Joe', 
     'last_name' => 'Conlin', 
     'company' => 'Speed Society', 
     'email'  => '[email protected]', 
     'phone'  => '760-555-1212', 
     'address_1' => '123 Main st.', 
     'address_2' => '104', 
     'city'  => 'San Diego', 
     'state'  => 'Ca', 
     'postcode' => '92121', 
     'country' => 'US' 
); 

    // Now we create the order 
    $order = wc_create_order(); 

    // The add_product() function below is located in /plugins/woocommerce/includes/abstracts/abstract_wc_order.php 
    $order->add_product(get_product('129'), 1); // This is an existing SIMPLE product 
    $order->set_address($address, 'billing'); 
    // 
    $order->calculate_totals(); 
    $order->update_status("Completed", 'Imported order', TRUE); 

} 

add_action('init', 'create_vip_order'); 

Buona fortuna e felice codifica: D

+0

Hai ragione, ma dovevi assegnare la taglia a @maha come prima risposta, ma volevo dire grazie per la risposta. –

+0

Ah, è bello, felice di aiutare – Digamber

4

Bene, è possibile farlo senza la funzione wc_create_order.

  $order_data      = array(); 
      $order_data[ 'post_type' ]   = 'shop_order'; 
      $order_data[ 'post_status' ]  = 'wc-' . apply_filters('woocommerce_default_order_status', 'pending'); 
      $order_data[ 'ping_status' ]  = 'closed'; 
      $order_data[ 'post_author' ]  = 1; 
      $order_data[ 'post_password' ]  = uniqid('order_'); 
      $order_data[ 'post_title' ]  = sprintf(__('Order – %s', 'woocommerce'), strftime(_x('%b %d, %Y @ %I:%M %p', 'Order date parsed by strftime', 'woocommerce'), strtotime($post_date))); 
      $order_data[ 'post_parent' ]  = 12; // parent post id 
      $order_data[ 'post_content' ]  = ""; 
      $order_data[ 'comment_status' ] = "open"; 
      $order_data[ 'post_name' ]   = sanitize_title(sprintf(__('Order – %s', 'woocommerce'), strftime(_x('%b %d, %Y @ %I:%M %p', 'Order date parsed by strftime', 'woocommerce'), strtotime($post_date)))); 

      $order_id = wp_insert_post(apply_filters('woocommerce_new_order_data', $order_data), true); 

quindi è possibile utilizzare questo $ order_id per l'aggiunta di altri dettagli, come ...

$order = wc_get_order($order_id); 
$product_item_id = $order->add_product(wc_get_product($product_id)); 
wc_add_order_item_meta($product_item_id,"meta_key","meta_values"); 
$addressShipping = array(
     'first_name' => $shippingName, 
     'email'  => $user_email_id, 
     'phone'  => $billingPhone, 
     'address_1' => $shippingAddress, 
     'address_2' => $shippingAddress2, 
     'city'  => $shippingCity, 
     'state'  => $shippingStateCode, 
     'postcode' => $shippingZip, 
     'country' => 'US'); 
$order->set_address($addressShipping, 'shipping'); 
    $addressBilling = array(
     'first_name' => $billingName, 
     'email'  => $user_email_id, 
     'phone'  => $billingPhone, 
     'address_1' => $billingAddress, 
     'address_2' => $billingAddress2, 
     'city'  => $billingCity, 
     'state'  => $billingStateCode, 
     'postcode' => $billingZip, 
     'country' => 'US'); 
$order->set_address($addressBilling, 'billing'); 
$order->calculate_totals(); 
Problemi correlati