有条件地应用 WooCommerce 上生成的优惠券添加到购物车

我目前正在使用重力表格(启用了推荐作为潜在客户)。我使用提交钩子根据优惠券模板(在我的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 meta

update_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;


}

料青山看我应如是
浏览 87回答 1
1回答

回首忆惘然

您需要在 WC 会话变量中设置创建的优惠券代码。那么你就可以轻松的得到它了。从 WooCommerce 3 开始,您的代码有点过时,并且存在错误。请尝试以下方法:// Early enable customer WC_Sessionadd_action( 'init', 'wc_session_enabler' );function wc_session_enabler() {&nbsp; &nbsp; if ( is_user_logged_in() || is_admin() )&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if ( isset(WC()->session) && ! WC()->session->has_session() ) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; WC()->session->set_customer_session_cookie( true );&nbsp;&nbsp; &nbsp; }&nbsp;}// Utility function that generate a non existing coupon code (as each coupon code required to be unique)function generated_counpon_code() {&nbsp; &nbsp; for ( $i = 0; $i < 1; $i++ ) {&nbsp; &nbsp; &nbsp; &nbsp; $coupon_code = strtolower( wp_generate_password( 10, false ) );&nbsp; &nbsp; &nbsp; &nbsp; // Check that the generated code doesn't exist yet&nbsp; &nbsp; &nbsp; &nbsp; if( coupon_exists( $coupon_code ) ) $i--; // continue&nbsp; &nbsp; &nbsp; &nbsp; else break; // Stop the loopp&nbsp; &nbsp; }&nbsp; &nbsp; return $coupon_code;}function coupon_exists( $coupon_code ) {&nbsp; &nbsp; global $wpdb;&nbsp; &nbsp; return $wpdb->get_var( $wpdb->prepare("SELECT COUNT(ID) FROM $wpdb->posts&nbsp; &nbsp; &nbsp; &nbsp; WHERE post_type = 'shop_coupon' AND post_name = '%s'", $coupon_code));}// Generate coupon and set code in customer WC session variablefunction after_submission () {&nbsp; &nbsp; // Get a random unique coupon code&nbsp; &nbsp; $coupon_code = generated_counpon_code();&nbsp; &nbsp; // Get an empty instance of the WC_Coupon Object&nbsp; &nbsp; $coupon = new WC_Coupon();&nbsp; &nbsp; $coupon->set_code( $coupon_code );&nbsp; &nbsp; $coupon->set_status( 'publish' );&nbsp; &nbsp; $coupon->set_discount_type( 'fixed_cart' );&nbsp; &nbsp; $coupon->set_description( __('Free Shipping & Handling') );&nbsp; &nbsp; $coupon->set_amount( 0 );&nbsp; &nbsp; $coupon->set_usage_limit( 1 );&nbsp; &nbsp; $coupon->set_free_shipping( 1 ); // <== Added back&nbsp; &nbsp; $coupon->set_individual_use( 1 );&nbsp; &nbsp; // Save coupon (data)&nbsp; &nbsp; $coupon->save();&nbsp; &nbsp; 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 cartadd_action( 'woocommerce_add_to_cart', 'apply_matched_coupons' );function apply_matched_coupons() {&nbsp; &nbsp; if ( is_cart() || is_checkout() ) return;&nbsp; &nbsp; // Load coupon code&nbsp; &nbsp; $coupon_code = WC()->session->get( 'unique_coupon' );&nbsp; &nbsp; if ( $coupon_code && ! WC()->cart->has_discount( $coupon_code ) ) {&nbsp; &nbsp; &nbsp; &nbsp; WC()->cart->apply_coupon( $coupon_code ); // Apply coupon code&nbsp; &nbsp; &nbsp; &nbsp; WC()->session->__unset( 'unique_coupon' ); // remove session variable&nbsp;&nbsp; &nbsp; }}代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。
打开App,查看更多内容
随时随地看视频慕课网APP