Wordpress 连接到';谢谢';他停止工作了

Wordpress 连接到';谢谢';他停止工作了,wordpress,woocommerce,hook-woocommerce,Wordpress,Woocommerce,Hook Woocommerce,几周前,我创建了一个小钩子,当订单包含特定类别的产品时,它会自动完成订单 /** * Autocomplete orders with only an 'abo' product */ add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' ); function custom_woocommerce_auto_complete_order( $order_id ) { if ( ! $or

几周前,我创建了一个小钩子,当订单包含特定类别的产品时,它会自动完成订单

/**
 * Autocomplete orders with only an 'abo' product
 */

add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) { 
if ( ! $order_id ) {
    return;
}

$order = wc_get_order( $order_id );
    // just in case

    $items = $order->get_items();

    foreach ( $items as $item ) {
        $product = $order->get_product_from_item( $item );

        // check if there's an "abo" in the order, then if there's anything else.
        if ( has_term( 'abo', 'product_cat', $product->id ) ) {
            $abo_in_order = 'true';
        }
        if ( has_term( 'goodies', 'product_cat', $product->id ) || has_term( 'revue', 'product_cat', $product->id ) || has_term( 'livre', 'product_cat', $product->id ) ) {
            $abo_alone_in_order = 'false';
        }
        else {
            $abo_alone_in_order = 'true';
        }
    }
    // if there's an 'abo' and nothing else, change the e-mail recipient to dada@sotiaf.fr
    if ( ($abo_in_order == 'true')&&($abo_alone_in_order == 'true') ) {
        $order->update_status( 'completed' );
    }
}
它过去可以正常工作,但几天来都没有,也许是在WooCommerce更新之后。整个“has_term”部分工作正常,因为它用于连接到“woocommerce_email_recipient_new_order”的另一个函数中,似乎问题来自Thankyu钩子或更新状态操作

这对任何人都有意义吗

谢谢, 乔斯


编辑:代码似乎没有系统地失败。一些订单仍然自动标记为“完成”,而另一些订单仍然处于“待定”状态。订单内容没有差异。

我认为,在检查订单中是否存在其他项目时,罪魁祸首是您的IF/ELSE条件

if ( has_term( 'goodies', 'product_cat', $product->id ) || has_term( 'revue', 'product_cat', $product->id ) || has_term( 'livre', 'product_cat', $product->id ) ) {
     $abo_alone_in_order = 'false';
}
else {
     $abo_alone_in_order = 'true';
}
如果订单中的最后一个项目不在这些类别中,则条件将为“true”,即使之前的一个项目将触发它为false

下面是一个包含3种产品的订单示例

  • 产品1(abo)
  • 产品2(评论)
  • 产品3(玉米饼)
产品3将错误地触发您当前的状态为“真”,即使产品2的存在意味着产品1 abo项目并非唯一。因此,调整这一点并更新代码的其余部分将导致以下结果:

add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) { 
    if ( ! $order_id ) {
        return;
    }

    // start with assumption that there is no ABO in order
    $abo_in_order = false;

    // start with assumption that ABO is alone in order
    $abo_alone_in_order = true;


    $order = wc_get_order( $order_id );


    $items = $order->get_items();

    foreach ( $items as $item ) {
        $product = $item->$item->get_product();

        // check if there's an "abo" in the order, then if there's anything else.
        if ( has_term( 'abo', 'product_cat', $product->get_id() ) ) {
            $abo_in_order = true;
        }
        // if other categories exist in order abo is not alone
        if ( has_term( array( 'goodies', 'revue', 'livre' ), 'product_cat', $product->get_id() ) ) {
            $abo_alone_in_order = false;
        }
    }
    // if there's an 'abo' and nothing else, change order status
    if ( ( $abo_in_order && $abo_alone_in_order ) {
        $order->update_status( 'completed' );
    }
}

嗯,
woocommerce\u thankyou
仍然在那里,所以应该仍然会开火。而且,
$product->id
应该可以工作,即使它在WC3.0中已经被弃用,并且应该被
$product->get_id()
替换。并接受一个术语数组,因此不需要使用
语句。所有这些都不应该影响结果,但它们会使代码更干净。哦,谢谢,Helga,很高兴知道。我有点厌倦了对代码进行更改,因为我在这方面相当笨拙,但如果我找到了罪魁祸首,变得更加自信,我肯定会做这些更改。谢谢,这段代码确实更好,但正如我所说,if/ELSE部分可以工作(因为没有任何其他类别)在另一个函数中,它在相同的条件下做了一些不同的事情。另外,以前的代码也可以工作,这就是为什么我想知道钩子或更新状态操作是否在最近的WooCommerce更新后不再以相同的方式工作。
$order->update_status()
仍然存在。我真的认为您的二进制IF/ELSE条件导致了问题,因为它将根据订单中的最终项目返回订单的状态,这可能是正确的,也可能不总是正确的。好的,我已经做了更改,正在交叉所有手指。:-)谢谢你的时间,我会把结果告诉你的。嗨,海尔加,经过几周又几周的观察订单,试图找出为什么一些自动完成,而另一些没有,我仍然看不出原因可能是什么。然而,我刚刚意识到这种特殊订单的装运方式总是准确的。我是否可以检查所选的装运方法,并使用该方法而不是我在订单中检查项目类别的“复杂”方法?