WooCommerce 中特定运输类别、最低金额和地理定位国家/地区的购物车消息

如何解决WooCommerce 中特定运输类别、最低金额和地理定位国家/地区的购物车消息

我尝试使 Cart Message for a specific shipping class and a minimal cart total in Woocommerce 中的代码与我最近的代码 Free shipping cart message for a specific shipping class,a minimal amount and a specific Country in Woocommerce 共存,并附加多个条件:

add_action( 'woocommerce_before_calculate_totals','cart_items_shipping_class_message',20,1 );
function cart_items_shipping_class_message( $cart ){
    if ( ! is_cart() || ( is_admin() && ! defined('DOING_AJAX') ) )
        return;    
    $shipping_class = 'supplier';
    $min_amout      = 20;
    $free_shipping  = 50;
    $amout_incl_tax = 0;
    foreach( $cart->get_cart() as $cart_item ){     
        if( $cart_item['data']->get_shipping_class() === $shipping_class ){            
            $amout_incl_tax += $cart_item['line_subtotal'] + $cart_item['line_subtotal_tax'];
        }       
    }
    if( $amout_incl_tax > 0 && $amout_incl_tax < $min_amout && is_cart() && WC_Geolocation::geolocate_ip()['country'] === 'IT' ){               
        remove_action( 'woocommerce_proceed_to_checkout','woocommerce_button_proceed_to_checkout',20 );       
        wc_add_notice( sprintf( '<p><strong><font color="#cc1602">20 € of minimum amount on products SUPPLIER</font></strong></p><a href="/brand/supplier-slug" class="button">Add %s of products</a><span class="sub-button">' . wc_price($free_shipping - $amout_incl_tax) . 'for free shipping!</span>',wc_price($min_amout - $amout_incl_tax) ),'error' );
    }
    elseif( $amout_incl_tax > 0 && $amout_incl_tax < $min_amout && is_cart() && ! WC_Geolocation::geolocate_ip()['country'] === 'IT' ){             
        remove_action( 'woocommerce_proceed_to_checkout',20 );       
        wc_add_notice( sprintf( '<p><strong><font color="#cc1602">20 € of minimum amount on products SUPPLIER</font></strong></p><a href="/brand/supplier-slug" class="button">Add %s of products</a>','error' );
    }
    elseif( $amout_incl_tax >= $min_amout && $amout_incl_tax < $free_shipping && is_cart() && WC_Geolocation::geolocate_ip()['country'] === 'IT' ){             
        wc_add_notice( sprintf( '<p><strong><font color="green">FREE SHIPPING on products SUPPLIER width at least 50 €</font></strong></p><a href="/brand/supplier-slug" class="button free">Add %s of products for free shipping!</a>',wc_price($free_shipping - $amout_incl_tax) ),'notice' );
    }
}

购物车“Ajax 更新”似乎变慢了。也许可以减轻代码?

解决方法

我替换了旧的 <font> html 标签,使代码可翻译且更加模块化。

“ajax 更新”变慢是由于 remove_action() 执行导致“供应商”项目小计小于最低要求数量,并且无法改进。

您重新访问的代码:

add_action( 'woocommerce_before_calculate_totals','cart_items_shipping_class_message',20,1 );
function cart_items_shipping_class_message( $cart ){
    if ( ( is_admin() && ! defined('DOING_AJAX' ) ) || ! is_cart() )
        return;

    $shipping_class = 'supplier';
    $min_amount      = 20;
    $free_shipping  = 50;
    $items_subtotal = 0;

    // Getting non discounted "supplier" cart items subtotal including taxes 
    foreach( $cart->get_cart() as $cart_item ){
        // Targeting our defined shipping class only
        if( $cart_item['data']->get_shipping_class() === $shipping_class ){
            // Add each subtotal from our defined shipping class only
            $items_subtotal += $cart_item['line_subtotal'] + $cart_item['line_subtotal_tax'];
        }
    }

    $is_geo_ip_italy = WC_Geolocation::geolocate_ip()['country'] === 'IT';

    if( $items_subtotal > 0 && $items_subtotal < $min_amount ){
        remove_action( 'woocommerce_proceed_to_checkout','woocommerce_button_proceed_to_checkout',20 );

        $free_message =  sprintf( ' <span class="sub-button">%s %s</span>',wc_price( $free_shipping - $items_subtotal ),__('for free shipping!','woocommerce')
        );

        wc_add_notice( sprintf( '<p><strong style="color:#cc1602;">%s %s</strong></p><a href="%s" class="button">%s</a>%s',wc_price( $min_amout ),__('of minimum amount on products SUPPLIER','woocommerce'),home_url('/brand/supplier-slug'),sprintf( __('Add %s of products',wc_price($min_amount - $items_subtotal) ),$is_geo_ip_italy ? $free_message : '',),'error' );
    }
    elseif( $items_subtotal >= $min_amount && $items_subtotal < $free_shipping && $is_geo_ip_italy ){
        wc_add_notice( sprintf( '<p><strong style="color:green">%s %s</strong></p><a href="%s" class="button free">%s</a>',__('FREE SHIPPING on products SUPPLIER width at least',wc_price( $free_shipping ),sprintf( __('Add %s of products for free shipping!',wc_price($free_shipping - $items_subtotal) ),'notice' );
    }
}

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?