向woocommerce代码段中添加其他检查 [英] Add additional check to a woocommerce snippet

查看:55
本文介绍了向woocommerce代码段中添加其他检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的下面的代码,可以正常工作,但尚未完全完成.目前,它只是检查购物车中添加的产品是否具有tech装运类别,然后应用静态的$ 5折扣.但是,我要实现的是添加一个附加要求. 我网站上的大多数产品均未设置运输等级,其余产品均设置为技术运输等级".我要执行以下操作:

Here's my code below which works but it's not yet fully completed. Currently it just checks if an added product in the cart has tech shipping class then apply a static $5 discount. However, what I want to achieve is to add an additional requirement. Most of my products on my website are with NO set shipping class and the rest are set with 'tech shipping class'. I want to do the following:

仅当客户添加一个或多个(没有关系)没有分配运输类别的产品时,才应提供5美元的折扣.如果购物车中只有tech shipping class件物品,则不应给予折扣.如果存在不设定类的产品,而无论商品的数量是tech shipping class,则请使用$ 5的静态折扣.

The discount of $5 should only be given if the customer add one or more (it doesn't matter) of products with no shipping class assigned to them. Discount should NOT be given if there are only tech shipping class items in the cart. If there are products with no set class and tech shipping class regardless of the quantity of the items then apply a static $5 discount.

我似乎不清楚如何检查购物车是否还有其他物品,更具体地说是NO set class + tech shipping class的物品,然后应用该折扣. 任何帮助是极大的赞赏.谢谢!

It seems unclear to me how to check if cart has other items and more specifically items with NO set class + tech shipping class then to apply that discount.. Any help is greatly appreciated. Thanks!

add_filter( 'woocommerce_package_rates', 'adjustment_in_rates_of_product_with_shipping_class', 12, 2 );

function adjustment_in_rates_of_product_with_shipping_class( $available_shipping_methods, $package ) {

   // Shipping class slug to be eligible for a discount
    $shipping_class = array(
        'tech',
    );

   // Discount
    $discount = 5;

    // Enter the Shipping Method Value
    $shipping_services = array(
       'flat_rate:2',
    );

    $shipping_class_exists = false;
    foreach(WC()->cart->get_cart_contents() as $key => $values) {
        if ( in_array($values['data']->get_shipping_class() , $shipping_class) ) {
            $shipping_class_exists = true;
            break;
        }
    }

    if ($shipping_class_exists) {
        foreach ($available_shipping_methods as $key => $value) {
            if ( in_array($value->get_id() , $shipping_services) ) {
                $available_shipping_methods[$key]->cost -= $discount;
            }
        }
    }

    return $available_shipping_methods;
}

推荐答案

替换此

if ( in_array($values['data']->get_shipping_class() , $shipping_class) )

对于

if ( $values['data']->get_shipping_class() == '' )

我认为,通过此修改,您的代码将检查购物车中是否至少有一种产品没有运输类别,在这种情况下,请应用折扣.

I think that with this modification your code will check if there is at least one product in the cart without shipping class and in this case apply the discount.

这篇关于向woocommerce代码段中添加其他检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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