【问题标题】:PHP foreach code not producing expeted resultsPHP foreach 代码没有产生加速的结果
【发布时间】:2017-02-15 08:02:47
【问题描述】:

以下代码来自 wordpress + woocommerce 安装。它应该检查购物车中的每件商品,如果商品属于特定产品类别(类别 ID 79 和 130 加 15 欧元费用,类别 80、136、139 加 50 欧元费用),则根据类别添加额外费用。为简单起见,我将它们称为 category group 15€category group 50€

当一件物品被添加到购物车时,属于类别组 15 欧元或类别组 50 欧元,按预期添加额外费用。

将属于不同类别组的两件商品添加到购物车时,会按预期添加额外费用。

但如果将多件商品添加到购物车中,属于同一类别组,则每个类别组仅添加一项费用,这是不可取的。添加的每个项目都应产生相关的类别组费用。例如。类别组 15 欧元的 2 项应另加 2 项费用,类别组 50 欧元的 3 项应另加 3 项费用,依此类推。

代码中是否有错误或...?

function woo_add_cart_fee() {

global $woocommerce;

    foreach ($woocommerce->cart->cart_contents as $key => $values ) {
        // Get the terms, i.e. category list using the ID of the product
    $terms = get_the_terms( $values['product_id'], 'product_cat' );
        // Because a product can have multiple categories, we need to iterate through the list of the products category for a match
        foreach ($terms as $term) {
            // 79 is the ID of the category for which we want to remove the payment gateway
            if($term->term_id == '79' || $term->term_id == '130'){
             $excost = 15;
             $woocommerce->cart->add_fee('Εκτύπωση με απλό μελάνι', $excost, $taxable = false, $tax_class = '');
             } else if ($term->term_id == '80' || $term->term_id == '139' || $term->term_id == '136'){
                 $excost = 50;
                 $woocommerce->cart->add_fee('Μακέτα & εκτύπωση (απλό μελάνι)', $excost, $taxable = false, $tax_class = '');
             }
         }
    }
}

【问题讨论】:

    标签: php wordpress foreach woocommerce


    【解决方案1】:

    您需要使每个费用名称唯一。

    来自documentation

    $name - ( string ) required - 费用的唯一名称。 不能添加多个同名费用。

    这是一种方法:

    $woocommerce->cart->add_fee('Μακέτα & εκτύπωση (απλό μελάνι) ' . $i++, $excost, $taxable = false, $tax_class = '');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多