php – WooCommerce – 基于子类别的条件购物车计算

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – WooCommerce – 基于子类别的条件购物车计算脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我有非常具体的项目,我需要一些不同的购物车规则.我找不到关于如何实现此目的的插件或任何其他资.

我有子类别1(即表格)和子类别2(即主席).用户只能从子类别表中添加1个产品,这些产品是强制性的,并且可以从子类别的主席中添加尽可能多的产品.

我需要下一条规则:如果用户还从子类别主席添加了产品,则从子类别表格产品中减去子类别主席产品的总价格.在这种情况下,如果价格将是< 0,然后将价格设为0. 有谁知道如何使用标准的wordpress Woocommerce做到一点

解决方法

这可以做到这一点,根据您的子类别要求和计算在购物车中添加折扣……

代码

add_action( 'woocommerce_cart_calculate_fees','table_chairs_cart_discount',10,1 );
function table_chairs_cart_discount($cart_object) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // InITializing VARiables
    $chairs_total = 0;
    $table_total = 0;
    $discount = 0;

    // Iterating through each cart item
    foreach($cart_object->get_cart() as $item_key => $item):

        $item_line_total = $item["line_total"]; // Item total PRice (price x quantity)

        // Chairs subcategory items
        if(has_term('chairs','product_cat',$item['product_id']))
            $chairs_total += $item_line_total;

        // Table subcategory items
        if(has_term('table',$item['product_id']))
            $table_total += $item_line_total;

    enDForeach;

    // ## CALCULATIONS ##
    if( $table_total <= $chairs_total &amp;& $chairs_total > 0 ) 
        $discount -= $table_total;
    elseif ($chairs_total > 0) 
        $discount -= $chairs_total;

    // Adding the discount
    if ($discount != 0)
        $cart_object->add_fee( __( 'Chairs discount','woocommerce' ),$discount,false );
        // Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
}

代码位于活动主题(或主题)的function.PHP文件中.或者也可以在任何插件PHP文件中.

相关回答:Discount for Certain Category Based on Total Number of Products

脚本宝典总结

以上是脚本宝典为你收集整理的php – WooCommerce – 基于子类别的条件购物车计算全部内容,希望文章能够帮你解决php – WooCommerce – 基于子类别的条件购物车计算所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。