2017-11-17 10 views
1

회원이 오랫동안 회원으로있는 경우 내 사이트에서 사용자 쿠폰을 동적으로 제공합니다. 쿠폰을 생성 할 때 쿠폰에 설명을 지정하려고합니다. 그러나, 나는 docs 내가 할 수 있어야 제안과 같은 description 키와 게시물의 메타 데이터를 업데이 트하여 설명을 할당 할 수없는 것 같습니다.WooCommerce에서 쿠폰 설명 설정

$percent = 25;//DISCOUNT PERCENTAGE 
$coupon_code = 'testcoupon'; //Coupon Code 
$discount_type = 'percent'; // Type: fixed_cart, percent, fixed_product, percent_product 

//ASSIGN COUPON AND DISCOUNT PERCENTAGE 

$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);//SET DICOUNT TO BE PERCENTAGE BASED 
update_post_meta($new_coupon_id, 'coupon_amount', $percent);//SET DISCOUNT PERCENTAGE 
update_post_meta($new_coupon_id, 'individual_use', 'yes');//ONLY ONE CUPON BE USED AT A TIME 
update_post_meta($new_coupon_id, 'product_ids', ''); //INCLUDE ALL PRODUCTS 
update_post_meta($new_coupon_id, 'exclude_product_ids', '');//DO NOT EXCLUDE ANY PRODUCTS 
update_post_meta($new_coupon_id, 'usage_limit', '1');//ONE TIME USE 
update_post_meta($new_coupon_id, 'expiry_date', strtotime("+6 months")); 
update_post_meta($new_coupon_id, 'apply_before_tax', 'yes'); 
update_post_meta($new_coupon_id, 'free_shipping', 'no');//DO NOT GIVE FREE SHIPPING 

//ASSIGN DESCRIPTION TO COUPON 
update_post_meta($new_coupon_id, 'description', 'This is an example description used for the example coupon'); 

어떻게 다른 내가 설명을 추가하는 방법에 대한 이동해야합니다

현재 나는 그렇게 같은 설명을 할당하려고?

$percent = 25;//DISCOUNT PERCENTAGE 
$coupon_code = 'testcoupon'; //Coupon Code 
$discount_type = 'percent'; // Type: fixed_cart, percent, fixed_product, percent_product 
$description = __('This is an example description used for the example coupon'); 

//ASSIGN COUPON AND DISCOUNT PERCENTAGE 

$coupon = array(
    'post_title' => $coupon_code, 
    'post_content' => '', 
    'post_excerpt' => $description, // <== HERE goes the description 
    'post_status' => 'publish', 
    'post_author' => 1, 
    'post_type'  => 'shop_coupon' 
); 

$new_coupon_id = wp_insert_post($coupon); 

## …/… and so on 

또는 :

+0

나는 int를 메이드했습니다. WC_Coupon 객체와 메소드를 사용하여 쿠폰을 생성하는 업데이트를 얻는 중 ... – LoicTheAztec

+1

굉장! 고마워요! 너무 나쁘다. 대답에 두 개의 +1을 줄 수 없다. –

답변

1

쿠폰 설명

그래서 코드를 대신해야 ... post_excerpt(그러나 포스트 메타 데이터)로 포스트 데이터에 추가 할 수있다 당신은 대신 WC_Coupon Object and methods을 사용할 수 있습니다 :

// Get an instance of the WC_Coupon object 
$wc_coupon = new WC_Coupon($coupon_code); 

// Some data 
$percent = 25; // DISCOUNT PERCENTAGE 
$coupon_code = 'testcoupon'; // Coupon Code 
$discount_type = 'percent'; // Type: fixed_cart, percent, fixed_product, percent_product 
$description = __('This is an example description used for the example coupon'); // Description 

// Set the coupon data 
$wc_coupon->set_code($coupon_code); 
$wc_coupon->set_description($description); 
$wc_coupon->set_discount_type($discount_type); 
$wc_coupon->set_amount(floatval($percent)); 
$wc_coupon->set_individual_use(true); 
$wc_coupon->set_usage_limit(1); 
$wc_coupon->set_date_expires(strtotime("+6 months")); 
## $wc_coupon->apply_before_tax(true); // ==> Deprecated in WC 3+ with no replacement alternatie 
$wc_coupon->set_free_shipping(false); 

// Test raw data output before save 
var_dump($wc_coupon); 

// SAVE the coupon 
$wc_coupon->save();