问题描述
我有以下功能,我希望它在每次创建订阅订单时触发。
然后我希望它从父订阅中删除商店信用优惠券(因为续订订单将包含优惠券)。
我收到错误:
“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);
它应该可以解决这个错误。