【问题标题】:Optimize little Woocommerce snippet code with IF .. ElseIF and such使用 IF .. ElseIF 等优化 Woocommerce 小片段代码
【发布时间】:2021-02-10 18:50:46
【问题描述】:

请你帮我优化我写的小sn-p,在将产品添加到购物车中后应用折扣tech分配了运输类别和其他没有为WordPress(Woocommerce)网站指定类别的产品。 如何优化if .. elseif 并使其整体变得更好。另外,添加break 是一个好习惯吗?我是 PHP 新手,但我目前正在学习并希望改进我的代码。或许我可以使用类似于switch 的东西?非常感谢任何帮助和示例!

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 when combined with no class products
    $shipping_class = array(
        'tech',
    );

   // Discount
    $discounteuro = 3.50;
    $discountgbp = 3.20;
    $discountusd = 4;
    $discountglobalusd = 5;

    // Enter the shipping method value
    $shipping_services = array(
       'flat_rate:5',
    );

    $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;
        }
    }

    $shipping_class_no_exists = false;
    foreach(WC()->cart->get_cart_contents() as $key => $values) {
        if ( strlen($values['data']->get_shipping_class()) == 0 ) {
            $shipping_class_no_exists = true;
            break;
        }
    }

    if ($shipping_class_exists && $shipping_class_no_exists) {
        foreach ($available_shipping_methods as $key => $value) {
            if ( in_array($value->get_id() , $shipping_services) && get_woocommerce_currency() == 'EUR' ) {
                $available_shipping_methods[$key]->cost -= $discounteuro;
        break;
            }
        elseif ( in_array($value->get_id() , $shipping_services) && get_woocommerce_currency() == 'GBP' ) {
                $available_shipping_methods[$key]->cost -= $discountgbp;
        break;
        }
        elseif ( in_array($value->get_id() , $shipping_services) && get_woocommerce_currency() == 'USD' ) {
                $available_shipping_methods[$key]->cost -= $discountusd;
        break;
       }
        else {
            $available_shipping_methods[$key]->cost -= $discountglobalusd;   
        break;
       }
        }
    }

    return $available_shipping_methods;
}

【问题讨论】:

  • @GrumpyCrouton - 我明白了,所以我将把break 留给$shipping_class_exists$shipping_class_no_exists,对吧?
  • @GrumpyCrouton - 非常感谢。除了您在下面发布的在一个if 中实现所有elseifs 的出色方法之外,您是否也看到了一种同时改进$shipping_class_exists$shipping_class_no_exists 的方法?总的来说,其余的代码好吗?再次感谢您抽出宝贵时间帮助我。非常感谢!

标签: php wordpress if-statement woocommerce shipping-method


【解决方案1】:

我会将您的前两个循环合并为一个循环,因为没有理由将数据循环两次。像这样的

$shipping_class_exists = false;
$shipping_class_no_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;
    }
    if(strlen($values['data']->get_shipping_class()) == 0) {
        $shipping_class_no_exists = true;
    }
}

我还将您的折扣转换为一个数组,以便更轻松地从您的循环中调用。

$discounts = [
    'EUR' => 3.5,
    'GBP' => 3.2,
    'USD' => 4,
    'globalusd' => 5
];

那么你的循环就会变成

if ($shipping_class_exists && $shipping_class_no_exists) {
    foreach ($available_shipping_methods as $key => $value) {
        
        //store currency type in an easier to access variable
        $currency = get_woocommerce_currency();   
        
        //create variable `$discount` defaulting to `$discounts['globalusd']`
        $discount = $discounts['globalusd'];

        if(in_array($value->get_id(), $shipping_services) && array_key_exists($currency, $discounts)) {

            //if $value->get_id()` is in `$shipping_services` and `$currency` key exists in $discounts
            //then alter $discount to the correct value
            $discount = $discounts[$currency];
        }

        //subtract discount from cost        
        $available_shipping_methods[$key]->cost -= $discount;

    }
}

【讨论】:

  • 非常感谢,但它并没有像我的代码那样做。如果送货服务flat_rate:5 和存储货币USDEURGBP 应该打折,但如果送货服务不同于它应该做globaldiscount。我对我应该对您的代码进行编辑以使其行为与我的完全一样感到困惑。谢谢
  • @Korla 对第二部分进行了更多修改,因为三元组对于我个人的口味来说有点太长了。这看起来更干净。您的评论中的问题也应该得到解决。
【解决方案2】:

更新了 2 删除了一些错误

以下优化代码应该可以解决问题(代码已注释):

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

function adjustment_in_rates_of_product_with_shipping_class( $rates, $package ) {

    // Shipping class slug to be eligible for a discount when combined with no class products
    $target_shipping_classes = array('tech');

    // Enter the shipping method value
    $shipping_rate_ids       = array('flat_rate:5');

    // Discount based on currency
    $currency_discounts      = array(
        ''      => 5,
        'EUR'   => 3.50,
        'GBP'   => 3.20,
        'USD'   => 4,
    );

    $shipping_class_found    = $other_shipping_classes = $currency_found = false;

    $woocommerce_currency    = get_woocommerce_currency();

    // Loop through cart items for the current package
    foreach( $package['contents'] as $item ) {
        $shipping_class = $item['data']->get_shipping_class(); // Get item shipping class

        if ( in_array( $shipping_class, $target_shipping_classes ) ) {
            $shipping_class_found = true;
        }
        elseif ( ! empty($shipping_class) ) {
            $other_shipping_classes = true;
        }
    }

    // When there is only items from the defined shipping classes
    if ( $shipping_class_found && ! $other_shipping_class ) {
        // Loop through available shipping rates in the current shipping package
        foreach ( $rates as $rate_key => $rate ) {
            if ( in_array($rate_key, $shipping_rate_ids) ) {
                // Loop through defined currency discounts
                foreach ( $currency_discounts as $currency => $discount ) {
                    if ( $woocommerce_currency === $currency ) {
                        $rates[$rate_key]->cost -= $discount;
                        $currency_found = true;
                        break;
                    }
                }
                // If no currency match, we get the default value (the first one)
                if ( ! $currency_found ) {
                    $rates[$shipping_rate_id]->cost -= reset($currency_discounts);
                }
            }
        }
    }

    return $rates;
}

代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

注意:您应该在保存此代码后清空您的代码。

【讨论】:

  • 感谢您花时间帮助我,但是在测试您的代码后,它根本不起作用。不知道为什么会这样,但根本没有折扣,而且不像我在原始帖子中的代码。
  • @Korla 我的错,缺少左括号和一些错误……请现在再试一次。如果这个答案回答了你的问题,你可以请accept回答,如果你喜欢/想要你也可以请upvote回答,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多