php - 检查 WooCommerce 优惠券是否已存在

标签 php wordpress woocommerce coupon orders

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

我的问题是,每次我刷新或重新打开装箱单时,都会使用相同的优惠券代码生成新的优惠券。所以我想实现一个规则,检查 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' );   
        */
    }
}

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

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

感谢您的帮助!

最佳答案

更新(添加了 WooCommerce 3 兼容性)

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

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

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();

    $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;

    // Loop through order items
    foreach ($order->get_items() as $item_id => $item) {
        $product = version_compare( WC_VERSION, '3.0', '<' ) ? $product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->id : $cart_item['data']->get_id() : $item->get_product();
        $product_id = method_exists( $product, 'get_id' ) ? $item->get_product_id() : $item['product_id'];
        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 = method_exists( $order, 'get_billing_email' ) ? $order->get_billing_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' );
        }
    }
}

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

关于php - 检查 WooCommerce 优惠券是否已存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38253877/

相关文章:

php - 使用 PHP 文件加载外部网站

php - 标准化阿拉伯语文本mysql

javascript - 使用 jQuery 显示 wordpress 帖子

mysql - 如何在两个表中找到匹配的行,第二个不存在

php - WooCommerce 按标签或描述搜索产品查询帖子

php - s3 存储桶中文件夹的大小

php - AWS PHP SDK v3 中的响应日志记录

php - 在产品详细信息页面上的 WooCommerce 中按类别 ID 获取类别 URL

mysql - 无法更改 wordpress query.php 中的查询值

php - 从 MySQL 的字符串中解析 HTML 标签