[英]How to exclude downloads using edd_store_discount function for easy digital downloads plugin
我正在通过 API 设置自定义折扣代码生成器,除了一件事,我让一切正常。 我不知道如何使用 edd_store_discount 函数排除某些产品/下载。 Wordpress 和 EDD 插件是最新的,我使用的是 7.3.8 php 版本。 以防万一您需要该信息。
我最大的问题是我不太确定字段名称是什么以及如何为排除的下载字段保存 ID。 我尽可能多地用谷歌搜索,但没有解决方案。 大多数代码是我找到的指南中的 c/p,但它也没有提到排除产品。
add_filter("wcra_zapier_callback" , "wcra_zapier_callback_handler");
function wcra_zapier_callback_handler($param){
$sendemail = $param['email']; //Grab email from post request
$email = sanitize_email( urldecode( $sendemail ) ); //sanitize it just in case
$parts = explode('@', $email); //split the email
$email = $parts[0]; //use only first part of email, before @
$email = preg_replace("/[^A-Za-z0-9 ]/", '', $email); //leave only alpha-numeric characters
//Create random discount code
function generateRandomString($length = 5) {
return substr(str_shuffle(str_repeat($x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length/strlen($x)) )),1,$length);
}
$rcode = generateRandomString();
$code = $email."-".$rcode;
//Set expiration time -- 604800 == week in seconds
$expires = time() + ( 1 * 604800 );
//create code and get it's ID
$details = array(
'code' => $code,
'name' => $email,
'status' => 'active',
'is_single_use' => 1,
// 'excluded-products[]' => array(23756,23705), This is the one thats bothering me, I tried switching up names and values but had no luck
'amount' => '100',
'expiration' => date( 'm/d/Y H:i:s', $expires ),
'type' => 'percent',
'max' => 1,
'uses' => 0
);
$id = edd_store_discount( $details, null );
//Check for error
if( ! is_numeric( $id ) ){
wp_send_json_error( ['error' => 'Failed to create discount' ] );
}
//get discount code
$discount_code = edd_get_discount_code( $id );
//Send emai
//user posted variables
$ademail = get_option('admin_email');
$message = "Hello, your special discount code is: \"".$discount_code."\"\r\n";
//php mailer variables
$to = $sendemail;
$subject = "Discount code you requested";
$headers = 'From: '. $ademail . "\r\n" .
'Reply-To: ' . $ademail . "\r\n";
//Here put your Validation and send mail
$sent = wp_mail($to, $subject, strip_tags($message), $headers);
if($sent) {
$mailstatus = "sent";
}//message sent!
else {
$mailstatus = "failed";
}//message wasn't sent
return $discount_code;
}
我希望定义一个包含 6 个产品/下载的静态列表,这些产品/下载将从这个折扣代码中排除。与管理仪表板https://puu.sh/E7H1D.png 完全一样(无法嵌入我的低代表的原因)
我知道这个答案可能晚了,但我只是把它放在那里,以防其他人遇到同样的问题。
要排除给定的产品,请将它们添加为具有键 exclude excluded-products => array($productId1, $productId2, ...)
如下所示:
$details = array(
'code' => 'YOUR-CODE,
'name' => 'DISCOUNT NAME',
'status' => 'active',
'is_single_use' => 1,
'amount' => '20',
'type' => 'percent',
'max' => 1,
'uses' => 0,
'not_global'=>1,
'product_condition'=>'all',
//an array of sample product ids to exclude
'excluded-products'=>array(5160,5130,3000),
'products'=>array(5158) //products that must be included
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.