WooCommerce 在成功结账时为每个产品执行 PHP [英] WooCommerce Execute PHP Per Product On Successful Checkout

查看:27
本文介绍了WooCommerce 在成功结账时为每个产品执行 PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为每个购买的产品(基于 ID)执行一些 PHP,只有在 WooCommerce 中成功结帐时.有谁知道这是否可行,如果可行 - 你能指出我正确的方向吗?

I need to execute some PHP for each product purchased (based on the ID), only on a successful checkout in WooCommerce. Does anyone know if this is possible, and if so - could you point me in the right direction?

谢谢!

推荐答案

您可以在 woocommerce_payment_complete 上运行一个函数.届时,您将可以访问 $order_id,然后您可以使用它来检索订单中的产品:

You could run a function on woocommerce_payment_complete. At that time you will have access to the $order_id which you can then use to retrieve the products in the order:

add_action( 'woocommerce_payment_complete', 'so_32512552_payment_complete' );
function so_32512552_payment_complete( $order_id ){
    $order = wc_get_order( $order_id );

    foreach ( $order->get_items() as $item ) {

        if ( $item['product_id'] > 0 ) {
            $_product = $order->get_product_from_item( $item );
            // do something with the product

        }
    }
}

这篇关于WooCommerce 在成功结账时为每个产品执行 PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆