WooCommerce 智能优惠券从哪个项目生成?

WooCommerce Smart Coupon generated from which Item?

我正在为智能优惠券编写业务扩展(pdf 打印)。

现在我需要生成的(订单的)优惠券与订单商品的关系。

例如,哪个订单项生成了优惠券? 有没有办法从优惠券中获取订单item_id

是否使用此代码获取优惠券:

$coupons = get_post_meta($order_id, 'sc_coupon_receiver_details', true));

非常感谢

我找到了解决方案,但我必须将代码放入 "woocommerce-smart-coupons.php" 文件中。

第 379 行:扩展过滤器变量

add_filter( 'generate_smart_coupon_action', array(  $this, 'generate_smart_coupon_action' ), 1, 10 );

第 4783 行:使用 $item_id

扩展变量
if( $this->is_coupon_amount_pick_from_product_price( array( $coupon_title ) ) ) {
                                            $email_to_credit[$receivers_emails[$coupon->id][0]][] = $coupon->id . ':' . $sc_called_credit_details[$item_id] . ':' . $item_id;
                                        } else {
                                            $email_to_credit[$receivers_emails[$coupon->id][0]][] = $coupon->id . ':' . $coupon->amount . ':' . $item_id;
                                        }

第4816行:从details中获取$item_id并引用到"generate_smart_coupon"方法

foreach ( $email_to_credit[$email_id] as $coupon_credit => $qty ) {
                        $coupon_details = explode( ':', $coupon_credit );
                        $coupon_title = get_the_title( $coupon_details[0] );
                        $coupon = new WC_Coupon( $coupon_title );
                        $credit_amount = $coupon_details[1];
                        $item_id = $coupon_details[2];
                        $message_index = array_search( $email_id, $email[$coupon->id], true );
                        if ( $message_index !== false && isset( $receivers_messages[$coupon->id][$message_index] ) && !empty( $receivers_messages[$coupon->id][$message_index] ) ) {
                            $message_from_sender = $receivers_messages[$coupon->id][$message_index];
                        } else {
                            $message_from_sender = '';
                        }
                        for ( $i = 0; $i < $qty; $i++ ) {
                            if ( $coupon->type != 'smart_coupon' ) continue;    // only process smart_coupon here, rest coupon will be processed by function update_coupon
                            $this->generate_smart_coupon( $email_id, $credit_amount, $order_id, $coupon, 'smart_coupon', $gift_certificate_receiver_name, $message_from_sender, $gift_certificate_sender_name, $gift_certificate_sender_email, $item_id );
                            $smart_coupon_codes = array();
                        }
                    }

第 5194 行:更改方法并扩展变量

public function generate_smart_coupon( $email, $amount, $order_id = '', $coupon = '', $discount_type = 'smart_coupon', $gift_certificate_receiver_name = '', $message_from_sender = '', $gift_certificate_sender_name = '', $gift_certificate_sender_email = '', $item_id = '' ) {
            return apply_filters( 'generate_smart_coupon_action', $email, $amount, $order_id, $coupon, $discount_type, $gift_certificate_receiver_name, $message_from_sender, $gift_certificate_sender_name, $gift_certificate_sender_email, $item_id );
        }

第 5212 行:将过滤器方法扩展到

public function generate_smart_coupon_action( $email, $amount, $order_id = '', $coupon = '', $discount_type = 'smart_coupon', $gift_certificate_receiver_name = '', $message_from_sender = '', $gift_certificate_sender_name = '', $gift_certificate_sender_email = '', $item_id = '' ) {

第 5307 行:将 $item_id 存储在 post_meta

update_post_meta( $smart_coupon_id, 'apply_before_tax', $apply_before_tax  );
                update_post_meta( $smart_coupon_id, 'free_shipping', $free_shipping );
                update_post_meta( $smart_coupon_id, 'product_categories', $product_categories  );
                update_post_meta( $smart_coupon_id, 'exclude_product_categories', $exclude_product_categories );
                update_post_meta( $smart_coupon_id, 'generated_from_order_id', $order_id );
                update_post_meta( $smart_coupon_id, 'generated_from_item_id', $item_id );