【问题标题】:Change woocommerce status based on different shipping and payment methods根据不同的运输和付款方式更改 woocommerce 状态
【发布时间】:2021-10-26 19:00:15
【问题描述】:

当客户选择特定交付选项和特定付款时,我遇到了一些问题。目前我们正在使用 bacs 和本地支付网关方法来收款,我们有本地取货和快递选项。此外,对于每个交付选项,我们都有 woocommerce 的自定义状态。当有人要求快递,他想用 bacs 付款时,就会出现问题。我们尝试为每个组合分配自定义状态,但代码不起作用,因为它为代码中的每个选项选择了相同的自定义状态。

add_action( 'woocommerce_thankyou', 'express_shipping_update_order_status', 10, 1 );
function express_shipping_update_order_status( $order_id ) {
    if ( ! $order_id ) return;
    
    $search_rm = 'Despacho Express Todo Santiago (Excluye Padre Hurtado. Recibe al siguiente día hábil)'; 
    $search_estoril = 'Retiro en Tienda Estoril';
    $search_vina = 'Retiro en Tienda Reñaca';
    
    $order = wc_get_order( $order_id );
    $payment_method=$order->get_payment_method();

    foreach($order->get_shipping_methods() as $shipping_item ){
    if( strpos( $shipping_item->get_method_title(), $search_rm ) !== false && $payment_method == "bacs"){
            $order->update_status('check-payment');
            break;
        } else {
            $order->update_status('express-rm');
            break;
        }

if( strpos( $shipping_item->get_method_title(), $search_estoril ) !== false && $payment_method == "bacs"){
            $order->update_status('check-payment');
            break;
        } else {
            $order->update_status('retiro-rm');
            break;
        }

        if( strpos( $shipping_item->get_method_title(), $search_vina ) !== false && $payment_method == "bacs"){
            $order->update_status('check-payment');
            break;
        } else {
            $order->update_status('retiro-vina');
            break;
        }
    }
}

这是结果:

有什么办法可以解决这个问题吗?谢谢!

【问题讨论】:

  • 您使用的是foreach($order->get_shipping_methods()。一个订单可以包含多少种不同的运输方式?
  • 我们有 3 种不同的送货方式和 2 种本地取货(针对不同的商店),但它们都在送货方式列表中。

标签: php woocommerce


【解决方案1】:

由于您使用自定义运输方式和自定义订单状态,因此很难给出适当的答案。

但是,如果您运行以下代码并在 if 条件中应用调试信息,您应该会得到想要的结果。

一个提示。逐步构建您的代码并同时对其进行测试,请参阅debugging in WooCommerce 与多个 if 和 else 条件,而您实际上无法确定哪里出了问题

通过代码中添加的注释标签进行解释:

function action_woocommerce_thankyou( $order_id ) {
    // Get $order object
    $order = wc_get_order( $order_id );

    // Get payment method
    $payment_method = $order->get_payment_method();

    // Get shipping method
    $shipping_method = $order->get_shipping_method();
    
    // DEBUGGING PURPOSES. Delete after testing
    echo 'DEBUG: Shipping method = ' . $shipping_method;

    // Compare payment method
    if ( $payment_method == 'bacs' ) {
        // Compare shipping method
        if ( $shipping_method == 'my_shipping_method_copy_pasted_from_the_debug_information' ) {
            $order->update_status( 'my-custom-order-status' );
        } else {
            // etc..
        }
    }
}
add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 );

【讨论】:

  • 谢谢!当用户选择 bacs 时,我完成了更改状态,但遗憾的是,我找不到在数据进入支付网关之前更改状态的方法。我将在选择某些状态和某些付款方式时,查找X秒后更改状态的解决方案。 span>
猜你喜欢
  • 2021-02-27
  • 2018-04-27
  • 2019-02-07
  • 2014-08-06
  • 1970-01-01
  • 2019-07-26
  • 2018-10-28
  • 2018-08-30
相关资源
最近更新 更多