php - 自 WooCommerce 3+ 起以编程方式创建优惠券

标签 php wordpress woocommerce coupon

我通过编程方式使用此代码在 woocommerce 添加优惠券。

    if(empty($coupon_post)){

        $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', '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', '' );
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' );      
update_post_meta( $new_coupon_id, 'exclude_sale_items', 'no' );     
update_post_meta( $new_coupon_id, 'free_shipping', 'no' );      
update_post_meta( $new_coupon_id, 'product_categories', '' );       
update_post_meta( $new_coupon_id, 'exclude_product_categories', '' );       
update_post_meta( $new_coupon_id, 'minimum_amount', '' );       
update_post_meta( $new_coupon_id, 'customer_email', '' ); 
    }

但它总是说 use_limit = 1。

我添加了额外的代码来更新:

add_action( 'save_post_shop_coupon', 'my_child_after_coupon_save', 10, 3 );

function my_child_after_coupon_save( $post_id, $post, $update ) {
   update_post_meta( $post_id, 'usage_limit', '');
}

但是它首先不起作用。但是如果我在后端打开优惠券并更新而不进行任何更改。使用限制设置为无限制。

如何触发这个,我不需要打开所有优惠券。

最佳答案

自 WooCommerce 3 起,您的代码有点过时,例如不再使用 apply_before_tax 。您应该更好地使用所有可用的WC_Coupon setter methods ,用于优惠券创建。

在下面的代码中,我只使用必要的 setter 方法(与您的代码相关):

// Get an empty instance of the WC_Coupon Object
$coupon = new WC_Coupon();

// Set the necessary coupon data (since WC 3+)
$coupon->set_code( $coupon_code ); // (string)
// $coupon->set_description( $description ); // (string)
$coupon->set_discount_type( $discount_type ); // (string)
$coupon->set_amount( $coupon_amount ); // (float)
// $coupon->set_date_expires( $date_expires ); // (string|integer|null)
// $coupon->set_date_created( $date_created ); // (string|integer|null)
// $coupon->set_date_modified( $date_created ); // (string|integer|null)
// $coupon->set_usage_count( $usage_count ); // (integer)
$coupon->set_individual_use( true ); // (boolean) 
// $coupon->set_product_ids( $product_ids ); // (array)
// $coupon->set_excluded_product_ids( $excl_product_ids ); // (array)
$coupon->set_usage_limit( 0 ); // (integer)
// $coupon->set_usage_limit_per_user( $usage_limit_per_user ); // (integer)
// $coupon->set_limit_usage_to_x_items( $limit_usage_to_x_items ); // (integer|null)
// $coupon->set_free_shipping( $free_shipping ); // (boolean) | default: false
// $coupon->set_product_categories( $product_categories ); // (array)
// $coupon->set_excluded_product_categories( $excl_product_categories ); // (array)
// $coupon->set_exclude_sale_items( $excl_sale_items ); // (boolean)
// $coupon->set_minimum_amount( $minimum_amount ); // (float)
// $coupon->set_maximum_amount( $maximum_amount ); // (float)
// $coupon->set_email_restrictions( $email_restrictions ); // (array)
// $coupon->set_used_by( $used_by ); // (array)
// $coupon->set_virtual( $is_virtual ); // (array)

// Create, publish and save coupon (data)
$coupon->save();

现在要更新优惠券,您可以将以下 Hook 函数与任何 setter 方法一起使用,例如:

add_action( 'woocommerce_coupon_options_save', 'action_coupon_options_save', 10, 2 );
function action_coupon_options_save( $post_id, $coupon ) {
    $coupon->set_usage_limit( 0 );
    $coupon->save();
}

代码位于事件子主题(或事件主题)的functions.php 文件中。

关于php - 自 WooCommerce 3+ 起以编程方式创建优惠券,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66890383/

相关文章:

php - 无法在 WordPress 中创建超过两级的子菜单

php - 使用 openSSL 在 PHP 中加密(大)文件

PhpStorm - 防止数组括号对齐

html - 用 div 包装子菜单项 - WordPress

php - 如何在 WordPress 中为其他子网站共享 WP_POST

php - WordPress 查询 19000+ 产品

php - 代币必须是短暂的代币,并且在合理的时间范围内

php - 如何从mysql中的一个以外的值开始自动增加列值?

javascript - 如何从 wordpress 中删除标题 p 类?

html - Showhide CSS 不适用于 Wordpress