【问题标题】:How to count custom field as extra fees in WooCommerce cart?如何将自定义字段计为 WooCommerce 购物车中的额外费用?
【发布时间】:2015-10-18 00:28:37
【问题描述】:

我对在 WooCommerce 的购物车中计数有疑问。 我想为每个产品添加一个处理费用字段,并大幅计算购物车中的费用总额。 根据我的研究,我在我的产品中创建了一个领域。 Demo-1

我的下一步是在我的购物车中统计这个字段。 我也在谷歌搜索过这个问题,但我只能找到一些解决方案(Wordpress: Add extra fee in cart)来计算固定费用而不是戏剧性的功能。
Demo-2

// Display Fields
  add_action( 'woocommerce_product_options_general_product_data',      'woo_add_custom_general_fields' );

  // Save Fields
  add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );

  function woo_add_custom_general_fields() {

    global $woocommerce, $post;

    echo '<div class="options_group">';

    // Custom fields will be created here...

    woocommerce_wp_text_input( 
    array( 
        'id'                => '_number_field', 
        'label'             => __( 'Environmental fee', 'woocommerce' ), 
        'placeholder'       => '', 
        'description'       => __( 'Enter the custom value here.', 'woocommerce' ),
        'type'              => 'number', 
        'custom_attributes' => array(
                'step'  => 'any',
                'min'   => '0'
            ) 
    )
  );

    echo '</div>';

  }


  function woo_add_custom_general_fields_save( $post_id ){


    // Number Field
    $woocommerce_number_field = $_POST['_number_field'];
    if( !empty( $woocommerce_number_field ) )
        update_post_meta( $post_id, '_number_field', esc_attr( $woocommerce_number_field ) );


  }


  add_action( 'woocommerce_cart_calculate_fees','endo_handling_fee' );
  function endo_handling_fee() {
       global $woocommerce;

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

       $fee = 5.00;
       $woocommerce->cart->add_fee( 'Handling', $fee, true, 'standard' );
  }

如何修改功能来计算每个产品的费用,我创建的自定义字段在小计栏中提供了哪个值?

现在,我正在尝试以下代码。 我认为关键是如何抓住产品的价值,把价值变成一个变量。

add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');
function add_custom_fees( WC_Cart $cart ){
$fees = 0;

foreach( $cart->get_cart() as $item ){
   $fees += $item[ 'quantity' ] * 0.08; 
}

if( $fees != 0 ){
    $cart->add_fee( 'Handling fee', $fees);
}
}

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    您需要的函数是 get_post_meta 来获取自定义字段的值。

    $prod_fee = get_post_meta($item['product_id'] , '_number_field', true);
    

    然后您可以将其累积并显示为综合费用。

    【讨论】:

    • 嗨 Anfelipe,感谢您的回复,我已经尝试了您的示例。它不起作用,而且似乎没有从产品中获得价值。 $prod_fee = get_post_meta($item['product_id'] , '_number_field', true); $prod_fee = 5; //这个例子有效..
    • 您是如何实施该解决方案的? @Mitul 的回答是错误的,因为 $item['product_id'] 超出了foreach的范围,把$prod_fee放在foreach块里面。
    【解决方案2】:

    你需要从产品的元数据中获取产品费用,因为@Anfelipe给出了代码

    $prod_fee = get_post_meta($item['product_id'] , '_number_field', true);

    之后你需要添加条件或进行计算。

    function add_custom_fees( WC_Cart $cart ){
        $fees = 0;
        $prod_fee = get_post_meta($item['product_id'] , '_number_field', true);
        foreach( $cart->get_cart() as $item ){
           $fees += $item[ 'quantity' ] * $prod_fee ; 
        }
        if( $fees != 0 ){
            $cart->add_fee( 'Handling fee', $fees);
        }
    }
    

    【讨论】:

    • 您好 Mitul,感谢您的回复。我已将您的示例添加到我的项目中。但这不是发生在我的小计列的购物车中。我还尝试使用另一个字段,例如“_sale_price”,作为变量。结果是一样的。你可能知道哪里错了吗?再次感谢。
    • 嗨,米图尔,谢谢你的例子。它激励我实现我想要的功能。
    猜你喜欢
    • 1970-01-01
    • 2019-08-26
    • 1970-01-01
    • 2015-12-10
    • 1970-01-01
    • 1970-01-01
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多