如何使用edd_store_discount函数排除下载内容以简化数字下载插件

时间:2019-08-21 03:24:29

标签: php wordpress

我正在通过API设置自定义折扣代码生成器,除了一件事之外,我都能正常工作。我不知道如何使用edd_store_discount函数排除某些产品/下载。 WordPress和EDD插件是最新的,我使用的是7.3.3 php版本。万一您需要该信息。

我最大的问题是我不太确定字段名称是什么,以及应如何为排除的下载字段保存ID。我尽可能地用Google搜索,但没有解决方案。大部分代码来自我发现的指南中的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(无法嵌入我的代表不高的原因)

1 个答案:

答案 0 :(得分:0)

我知道这个答案可能晚了,但是我只是把它放在那里,以防其他人在同一个问题上苦苦挣扎。

要排除给定产品,请使用键将它们添加为数组 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
    );