首页 > 解决方案 > 结帐后以编程方式添加运输方式 - 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();

}

这是另一个函数中的工作代码,所以我一定有其他错误,但不确定是什么。

如果有人可以给我提示,请提前感谢!

标签: phpwordpresswoocommerceshipping

解决方案


您需要增加 的优先级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 );


推荐阅读