亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有條件地應用 WooCommerce 上生成的優惠券添加到購物車

有條件地應用 WooCommerce 上生成的優惠券添加到購物車

PHP
料青山看我應如是 2023-09-15 10:23:07
我目前正在使用重力表格(啟用了推薦作為潛在客戶)。我使用提交鉤子根據優惠券模板(在我的functions.php 中)生成隨機優惠券。我的目標是在結賬頁面將該優惠券自動應用于他們的訂單。我成功地生成了優惠券,但不太清楚如何應用它。僅供參考-我在用戶體驗中不使用購物車,因此產品頁面>結賬。如果有人可以提供一些幫助,或者為我指明正確的方向,我將不勝感激。// Generate Random Coupon  function after_submission () {$coupon_code = substr( "abcdefghijklmnopqrstuvwxyz123456789", mt_rand(0, 50) , 1) .substr( md5( time() ), 1); // Code$coupon_code = substr( $coupon_code, 0,10); // create 10 letters coupon$amount = '0'; // Amount$discount_type = 'free_shipping'; // Type: fixed_cart, percent, fixed_product, percent_product$coupon = array(    'post_title' => $coupon_code,    'post_content' => 'Free Shipping & Handling',    'post_excerpt' => 'Free Shipping & Handling',    'post_status' => 'publish',    'post_author' => 1,    'post_type'     => 'shop_coupon');$new_coupon_id = wp_insert_post( $coupon );// Add metaupdate_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', '1' );update_post_meta( $new_coupon_id, 'expiry_date', '' );update_post_meta( $new_coupon_id, 'apply_before_tax', 'no' );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', 'yes' );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', '' );       return $coupon_code;}
查看完整描述

1 回答

?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

您需要在 WC 會話變量中設置創建的優惠券代碼。那么你就可以輕松的得到它了。


從 WooCommerce 3 開始,您的代碼有點過時,并且存在錯誤。請嘗試以下方法:


// Early enable customer WC_Session

add_action( 'init', 'wc_session_enabler' );

function wc_session_enabler() {

    if ( is_user_logged_in() || is_admin() ) 

        return; 

        

    if ( isset(WC()->session) && ! WC()->session->has_session() ) { 

        WC()->session->set_customer_session_cookie( true ); 

    } 

}


// Utility function that generate a non existing coupon code (as each coupon code required to be unique)

function generated_counpon_code() {

    for ( $i = 0; $i < 1; $i++ ) {

        $coupon_code = strtolower( wp_generate_password( 10, false ) );


        // Check that the generated code doesn't exist yet

        if( coupon_exists( $coupon_code ) ) $i--; // continue

        else break; // Stop the loopp

    }

    return $coupon_code;

}


function coupon_exists( $coupon_code ) {

    global $wpdb;

    return $wpdb->get_var( $wpdb->prepare("SELECT COUNT(ID) FROM $wpdb->posts

        WHERE post_type = 'shop_coupon' AND post_name = '%s'", $coupon_code));

}


// Generate coupon and set code in customer WC session variable

function after_submission () {

    // Get a random unique coupon code

    $coupon_code = generated_counpon_code();


    // Get an empty instance of the WC_Coupon Object

    $coupon = new WC_Coupon();


    $coupon->set_code( $coupon_code );

    $coupon->set_status( 'publish' );

    $coupon->set_discount_type( 'fixed_cart' );

    $coupon->set_description( __('Free Shipping & Handling') );

    $coupon->set_amount( 0 );

    $coupon->set_usage_limit( 1 );

    $coupon->set_free_shipping( 1 ); // <== Added back

    $coupon->set_individual_use( 1 );


    // Save coupon (data)

    $coupon->save();


    WC()->session->set( 'unique_coupon', $coupon_code );

}


// Gravity Forms Referral (hook)

add_action( 'gform_after_submission_9', 'after_submission', 10, 2 );



// Add generated coupon to cart

add_action( 'woocommerce_add_to_cart', 'apply_matched_coupons' );

function apply_matched_coupons() {

    if ( is_cart() || is_checkout() ) return;


    // Load coupon code

    $coupon_code = WC()->session->get( 'unique_coupon' );


    if ( $coupon_code && ! WC()->cart->has_discount( $coupon_code ) ) {

        WC()->cart->apply_coupon( $coupon_code ); // Apply coupon code

        WC()->session->__unset( 'unique_coupon' ); // remove session variable 

    }

}

代碼位于活動子主題(或活動主題)的functions.php 文件中。經過測試并有效。


查看完整回答
反對 回復 2023-09-15
  • 1 回答
  • 0 關注
  • 129 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號