php - 有条件地为特定产品 ID 和数量自动应用优惠券

标签 php wordpress woocommerce cart coupon

我正在尝试根据产品 ID 和数量条件在我的 WooCommerce 商店中自动应用优惠券。我的最终目标是在将两 (2) 种所需产品添加到购物车时自动应用特定优惠券,并在将三 (3) 种所需产品添加到购物车时自动应用另一张优惠券。单个数量的产品不应有折扣。 以下是代码的更正版本,现在可以使用了:

add_action( 'woocommerce_before_cart', 'conditional_auto_add_coupons' );
function conditional_auto_add_coupons() {

if ( !WC()->cart->is_empty() ){

    // Define HERE your Targeted Product ID and coupons codes
    $target_pid = 103;
    $coupon1 = 'soccer-sibling-2';
    $coupon2 = 'soccer-sibling-3';

    // First cart loop: Counting number of subactegory items in cart
    foreach ( WC()->cart->get_cart() as $cart_item ){
        if( $target_pid == $cart_item['data']->id ){
            // Removes any coupons in the cart already
            WC()->cart->remove_coupons();
            if( 2 == WC()->cart->get_cart_contents_count() && !WC()->cart->has_discount( $coupon1 ) ){
                WC()->cart->remove_coupons();
                WC()->cart->add_discount( $coupon1 );
                wc_add_notice( __( 'The multiple sibling discount has been applied.', 'theme_domain' ), 'success' );
            } elseif( 3 == WC()->cart->get_cart_contents_count() && !WC()->cart->has_discount( $coupon2 ) ){
               WC()->cart->remove_coupons();
                WC()->cart->add_discount( $coupon2 );
                wc_add_notice( __( 'The multiple sibling discount has been applied.', 'theme_domain' ), 'success' );
            }
            // Recalculates Cart Totals to show correct price
            WC()->cart->calculate_totals();
        }
     }
  }
}

最佳答案

你的代码中有很多错误,它也有点过时了...... 我已经重写了您函数中的所有内容,并将其挂接到另一个 Hook 中。

这是您重新访问的代码:

add_action( 'woocommerce_before_cart', 'conditional_auto_add_coupons' );
function conditional_auto_add_coupons() {

    if ( !WC()->cart->is_empty() ){

        // Define HERE your Targeted Product ID and coupons codes
        $target_pid = 103;
        $coupon1 = 'soccer-sibling-2';
        $coupon2 = 'soccer-sibling-3';

        // First cart loop: Counting number of subactegory items in cart
        foreach ( WC()->cart->get_cart() as $cart_item ){
            if( $target_pid == $cart_item['data']->id ){
                if( 2 == WC()->cart->get_cart_contents_count() && !WC()->cart->has_discount( $coupon1 ) ){
                    WC()->cart->add_discount( $coupon1 );
                    wc_add_notice( __( 'A quantity discount of <strong>5%</strong> has been added.', 'theme_domain' ), 'success' );
                } elseif( 3 == WC()->cart->get_cart_contents_count() && !WC()->cart->has_discount( $coupon2 ) ){
                    WC()->cart->add_discount( $coupon2 );
                    wc_add_notice( __( 'A quantity discount of <strong>10%</strong> has been added.', 'theme_domain' ), 'success' );
                }
            }
        }
    }
}

此代码位于您的事件子主题(或主题)的 function.php 文件中或任何插件文件中。

这应该有效,不会让您的网站崩溃,但我还没有真正测试过,因为这是非常特殊的。

关于php - 有条件地为特定产品 ID 和数量自动应用优惠券,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41818154/

相关文章:

php - 在标题中显示页面标题 - Wordpress

php - 按属性过滤的 WooCommerce 相关产品

php - 自动向嵌入式youtube视频添加参数-正则表达式有帮助吗? (PHP,Wordpress)

css - 对背景图像的悬停效果

php - WooCommerce 订单电子邮件更改/从产品元中删除 "backordered: #"

javascript - flexslider 2 事件缩略图

php - PHP echo 源码在哪里实现的?

php - Laravel Guzzle 请求获取错误的数据库连接

PHP 特征 - 定义通用常量

php - 如何在 PHP 中使用 MaxMind 的 GeoIp2 和另一个自动加载器?