WooCommerce: Get custom field from product variations and display it as a suffix to variation prices

﹥>﹥吖頭↗ 提交于 2021-02-05 07:51:34

问题


I'm trying to get a value from a custom numbers field on product variations, and show it as a suffix to variation prices along with custom text.

I'm working from

  • WooCommerce: Get custom field from product variations and display it on the “additional information area”
  • Adding custom text to the variation price in Woocommerce

This is what I have:

 // 1. Add custom field input @ Product Data > Variations > Single Variation
add_action( 'woocommerce_variation_options_pricing', 'Add_bulk_price_to_variations', 10, 3 );
function Add_bulk_price_to_variations( $loop, $variation_data, $variation ) {  
    woocommerce_wp_text_input( array(
        'id' => 'bulk_price[' . $loop . ']',
      'desc_tip'    => 'true',
            'description' => __( 'Enter the Bulk price here.', 'woocommerce' ),
        'label' => __( 'Custom Field', 'woocommerce' ),
        'value' => get_post_meta( $variation->ID, 'bulk_price', true ) 
    ));
}

// 2. Save custom field on product variation save
add_action( 'woocommerce_save_product_variation', 'Save_bulk_price_variations', 10, 2 );
function Save_bulk_price_variations( $variation_id, $i ) {
    $bulk_price = $_POST['bulk_price'][$i];
    if ( isset( $bulk_price ) ) {
        update_post_meta( $variation_id, 'bulk_price', esc_attr( $bulk_price ) );
    }
}

// 3. Store custom field value into variation data
add_filter( 'woocommerce_available_variation', 'Add_bulk_price_variation_data' );
function Add_bulk_price_variation_data( $variations ) {  
    $variations['bulk_price'] = '<div class="woocommerce_bulk_price">Custom Field: <span>' . get_post_meta( $variations[ 'variation_id' ], 'bulk_price', true ) . '</span></div>';

    return $variations;
}



// 4. Show the bulk price on product variations

function variation_price_custom_suffix( $variation_data, $product, $variation ) {


 // Get childIDs in an array
    $children_ids = $variations->get_children();

    foreach ( $children_ids as $child_id ) {
        $value = get_post_meta( $child_id, 'bulk_price', true );

        // True
        if ( $value ) {


    $variation_data['price_html'] .= ' <span class="price-suffix">' . $value . __("custom text", "woocommerce") . '</span>';

    return $variation_data;
}
add_filter('woocommerce_available_variation', 'variation_price_custom_suffix', 10, 3 );

回答1:


You use the same hook 2x, this should suffice (where step 4 is no longer needed)

The use of a foreach loop to get the child ID's is not necessary

// 3 & 4. Store custom field value into variation data + show the bulk price on product variations
function add_bulk_price_variation_data( $variation_data, $product, $variation ) {
    $bulk_price = get_post_meta( $variation_data[ 'variation_id' ], 'bulk_price', true);

    if ( $bulk_price ) {
        $variation_data['price_html'] .= ' <span class="price-suffix">' . __( $bulk_price , "woocommerce") . '</span>';
    }

    return $variation_data;
}
add_filter( 'woocommerce_available_variation', 'add_bulk_price_variation_data', 10, 3 );


来源:https://stackoverflow.com/questions/61970535/woocommerce-get-custom-field-from-product-variations-and-display-it-as-a-suffix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!