通过自定义功能从WooCommerce动态获取优惠券代码 [英] Get a coupon code dynamically from WooCommerce in a custom function

查看:121
本文介绍了通过自定义功能从WooCommerce动态获取优惠券代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考



因此,您无需在函数中定义任何优惠券代码。



整个代码:

  //向Admin coupon设置页面
添加自定义复选框add_action('woocommerce_coupon_options','add_coupon_option_checkbox',10);
函数add_coupon_option_checkbox(){
woocommerce_wp_checkbox(array(
'id'=>'items_mandatory',
'label'=> __('Force specific items',' woocommerce'),
'description'=> __('将此优惠券强制用于特定商品。','woocommerce'),
'desc_tip'=> false,
))) ;
}

//保存管理员优惠券设置页面中的自定义复选框值
add_action(``woocommerce_coupon_options_save',save_coupon_option_checkbox',10,2);
函数save_coupon_option_checkbox($ post_id,$ coupon){
update_post_meta($ post_id,'items_mandatory',isset($ _POST ['items_mandatory']))?'是':'否');
}

add_action(‘woocommerce_check_cart_items’,’mandatory_coupon_for_specific_items’);
function required_coupon_for_specific_items(){
$ targeted_ids = array(37); //目标产品ID(在此数组中)
$ applied_coupons = WC()->购物车-> get_applied_coupons();
$ coupon_applied = false;

if(sizeof($ applied_coupons)> 0){
//遍历应用的优惠券
foreach($ applied_coupons作为$ coupon_code){
$ coupon =新的WC_Coupon($ coupon_code);
if($ coupon-> get_meta(‘items_mandatory’)===‘是’){
$ coupon_applied = true;
休息时间;
}
}
}

//遍历购物车项目
foreach(WC()-> cart-> get_cart()as $ cart_item){
//检查购物车中已定义产品ID和已应用优惠券的价格
if(in_array($ cart_item ['product_id'],$ targeted_ids)&&!$ coupon_applied){
wc_clear_notices(); //清除所有其他通知

//避免结帐时显示错误通知
wc_add_notice(sprintf('产品%s需要结帐的优惠券。',$ cart_item ['data ']-> get_name()),'error');
休息时间; //停止循环
}
}
}

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





在结帐中:




With reference to "Allow specific products to be purchase only if a coupon is applied in Woocommerce" answer code to one of my previous questions, the coupon code is hardcoded in the function and need to match from an existing coupon code in Woocommerce.

But I would like to pick the coupon dynamically from Woocommerce.

How can get the coupon dynamically from woocommerce?

解决方案

The following code extends my previous answer and will add a checkbox in WooCommerce > coupon section enabling any coupon code to be "mandatory" for specific defined items:

So this way you will not have to define any coupon code in a function.

The whole code:

// 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
        }
    }
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

And in checkout:

这篇关于通过自定义功能从WooCommerce动态获取优惠券代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆