php - 结帐后以编程方式添加运输方式 - WooCommerce
问题描述
我正在尝试在结帐完成后立即插入运输方式。不幸的是,这不是很好,我不知道为什么。我在结帐后尝试了所有的钩子,但是......
这是我的代码:
add_action('woocommerce_checkout_create_order', 'action_checkout_order_processed', 10, 1);
function action_checkout_order_processed( $order ) {
$item = new WC_Order_Item_Shipping();
$item->set_method_title( "Безплатна Доставка - Speedy" );
$item->set_method_id( "speedy_shipping_method" ); // set an existing Shipping method rate ID
$item->calculate_taxes("0");
$shipping_item_id = $order->add_item( $item );
wc_add_order_item_meta($shipping_item_id, "method_id", "speedy_shipping_method");
wc_add_order_item_meta($shipping_item_id, "instance_id", "0");
wc_add_order_item_meta($shipping_item_id, "cost", "0");
wc_add_order_item_meta($shipping_item_id, "total_tax", maybe_unserialize('a:1:{s:5:"total";a:0:{}}'));
$order->calculate_totals();
$order->update_status('on-hold');
$order->save();
}
这是另一个函数中的工作代码,所以我一定有其他错误,但不确定是什么。
如果有人可以给我提示,请提前感谢!
解决方案
您需要增加 的优先级add_action
。
add_action('woocommerce_checkout_create_order', 'action_checkout_order_processed', 10, 1);
欲了解更多详情,请参阅以下链接。
https://developer.wordpress.org/reference/functions/add_action/
do_action('woocommerce_after_checkout_shipping_form', $wccs_custom_checkout_field_pro );
推荐阅读
- msal - 从具有 Kerberos 身份验证的 Web API 调用图形 API
- javascript - ES6:如何将这个对象数组简化为唯一值?
- google-apps-script - 使用 DriveApp 移动文件同步非常慢
- django - 如何将 postgresql 数据库存储在 django - 项目文件夹中?
- mysql - 使用 SSH 在 CentOS 上恢复 MYSQL 数据库时遇到问题
- c++ - 迭代的“基于范围”调用是什么
- python - 如何保持登录状态,MySQL+Python 3+ tkinter
- pyspark - 我在 pypsark 中遇到错误,指出:TypeError: 'Column' object is not callable
- android - 如何在 Angular NativeScript Android 应用程序中自定义带有动画的主启动画面?
- google-analytics - GA 通过单击 GA 事件跟踪的按钮来触发新会话