Problem with custom field for WooCommerce's products

Support General Problem with custom field for WooCommerce's products

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #36087
    kadotakadota
    Participant

    I don't know what I'm doing wrong here. I've created a custom field for products, ID is "extra_fee".

    The function I'm using is:

    add_action( 'woocommerce_cart_calculate_fees', 'wpf_wc_add_cart_fee' );
    if ( ! function_exists( 'wpf_wc_add_cart_fee' ) ) {
        function wpf_wc_add_cart_fee( $cart ) {
    	$name      = 'Extra fee: ';
    	$amount    = rwmb_meta( 'extra_fee' );
    	$taxable   = true;
    	$tax_class = 'reduced';
    	$cart->add_fee( $name, $amount, $taxable, $tax_class );
        }
    }

    If a create a $value and assign it to rwmb_meta( 'extra_fee' ) before assigning it to the amount, the result is the same, it shows 0. The function works fine because if I change rwmb_meta( 'extra_fee' ) to a number, it shows it properly.

    Thank you!

    #36096
    Long NguyenLong Nguyen
    Moderator

    Hi Kadota,

    If the helper function is called outside the loop, you need to pass the post (product) ID to get the field value.

    For example:
    $amount = rwmb_meta( 'extra_fee', '', 123 );
    where 123 is the product ID.

    Read more on this documentation https://docs.metabox.io/functions/rwmb-meta/

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.