【问题标题】:How to exclude downloads using edd_store_discount function for easy digital downloads plugin如何使用 edd_store_discount 功能排除下载,以便轻松进行数字下载插件
【发布时间】:2019-08-21 03:24:29
【问题描述】:

我正在通过 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(无法嵌入我的低代表的原因)

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    我知道这个答案可能已经晚了,但我会把它放在那里以防万一其他人遇到同样的问题。

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

    【讨论】:

      猜你喜欢
      • 2013-04-03
      • 2021-09-02
      • 2022-10-13
      • 1970-01-01
      • 2013-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-14
      相关资源
      最近更新 更多