Création par programmation d'un nouvel ordre dans Woocommerce

J'ai le plus de mal à créer par programmation une commande dans WooCommerce. J'utilise le code ci-dessous et je crée une commande mais je ne peux pas obtenir des informations client ou des articles de la gamme de produits ajoutés à la commande. La nouvelle commande qui est create est simplement en tant Qu'invité sans éléments, informations utilisateur, etc.

Le problème semble être qu'une fois l'objet order créé, il échoue lorsque vous essayez d'ajouter des données à la commande.

function create_vip_order() {

  global $woocommerce;

  $address = array(
      'first_name' => '111Joe',
      'last_name'  => 'Conlin',
      'company'    => 'Speed Society',
      'email'      => 'joe@testing.com',
      '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' );

Voici l'erreur que je reçois dans mon journaux:

[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

Toute aide à ce sujet serait très appréciée!

23
demandé sur Joe Conlin 2016-04-20 00:20:39

4 réponses

Le problème est dans votre crochet d'action. Utilisez le crochet suivant:

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'      => 'joe@testing.com',
      '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);  
}

Assurez-vous que l'id de produit donné doit exister dans le système.

23
répondu Maha Dev 2016-04-29 04:35:42

Eh bien, vous pouvez le faire sans la fonction 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 );

, Alors vous pouvez utiliser ce $order_id pour ajouter d'autres détails, comme...

$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();
6
répondu Rahul Prajapati 2016-05-05 20:30:15

En fait, je ne pouvais pas comprendre votre problème, mais en vous fournissant une autre alternative, cela pourrait vous aider.

J'ai d'abord ajouté des produits dans $woocommerce->cart, puis j'attribue ces données de panier à un nouvel ordre créé comme ceci:

/ / pour un produit simple

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

/ / Pour produit variable

    $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');
4
répondu Maha Dev 2016-04-20 04:22:35

Tu l'as presque eu, add_action( 'woocommerce_init', 'create_vip_order' ); woocommerce_init est trop tôt, vous devez changer votre hook pour au moins init, vos erreurs

[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

Montrer que cela se passe la variable $ order a été renvoyée false et donc vous ne pouvez pas utiliser $order->add_product

Voici mon code de travail

function create_vip_order() {

  global $woocommerce;

  $address = array(
      'first_name' => '111Joe',
      'last_name'  => 'Conlin',
      'company'    => 'Speed Society',
      'email'      => 'joe@testing.com',
      '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' );

Bonne chance et bon codage: D

1
répondu Digamber 2016-04-29 10:28:35