【问题标题】:Woocommerce - Adding Custom Fee Together in CartWoocommerce - 在购物车中一起添加自定义费用
【发布时间】:2019-12-02 07:06:10
【问题描述】:

我为自定义字段创建了一个功能,费用也会根据预订人数成倍增加:

// Add Custom Field to Product under General
function create_extra_fee_field() {
    $args = array(
    'id' => 'park_fee',
    'label' => __( 'Natl Park Entrance Fee', 'tranq-lsx-child' ),
    'class' => 'tranq-custom-field',
    'desc_tip' => true,
    'description' => __( 'This sets the Fee that will be added to the car.', 'tranq-lsx-child' ),
    );
    woocommerce_wp_text_input( $args );
   }
   add_action( 'woocommerce_product_options_general_product_data', 'create_extra_fee_field' );

   // Save Custom Field Data
   function save_extra_fee_field( $post_id ) {
    $product = wc_get_product( $post_id );
    $title = isset( $_POST['park_fee'] ) ? $_POST['park_fee'] : '';
    $product->update_meta_data( 'park_fee', sanitize_text_field( $title ) );
    $product->save();
   }
   add_action( 'woocommerce_process_product_meta', 'save_extra_fee_field' );

   // Add Custom Field to Cart Totals

   function woo_add_cart_fee() {

       global $woocommerce; 

       foreach( WC()->cart->get_cart() as $cart_items ){
           // Get the WC_Product object (instance)
           $product = $cart_items['data'];
           $product_id = $product->get_id(); // get the product ID
           $custom_field_value = get_post_meta( $product->get_id(), 'park_fee', true );
           $person = array_sum( $cart_items['booking']['_persons'] );
       }    
       $additional_fee_name = "Natl Park Entrance Fee";
       $extra_fee = $custom_field_value * $person;
       $addedFee = false;
       // first check to make sure it isn’t already there
       foreach ( $woocommerce->cart->get_fees() as $_fee )
       {
           if ($_fee->id == sanitize_title($additional_fee_name) )
           {
           $_fee->amount = (float) esc_attr( $extra_fee );
           }
       }
       if (!$addedFee)
       {
           $woocommerce->cart->add_fee( __($additional_fee_name, "woocommerce"),
           $extra_fee, $additional_fee_taxable );
       }
   }

   add_action( "woocommerce_before_calculate_totals", "woo_add_cart_fee" );

一种产品正在使用该自定义费用,而另一种产品未设置为 0

但当两者都添加到购物车时,费用仅显示为 0。

有没有办法把两个公园费用加在一起?

【问题讨论】:

  • 条件 !addedFee 始终为 True,因为 $addedFee = false 设置在 if 条件之前。不确定这是否是原因,但在那里有 if 条件是没有意义的。

标签: function woocommerce cart


【解决方案1】:

@Demonix,这是因为 $custom_field_value 正在被“0”值覆盖,因为函数会循环访问附加的产品。

如果您在替换$custom_field_value之前添加一个语句以检查park_fee是否为实际数字

function woo_add_cart_fee() {
    global $woocommerce;
    $custom_field_value = 0;
    foreach( WC()->cart->get_cart() as $cart_item ) {
        // Get the WC_Product object (instance)
        $product = $cart_item['data'];
        $product_id = $product->get_id(); // get the product ID

        $temp_value = get_post_meta( $product->get_id(), 'park_fee', true );
        if ( false !== $temp_value && 0 !== $temp_value && '' !== $temp_value && '0' !== $temp_value ) {
            $custom_field_value = $temp_value;
        }
        $person = array_sum( $cart_item['booking']['_persons'] );
    }
    $additional_fee_name = "Natl Park Entrance Fee";
    $extra_fee = $custom_field_value * $person;
    $addedFee = false;
    // first check to make sure it isn’t already there
    foreach ( $woocommerce->cart->get_fees() as $_fee ) {
        if ($_fee->id == sanitize_title($additional_fee_name) ) {
            $_fee->amount = (float) esc_attr( $extra_fee );
        }
    }
    if (!$addedFee) {
        $woocommerce->cart->add_fee( __($additional_fee_name, "woocommerce"),
        $extra_fee, $additional_fee_taxable );
    }
}
add_action( "woocommerce_before_calculate_totals", "woo_add_cart_fee" );

【讨论】:

    猜你喜欢
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 2018-05-18
    • 2015-12-10
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    相关资源
    最近更新 更多