php - Woocommerce:特定产品的强制优惠券

标签 php wordpress woocommerce product coupon

基于Make coupon field mandatory for a product category in WooCommerce回答,我正在尝试对 woocommerce 中的特定产品实现强制优惠券。

这是我的代码尝试:

add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_for_specific_items' );
function mandatory_coupon_for_specific_items() {
    $targeted_ids = array(40, 41, 42, 43, 44); // The targeted product ids (in this array)
    $coupon_code = 'summer1, summer2, summer3, summer4, summer5'; // The required coupon code

    $coupon_applied = in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() );

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

该代码非常适合强制使用优惠券,但我需要仅使用一张优惠券即可结帐产品。

使用实际代码,如果我将产品 ID 40 添加到购物车,则必须添加 5 张优惠券,summer1、summer2、summer3、summer 4 和 Summer5,否则无法结帐。我希望客户可以使用 Targeted_id 数组中列出的任何产品的任何优惠券进行结账。

换句话说,对于所有产品,优惠券的使用都是强制性的,但不一定是特定的优惠券,可以是代码中列出的任何优惠券。

出于示例目的,我列出了 5 种产品和 5 张优惠券,但实际上有 100 种产品和 100 张优惠券

提前谢谢

最佳答案

您的代码中有一些错误,请改用以下代码:

add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_for_specific_items' );
function mandatory_coupon_for_specific_items() {
    $targeted_ids  = array(40, 41, 42, 43, 44); // The targeted product ids (in this array)
    $coupon_codes  = array('summer1', 'summer2', 'summer3', 'summer4', 'summer5'); // Array of required coupon codes

    $coupons_found = array_intersect( array_filter( array_map( 'sanitize_title', $coupon_codes) ), WC()->cart->get_applied_coupons() );

    // Loop through cart items
    foreach(WC()->cart->get_cart() as $item ) {
        // Check cart item for defined product Ids and applied coupon
        if( in_array( $item['product_id'], $targeted_ids ) && empty($coupons_found) ) {
            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.', $item['data']->get_name() ), 'error' );
            break; // stop the loop
        }
    }
}

代码位于事件子主题(或事件主题)的functions.php 文件中。经过测试并有效。

关于php - Woocommerce:特定产品的强制优惠券,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66355212/

相关文章:

php - 在 PHP 中使用 ' instead of "真的有影响吗

php - mysql - 更新、插入或删除行值列表

php - 设置基于 PHP 定时器的函数

php - 页面未从正确的类别中提取?

mysql - 用于搜索和替换(需要更新)整个数据库的 SQL 查询(如果与 phpMyAdmin/MySQL - WordPress 站点中的字符串匹配)

php - 当用户未登录时将用户重定向到 WordPress 中的某个页面

php - 在 PHP 中调用任意函数(闭包)时类型提示

php - WP WooCommerce 页面产品数量

php - 有什么方法可以在 Wordpress/WooCommerce 中获取产品图片/特色图片的绝对路径

php - 警告 : sizeof(): Parameter must be an array or an object that implements Countable