Sye*_*avi 12 php wordpress checkout orders woocommerce
在WooCommerce中,我想在客户成功签出后向API发送请求.它基本上是一个网站,客户在那里销售在线课程(像udemy).
当客户退房时,我想发送API请求并为该特定课程注册用户.我尝试了几个WooCommerce钩子,但没有一个对我有效.
这是我正在使用的代码:
add_action('woocommerce_checkout_order_processed', 'enroll_student', 10, 1);
function enroll_student($order_id)
{
echo $order_id;
echo "Hooked";
}
Run Code Online (Sandbox Code Playgroud)
我在激活的插件中编写此代码,为了方便起见,我目前正在使用Cash on Delivery方法.
任何人都可以指出我出错的地方,因为当我结账时,我无法看到消息"挂钩"我正在打印也不是$order_id?
我带我到成功页面,并没有显示我正在打印的这两件事.
Loi*_*tec 26
更新(从评论中询问的订单项目中获取产品ID)
也许你可以使用woocommerce_thankyou钩子,它会在订单接收的页面上显示你的回音代码,这样:
add_action('woocommerce_thankyou', 'enroll_student', 10, 1);
function enroll_student( $order_id ) {
if ( ! $order_id )
return;
// Allow code execution only once
if( ! get_post_meta( $order_id, '_thankyou_action_done', true ) ) {
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
if($order->is_paid())
$paid = __('yes');
else
$paid = __('no');
// Loop through order items
foreach ( $order->get_items() as $item_id => $item ) {
// Get the product object
$product = $item->get_product();
// Get the product Id
$product_id = $product->get_id();
// Get the product name
$product_id = $item->get_name();
}
// Output some data
echo '<p>Order ID: '. $order_id . ' — Order Status: ' . $order->get_status() . ' — Order is paid: ' . $paid . '</p>';
// Flag the action as done (to avoid repetitions on reload for example)
$order->update_meta_data( '_thankyou_action_done', true );
$order->save();
}
}
Run Code Online (Sandbox Code Playgroud)
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中.
代码经过测试和运行.
然后,您可以
WC_Abstract_Order在$order对象上使用所有类方法.
Nab*_*aiz 19
相关钩子不是“woocommerce_thankyou”钩子,而是“woocommerce_checkout_order_processed”钩子。“woocommerce_checkout_order_processed”挂钩将仅被调用一次,您无需为每个产品添加元数据并进行额外的调用来保持检查代码仅运行一次。因为,“woocommerce_thankyou”可以被调用多次,即每次加载感谢页面时。\n替换add_action('woocommerce_thankyou', 'enroll_student', 10, 1); n将 \n替换为
add_action('woocommerce_checkout_order_processed', 'enroll_student', 10, 1);\nRun Code Online (Sandbox Code Playgroud)\n并删除元代码和检查。更新后的代码是
\nadd_action('woocommerce_checkout_order_processed', 'enroll_student', 10, 1);\nfunction enroll_student( $order_id ) {\n// Getting an instance of the order object\n$order = wc_get_order( $order_id );\n\nif($order->is_paid())\n $paid = 'yes';\nelse\n $paid = 'no';\n\n // iterating through each order items (getting product ID and the product object) \n// (work for simple and variable products)\nforeach ( $order->get_items() as $item_id => $item ) {\n\n if( $item['variation_id'] > 0 ){\n $product_id = $item['variation_id']; // variable product\n } else {\n $product_id = $item['product_id']; // simple product\n }\n\n // Get the product object\n $product = wc_get_product( $product_id );\n\n}\n\n// Ouptput some data\necho '<p>Order ID: '. $order_id . ' \xe2\x80\x94 Order Status: ' . $order->get_status() . ' \xe2\x80\x94 Order is paid: ' . $paid . '</p>';\nRun Code Online (Sandbox Code Playgroud)\n}
\n您可以通过以下方式获取订单的订单项目
// Getting an instance of the order object
$order = new WC_Order( $order_id );
$items = $order->get_items();
//Loop through them, you can get all the relevant data:
foreach ( $items as $item ) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23572 次 |
| 最近记录: |