检查WooCommerce优惠券是否已存在

时间:2022-04-03 04:28:10

I'm editing a packing slip, which is generated when the order is paid and being packed. The packing slip automatically adds a coupon which will be printed on it as well, if a product of a certain category is included.

我正在编辑一个装箱单,这是在订单付款和打包时生成的。如果包含特定类别的产品,装箱单会自动添加优惠券,并在其上打印。

My problem is, that every time I refresh or reopen the packing slip, a new coupon is being generated with the same coupon code. So I would like to implement a rule, that checks if the coupon_code already exists.

我的问题是,每次刷新或重新打开装箱单时,都会使用相同的优惠券代码生成新的优惠券。所以我想实现一个规则,检查coupon_code是否已经存在。

add_action( 'wpo_wcpdf_after_order_details', 'wpo_wcpdf_custom_text_categories', 10, 2 );
function wpo_wcpdf_custom_text_categories ($template_type, $order) {
    // collect categories from order items
    $order_cats = array();
    $items = $order->get_items();
    foreach ($items as $item_id => $item) {
        $product = $order->get_product_from_item($item);
        if ($product) {
            $terms = get_the_terms( $product->id , 'product_cat' );
            foreach ($terms as $term) {
                $order_cats[$term->term_id] = $term->name;
            }
        }
    }
    $target = array('Testtragen', 'Testtragetuch');
    // 
    // check for your category requirement
    if(count(array_intersect($order_cats, $target)) > 0 && $template_type == 'packing-slip'){
        $coupon_code = $order->get_order_number();
        $discount_type = 'fixed_product'; // Type: fixed_cart, percent, fixed_product, percent_product       
        $order_date = get_post_meta( $order->id, '_wcpdf_order_date', true );
        $due_date = date_i18n( get_option( 'date_format' ), strtotime( $invoice_date . ' + 60 days') );
        $email = $order->billing_email;   



        $amount = '10'; // Amount
        $discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product

        $coupon = array(
            'post_title'   => $coupon_code,
            'post_content' => '',
            'post_status'  => 'publish',
            'post_author'  => 1,
            'post_type'    => 'shop_coupon'
        );
        /*          
        $new_coupon_id = wp_insert_post( $coupon );

        // Add meta
        update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
        update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
        update_post_meta( $new_coupon_id, 'individual_use', 'no' );
        update_post_meta( $new_coupon_id, 'product_ids', '' );
        update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
        update_post_meta( $new_coupon_id, 'usage_limit', '' );
        update_post_meta( $new_coupon_id, 'expiry_date', '' );
        update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
        update_post_meta( $new_coupon_id, 'free_shipping', 'no' );   
        */
    }
}

So I would like to add an if statement before the wp_insert_post($coupon), that all this code only gets executed if the coupon with the coupon_code doesn't already exist.

所以我想在wp_insert_post($ coupon)之前添加一个if语句,只有当coupon_code的优惠券不存在时才会执行所有这些代码。

I tried : term_exists( $coupon_code, ‘coupons’)but this didn't work.

我尝试过:term_exists($ coupon_code,'coupons'),但这不起作用。

Thanks for your help!

谢谢你的帮助!

1 个解决方案

#1


1  

Here it is a solution creating a special meta_key for the $order->id to avoid new coupon generation with the same coupon code, when refreshing or reopening the packing slip.

这是一个解决方案,为$ order-> id创建一个特殊的meta_key,以避免在刷新或重新打开装箱单时使用相同的优惠券代码生成新的优惠券。

Here is your changed code (with comment explanations):

这是您更改的代码(带注释说明):

add_action( 'wpo_wcpdf_after_order_details', 'wpo_wcpdf_custom_text_categories', 10, 2 );
function wpo_wcpdf_custom_text_categories ($template_type, $order) {
    // collect categories from order items
    $order_cats = array();
    $items = $order->get_items();
    foreach ($items as $item_id => $item) {
        $product = $order->get_product_from_item($item);
        if ($product) {
            $terms = get_the_terms( $product->id , 'product_cat' );
            foreach ($terms as $term) {
                $order_cats[$term->term_id] = $term->name;
            }
        }
    }
    $target = array('Testtragen', 'Testtragetuch');
    // 
    // check for your category requirement
    if(count(array_intersect($order_cats, $target)) > 0 && $template_type == 'packing-slip'){
        $coupon_code = $order->get_order_number();
        $discount_type = 'fixed_product'; // Type: fixed_cart, percent, fixed_product, percent_product       
        $order_date = get_post_meta( $order->id, '_wcpdf_order_date', true );
        $due_date = date_i18n( get_option( 'date_format' ), strtotime( $invoice_date . ' + 60 days') );
        $email = $order->billing_email;




        $amount = '10'; // Amount
        $discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product

        $coupon = array(
            'post_title'   => $coupon_code,
            'post_content' => '',
            'post_status'  => 'publish',
            'post_author'  => 1,
            'post_type'    => 'shop_coupon'
        );


        // @@@ We create a meta_key '_wcpdf_coupon' for this order with value 'no'
        if( empty( get_post_meta( $order->id, '_wcpdf_coupon', true) ) ) {
            add_post_meta( $order->id, '_wcpdf_coupon', 'no', true );
        }

        // @@@ if this meta_key for this order has a value = 'no' was update it to 'yes'
        if( get_post_meta( $order->id, '_wcpdf_coupon', true) == 'no' ) {

            // Now we update it to 'yes'. This avoid the coupon be reused for this order.
            update_post_meta( $order->id, '_wcpdf_coupon', 'yes');

            $new_coupon_id = wp_insert_post( $coupon );

            // Add meta
            update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
            update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
            update_post_meta( $new_coupon_id, 'individual_use', 'no' );
            update_post_meta( $new_coupon_id, 'product_ids', '' );
            update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
            update_post_meta( $new_coupon_id, 'usage_limit', '' );
            update_post_meta( $new_coupon_id, 'expiry_date', '' );
            update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
            update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
        }
    }
}

This should solve this issue…

这应该解决这个问题......

#1


1  

Here it is a solution creating a special meta_key for the $order->id to avoid new coupon generation with the same coupon code, when refreshing or reopening the packing slip.

这是一个解决方案,为$ order-> id创建一个特殊的meta_key,以避免在刷新或重新打开装箱单时使用相同的优惠券代码生成新的优惠券。

Here is your changed code (with comment explanations):

这是您更改的代码(带注释说明):

add_action( 'wpo_wcpdf_after_order_details', 'wpo_wcpdf_custom_text_categories', 10, 2 );
function wpo_wcpdf_custom_text_categories ($template_type, $order) {
    // collect categories from order items
    $order_cats = array();
    $items = $order->get_items();
    foreach ($items as $item_id => $item) {
        $product = $order->get_product_from_item($item);
        if ($product) {
            $terms = get_the_terms( $product->id , 'product_cat' );
            foreach ($terms as $term) {
                $order_cats[$term->term_id] = $term->name;
            }
        }
    }
    $target = array('Testtragen', 'Testtragetuch');
    // 
    // check for your category requirement
    if(count(array_intersect($order_cats, $target)) > 0 && $template_type == 'packing-slip'){
        $coupon_code = $order->get_order_number();
        $discount_type = 'fixed_product'; // Type: fixed_cart, percent, fixed_product, percent_product       
        $order_date = get_post_meta( $order->id, '_wcpdf_order_date', true );
        $due_date = date_i18n( get_option( 'date_format' ), strtotime( $invoice_date . ' + 60 days') );
        $email = $order->billing_email;




        $amount = '10'; // Amount
        $discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product

        $coupon = array(
            'post_title'   => $coupon_code,
            'post_content' => '',
            'post_status'  => 'publish',
            'post_author'  => 1,
            'post_type'    => 'shop_coupon'
        );


        // @@@ We create a meta_key '_wcpdf_coupon' for this order with value 'no'
        if( empty( get_post_meta( $order->id, '_wcpdf_coupon', true) ) ) {
            add_post_meta( $order->id, '_wcpdf_coupon', 'no', true );
        }

        // @@@ if this meta_key for this order has a value = 'no' was update it to 'yes'
        if( get_post_meta( $order->id, '_wcpdf_coupon', true) == 'no' ) {

            // Now we update it to 'yes'. This avoid the coupon be reused for this order.
            update_post_meta( $order->id, '_wcpdf_coupon', 'yes');

            $new_coupon_id = wp_insert_post( $coupon );

            // Add meta
            update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
            update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
            update_post_meta( $new_coupon_id, 'individual_use', 'no' );
            update_post_meta( $new_coupon_id, 'product_ids', '' );
            update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
            update_post_meta( $new_coupon_id, 'usage_limit', '' );
            update_post_meta( $new_coupon_id, 'expiry_date', '' );
            update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
            update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
        }
    }
}

This should solve this issue…

这应该解决这个问题......