在WooCommerce网站上,我出售一张礼券,客户可以在那里输入他想要赠送自己的金额,我希望在订单支付后,WooCommerce促销代码会与相应的金额一起创建.

这是我想出来的代码,但似乎根本不起作用.

add_action('woocommerce_order_status_completed', 'custom_promo_code_creation', 10, 1);
function custom_promo_code_creation($order_id) {
    $order = wc_get_order($order_id);
    $specific_product_id = 2081;
    $specific_product_price = 0;

    foreach ($order->get_items() as $item_id => $item) {
        $product_id = $item->get_product_id();
        if ($product_id === $specific_product_id) {
            $specific_product_price = $item->get_total();
            break;
        }
    }

    if ($specific_product_price > 0) {
        $coupon_code = generate_unique_voucher_code();
        $expiration_date = strtotime('+3 months'); // Set the expiration to 3 months from now

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

        if ($new_coupon_id) {
            update_post_meta($new_coupon_id, 'discount_type', 'fixed_cart');
            update_post_meta($new_coupon_id, 'coupon_amount', $specific_product_price);
            update_post_meta($new_coupon_id, 'individual_use', 'yes');
            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', 1);
            update_post_meta($new_coupon_id, 'expiry_date', $expiration_date);
        }
    }
}

订单正在正常进行,但没有创建优惠券

推荐答案

首先,请按照:How to debug in WooCommerce 3+中的说明启用WP_DEBUG

我以正确的方式重写了您的代码,使用了WooCommerce CRUD方法,而不是使用WordPress函数(旧的过时方法).我添加了一些调试点,以允许您判断一切是否正常工作:

add_action( 'woocommerce_order_status_completed', 'custom_promo_code_creation', 10, 2 );
function custom_promo_code_creation( $order_id, $order ) {
    $targeted_product_id = 2081;
    $item_total = 0;

    foreach ( $order->get_items() as $item ) {
        $product_id = $item->get_product_id();
        if ( in_array( $targeted_product_id, [$item->get_product_id(), $item->get_variation_id()]) ) {
            $item_total = $item->get_total();
            error_log('Item Found with a total amount of '.$item_total); // For testing
            break;
        }
    }

    if ( $item_total > 0 ) {
        $coupon_code = generate_unique_voucher_code();
        $coupon = new WC_Coupon();
        $coupon->set_code( $coupon_code );
        $coupon->set_status( 'publish' );
        $coupon->set_amount( $item_total );
        $coupon->set_discount_type( 'fixed_cart' );
        $coupon->set_description( sprintf(__('Generated coupon code from order #%s'), $order_id ) );
        $coupon->set_individual_use( 1 );
        $coupon->set_usage_limit( 1 );
        $coupon->set_date_expires( strtotime('+3 months') ); 
        $coupon->save();

        error_log('Generated coupon code: '.$coupon_code); // For testing
        error_log(print_r($coupon, true)); // For testing
    }
}

应该能行得通.

一旦生成优惠券,您就可以从代码中删除所有3行ERROR_LOG()代码,并禁用Back WP_DEBUG.

Php相关问答推荐

Laravel迁移返回SQL错误,但SQL有效且工作正常

如何隐藏x轴图表上的值

通过访问单个产品页面更新WooCommerce产品元数据

有没有可能从composer 过时的输出中隐藏不需要的主要版本?

laravel中获取的数据会自动更改时间戳值

如何在汇总表构建器中操作LALAVEL细丝Sum::Make()值

设置WordPress最近的帖子小部件按修改日期排序

如何使用PHP从TXM(XML)标记中读取特定属性

curl_close()未从PHP 8开始写入CURLOPT_COOKIEJAR会话文件

无法允许元素(img:'src')与symfony html-sanitizer(symfony6.3 php 8.2)

如何使用 AJAX、PHP 和 MYSQL 在 Select 项目时在文本框中填充数据

WooCommerce:如果订单总数为零,则隐藏本地取货运输选项

在 DateTime 或 DateTimeImmutable 对象上调用modify() 时是否必须使用空格?

使用 Composer 找不到 Google APIClient 类

通过存储库检索 Blade 上的单值数据 出错

PHP 中的curl -F request=@file.xml

htaccess Rewriterule 无法以任何方式工作

在WooCommerce购物车和 checkout 页面中更改总计文本

如何在自定义消息中包含 WooCommerce 选定的产品变体详细信息

如何将文件直接上传到 Cloudinary 而不先保存在 public 文件夹中