在自定义函数中从 WooCommerce 动态获取优惠券代码

Get a coupon code dynamically from WooCommerce in a custom function

参考""我之前的一个问题的答案代码,优惠券代码在函数中是硬编码的,需要与现有优惠券匹配Woocommerce 中的代码。

但我想从 Woocommerce 动态选择优惠券。

如何从 woocommerce 动态获取优惠券?

试试这个,更改已评论:

add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_for_specific_items' );
function mandatory_coupon_for_specific_items() {
    // The targeted product ids (in this array)
    $targeted_ids   = array(37); 
    // Get applied $coupon_names
    $cart_applied_coupons = WC()->cart->get_applied_coupons()
    // Set the variable $coupon_applied to false
    $coupon_applied = false;
    // Create an array to save all coupon names
    $coupon_names = array();
    // Get the available coupons
    $args = array(
        'posts_per_page'   => -1,
        'orderby'          => 'title',
        'order'            => 'asc',
        'post_type'        => 'shop_coupon',
        'post_status'      => 'publish',
    );
    $all_coupons = get_posts( $args );
    if( !empty($all_coupons) ){
      // Loop through the available coupons
      foreach ( $all_coupons as $coupon ) {
          // Get the name for each coupon and add to the previously created array
          $coupon_name = $coupon->post_title;
          array_push( $coupon_names, $coupon_name );
      }
      // If one on the coupons is applied change the value of $coupon_applied to true
      foreach($coupon_names as $coupon_code){
        if( in_array( strtolower($coupon_code), $cart_applied_coupons) ){
          $coupon_applied = true;
          break;
        }
      }
    }


    // Loop through cart items
    foreach(WC()->cart->get_cart() as $cart_item ) {
        // Check cart item for defined product Ids and applied coupon
        if( in_array( $cart_item['product_id'], $targeted_ids ) && ! $coupon_applied ) {
            wc_clear_notices(); // Clear all other notices

            // Avoid checkout displaying an error notice
            wc_add_notice( sprintf( 'The product"%s" requires a coupon for checkout.', $cart_item['data']->get_name() ), 'error' );
            break; // stop the loop
        }
    }
}

以下代码扩展了我的 并将在 WooCommerce > 优惠券部分添加一个复选框,使任何优惠券代码成为特定定义项目的 "mandatory":

这样一来,您就不必在函数中定义任何优惠券代码了。

全部代码:

// Add a custom checkbox to Admin coupon settings pages
add_action( 'woocommerce_coupon_options', 'add_coupon_option_checkbox', 10 );
function add_coupon_option_checkbox() {
    woocommerce_wp_checkbox( array(
        'id'            => 'items_mandatory',
        'label'         => __( 'Force specific items', 'woocommerce' ),
        'description'   => __( 'Make this coupon mandatory for specific items.', 'woocommerce' ),
        'desc_tip'      => false,
    ) );
}

// Save the custom checkbox value from Admin coupon settings pages
add_action( 'woocommerce_coupon_options_save', 'save_coupon_option_checkbox', 10, 2 );
function save_coupon_option_checkbox( $post_id, $coupon ) {
    update_post_meta( $post_id, 'items_mandatory', isset( $_POST['items_mandatory'] ) ? 'yes' : 'no' );
}

add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_for_specific_items' );
function mandatory_coupon_for_specific_items() {
    $targeted_ids    = array(37); // The targeted product ids (in this array)
    $applied_coupons = WC()->cart->get_applied_coupons();
    $coupon_applied  = false;

    if( sizeof($applied_coupons) > 0 ) {
        // Loop through applied coupons
        foreach( $applied_coupons as $coupon_code ) {
            $coupon = new WC_Coupon( $coupon_code );
            if( $coupon->get_meta('items_mandatory') === 'yes' ) {
                $coupon_applied = true;
                break;
            }
        }
    }

    // Loop through cart items
    foreach(WC()->cart->get_cart() as $cart_item ) {
        // Check cart item for defined product Ids and applied coupon
        if( in_array( $cart_item['product_id'], $targeted_ids ) && ! $coupon_applied ) {
            wc_clear_notices(); // Clear all other notices

            // Avoid checkout displaying an error notice
            wc_add_notice( sprintf( 'The product"%s" requires a coupon for checkout.', $cart_item['data']->get_name() ), 'error' );
            break; // stop the loop
        }
    }
}

代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。

结帐时:

我想我会分享我的类别版本。结合使用此处发布的解决方案和此

// Add a custom checkbox to Admin coupon settings pages
add_action( 'woocommerce_coupon_options', 'add_coupon_option_checkbox', 10 );
function add_coupon_option_checkbox() {
    woocommerce_wp_checkbox( array(
        'id'            => 'items_mandatory',
        'label'         => __( 'Force specific items', 'woocommerce' ),
        'description'   => __( 'Make this coupon mandatory for specific items.', 'woocommerce' ),
        'desc_tip'      => false,
    ) );
}


// Save the custom checkbox value from Admin coupon settings pages
add_action( 'woocommerce_coupon_options_save', 'save_coupon_option_checkbox', 10, 2 );
function save_coupon_option_checkbox( $post_id, $coupon ) {
    update_post_meta( $post_id, 'items_mandatory', isset( $_POST['items_mandatory'] ) ? 'yes' : 'no' );
}


// Force Coupon codes for Woocommerce
add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_code' );
function mandatory_coupon_code() {
     // Set your product categories in the array (can be term IDs, slugs or names)
    $product_categories = array( '58');
    $applied_coupons = WC()->cart->get_applied_coupons();
    $found = false;
    
    if( sizeof($applied_coupons) > 0 ) {
        // Loop through applied coupons
        foreach( $applied_coupons as $coupon_code ) {
            $coupon = new WC_Coupon( $coupon_code );
            if( $coupon->get_meta('items_mandatory') === 'yes' ) {
                $coupon_applied = true;
                break;
            }
        }
    }
    // Loop through cart items to check for the product categories
    foreach ( WC()->cart->get_cart() as $cart_item ){
        if( has_term( $product_categories, 'product_cat', $cart_item['product_id'] ) ){
            $found = true; // cart item from the product category is found
            break; // We can stop the loop
        }
    }

    // If not found we exit
    if( ! $found ) return; // exit
    
    // Coupon not applied and product category found
    
    if( ! $coupon_applied) {
        // Display an error notice preventing checkout
        $message = __( 'The product "%s" requires a coupon for checkout.' );
        wc_add_notice( sprintf($message, $cart_item['data']->get_name() ), 'error' );
    }
}