如何解决使用IF .. ElseIF等优化Woocommerce的小片段代码
在将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;
}
解决方法
我会将您的前两个循环合并为一个循环,因为没有理由将数据循环两次。像这样
$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;
}
}
,
更新了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文件中。经过测试,可以正常工作。
注意:保存此代码后,您应该清空您的代码。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。