将高级自定义字段添加到WooCommerce产品变体

Sye*_*jid 1 wordpress wordpress-plugin variations woocommerce advanced-custom-fields

我正在使用名为Advanced Custom Fields(ACF)和WooCommerce的插件.我想为WooCommerce产品变体创建一个文本和图像字段.我在ACF中创建了字段并将它们分配给了"Post Type" > "product_variation".

在此输入图像描述

但是因为我没有看到这些领域product > Variations.我搜索过,看起来我必须编写一个自定义代码来容纳这些字段.我尝试搜索问题,我发现的大多数建议和教程都是关于通过代码创建自定义字段而不使用ACF,这对我没有帮助,因为字段必须使用ACF,这是因为我使用Visual Composer来提取这些前端的ACF字段.

在此输入图像描述

小智 6

它不是acf,但你只需要在/public_html/wp-content/themes/yourtheme-child/function.php下为你的孩子主题的"function.php"添加一些代码.

请查看本教程http://www.remicorson.com/mastering-woocommerce-products-custom-fields/

例如,在我的代码中,我为RRP添加了2个字段,另外一个用于个人使用(每对价格):

        /* Adds RRP or PPP* price after each product price throughout the shop for user != Customer & Guest
    .Not displayed in cart as it's not per var and we don't need to.
    PPP = Price Per Pair (for product composite/bundle display)
    ================================================== */

// 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;

    // Text Field creation
woocommerce_wp_text_input( 
    array( 
        'id'          => '_rrpprice', 
        'label'       => __( 'RRP price ($)', 'woocommerce' ), 
        'placeholder' => 'e.g. 499',
        'desc_tip'    => 'true',
        'description' => __( 'Enter the RRP .', 'woocommerce' )
    )
);
woocommerce_wp_text_input( 
    array(  
        'id'          => '_pppprice', 
        'label'       => __( 'Price Per Pair*', 'woocommerce' ),
        'placeholder' => 'e.g. 122',
        'desc_tip'    => 'true',
        'description' => __( 'Enter the PPP (Price Per Pair) if Bundle/composite .', 'woocommerce' ) 
    )
);  
}
function woo_add_custom_general_fields_save( $post_id ){

    // TextField save
    $woocommerce_rrpprice = $_POST['_rrpprice'];
    update_post_meta( $post_id, '_rrpprice', esc_html( $woocommerce_rrpprice ) );
    if( !empty( $woocommerce_rrpprice ) )

    // TextField save
    $woocommerce_pppprice = $_POST['_pppprice'];
    if( !empty( $woocommerce_pppprice ) )
    update_post_meta( $post_id, '_pppprice', esc_html( $woocommerce_pppprice ) );
}

// Display Custom Field Value

if ( is_user_logged_in() && !(current_user_can('customer'))) {
    function sv_change_product_price_display( $price ) {
        $product = wc_get_product( get_the_ID() );
        $priceRRP = get_post_meta( get_the_ID(), '_rrpprice', true );
        $pricePPP = get_post_meta( get_the_ID(), '_pppprice', true );
          if ( (is_shop() || is_product()) && !is_cart() ) { //double belt check
            if($product->is_type( 'variable' )){
                $price .= ' + GST<br /><span class="rrp-price">RRP: $' . $priceRRP .'</span>';
            }else{

                $price = ' <span class="rrp-price"><b>$' . $pricePPP .' + GST </b></span>' . '<br /><span class="rrp-price">RRP: $' . $priceRRP .'</span>';
            }
          }
        return $price;          
    }
    add_filter( 'woocommerce_get_price_html', 'sv_change_product_price_display' );
    add_filter( 'woocommerce_cart_item_price', 'sv_change_product_price_display' );
}
Run Code Online (Sandbox Code Playgroud)

如果您有任何疑问,请随时提出.