2014-10-31 65 views
1

我创建一个WordPress和WooCommerce插件,它具有以下功能:更新woocommerce秩序,而不是创建一个新的

  1. 匿名用户定制产品的页面
  2. 上。在PHP脚本在此过程中,它会在数据库中创建订单
  3. 用户转到请求客户数据,交货等的结账页面。

通过点击“立即购买” WooCommerce在系统中创建一个新的秩序和我要的是更新在个性化过程早期创建的订单,增加客户的详细信息,付款,发货等来了订购。

这可能吗?

谢谢!

回答

0

您可以使用函数wc_update_order()来获取现有的订单对象。

$order_id = $_GET[ 'order_id' ] 
$order = wc_update_order(array ('order_id' => $order_id)); 

// now you got the woocommerce order object and you can execute many functions 
// 
$address = array(
     'first_name' => 'Fresher', 
     'last_name' => 'StAcK OvErFloW', 
     'company' => 'stackoverflow', 
     'email'  => '[email protected]', 
     'phone'  => '777-777-777-777', 
     'address_1' => '31 Main Street', 
     'address_2' => '', 
     'city'  => 'Chennai', 
     'state'  => 'TN', 
     'postcode' => '12345', 
     'country' => 'IN' 
    ); 

$order->add_product(get_product('12'), 1); //(get_product with id and next is for quantity) 
$order->set_address($address, 'billing'); 
$order->set_address($address, 'shipping'); 
$order->calculate_totals(); 

对于所有你可以在命令对象调用,以后你执行下面的代码获取您的订单

var_dump(get_class_methods($order)); 

这将列出所有可用的功能功能的完整列表Woocommerce Order对象。

希望这回答你的问题

+1

非常感谢您的帮助。尽管如此,案件更为特殊。在此过程中,用户可以访问标准的WooCommerce结账页面,该页面将加载form-checkout.php模板。 在此脚本中,有几个“do_action”调用,并且加载了新用户,新送货地址,付款方式,送货单和结束按钮。 我不知道如何做,一旦用户按下该按钮,WooCommerce的“class-wc-checkout.php”类的“create_order”功能将运行订单更新,而不是创建一个新的。 – 2014-11-06 10:15:37

0

最后的解决方案是,一旦订单在“thankyou.php” WooCommerce页面进行了更新值。

更新如下:

$item = $order->get_items('line_item'); 
$data = (array) WC()->session->get('_lm_product_data'); 

wc_update_order_item_meta(key($item), __('Name', 'hpwallart'), $data['_name']); 
wc_update_order_item_meta(key($item), __('Width', 'hpwallart'), $data['_width']); 
wc_update_order_item_meta(key($item), __('Height', 'hpwallart'), $data['_height']); 

此前,在这个过程中的一步,我已经保存在session变量“_lm_product_data”信息感兴趣我的最后一步。

0

嗨Jorge我知道它有点晚,但我认为这将满足您的需求比您的解决方案稍好一点。其实这是根据woocommerce做到这一点的推荐方式。即使他们由于某种原因未达到“谢谢”模板,它也会起作用。 (例如,没有完成付款流程)

在我的情况下,我必须这样做才能添加我的客户需要的附加信息,对于该特定购买,所以我决定将其附加到订单元。

基本上,您必须将您的自定义函数附加到创建订单时执行的挂钩。这是使用该插件已经提供这样你就可以创造秩序的过程中做你自己的事情挂钩的的exaple:

/** 
* Update the order meta with field value 
*/ 
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta'); 

function my_custom_checkout_field_update_order_meta($order_id) { 
    if (! empty($_POST['my_field_name'])) { 
     update_post_meta($order_id, 'My Field', sanitize_text_field($_POST['my_field_name'])); 
    } 
} 

来源:http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/

在这个例子中,他们都增加一个额外的自定义字段(可以根据需要购买产品),但这部分代码可以用于您的情况,只需将您的定制产品信息传递给结帐表单子目录(一个选项可以添加隐藏的输入与样式=“显示:无”通过JavaScript或在eplugin提供的PHP模板)。

相关问题