创建订单时触发功能以从父订阅中删除优惠券

问题描述

我有以下功能,我希望它在每次创建订阅订单时触发。

然后我希望它从父订阅中删除商店信用优惠券(因为续订订单将包含优惠券)。

我收到错误:

“PHP 消息:PHP 致命错误:未捕获的错误:调用未定义的方法 WC_Order_Item_Coupon::get_discount_type()”

我哪里出错了?

是否以正确的方式传入父订阅项?

awesome.com

解决方法

方法get_discount_type()属于WC_Coupon类但不属于WC_Order_Item_Coupon类。

因此,您需要在 foreach 循环中的优惠券项目中获取 WC_Coupon 的实例对象。

function remove_store_credit( $subscription ) {
    // Loop through order coupon items
    foreach ( $subscription->get_items( 'coupon' ) as $item ) {
        $coupon = new WC_Coupon( $item->get_code() ); // get an instance of the WC_Coupon Object 

        if( $coupon->get_discount_type() == "smart_coupon" ){
            $subscription->remove_coupon( $item->get_code() );
        }
    }
}
add_action('woocommerce_subscription_payment_complete','remove_store_credit',10,1);

它应该可以解决这个错误。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...