dongwu8050 2019-06-08 15:32
浏览 35

我无法在变量产品上显示自定义帖子。 知道怎么解决吗?

I add few custom field on the woocommerce product. I used the code below to display the custom field on the Woocommerce process : cart; check out; order and email. But I can't make them appear on variable product process. It works perfectly on single product. Can anyone help me with to fix my code? Thanks a lot :)

/**
 Display info
 **/

// Admin: Add custom field
add_action('woocommerce_product_options_sku', 'vp_add_pp_info' );
function vp_add_pp_info(){

    woocommerce_wp_text_input( array(
        'id'          => '_venue',
        'label'       => __('Venue', 'woocommerce' ),
        'placeholder' => __('Enter Venue here', 'woocommerce' ),
        'desc_tip'    => true,
        'description' => __('This field is for the Venue of the product.', 'woocommerce' ),
    ) );
    woocommerce_wp_text_input( array(
        'id'          => '_date',
        'label'       => __('Date', 'woocommerce' ),
        'placeholder' => __('Enter Date here', 'woocommerce' ),
        'desc_tip'    => true,
        'description' => __('This field is for the Date of the product.', 'woocommerce' ),
    ) );
    woocommerce_wp_text_input( array(
        'id'          => '_time',
        'label'       => __('Time', 'woocommerce' ),
        'placeholder' => __('Enter Time here', 'woocommerce' ),
        'desc_tip'    => true,
        'description' => __('This field is for the time of the product.', 'woocommerce' ),
    ) );
}

// Admin: Save custom field value for simple product inventory options
add_action('woocommerce_admin_process_product_object', 'vp_product_save_pp_info', 10, 1 );
function vp_product_save_pp_info( $product ){
    if( isset($_POST['_venue']) )
        $product->update_meta_data( '_venue', sanitize_text_field($_POST['_venue']) );

    if( isset($_POST['_date']) )
        $product->update_meta_data( '_date', sanitize_text_field($_POST['_date']) );

    if( isset($_POST['_time']) )
        $product->update_meta_data( '_time', sanitize_text_field($_POST['_time']) );
}




// Frontend: Display PP Info on product
add_action( 'woocommerce_single_product_summary', 'vp_product_display_pp_info' );
function vp_product_display_pp_info() {
    global $product;

    if( $value = $product->get_meta( '_venue' ) ) {
        echo '<div class="vp-ccode-wrapper"><strong>' . __("Venue info", "woocommerce") .
        ': </strong>'.esc_html( $value ).'</div>';
    }
    if( $value = $product->get_meta( '_date' ) ) {
        echo '<div class="vp-ccode-wrapper"><strong>' . __("Date info", "woocommerce") .
        ': </strong>'.esc_html( $value ).'</div>';
    }
    if( $value = $product->get_meta( '_time' ) ) {
        echo '<div class="vp-ccode-wrapper"><strong>' . __("Time info", "woocommerce") .
        ': </strong>'.esc_html( $value ).'</div>';
    }
}

// Frontend: Display PP Info on product variations
add_filter( 'woocommerce_available_variation', 'vp_variation_display_pp_info', 10, 3 );
function vp_variation_display_pp_info( $data, $product, $variation ) {

    if( $value = $variation->get_meta( '_venue' ) ) {
        $data['price_html'] .= '<p class="vp-ccode"><small><strong>' . __("Venue", "woocommerce") .
        ': </strong>'.esc_html( $value ).'</small></p>';
    }

    return $data;
}

// Frontend: Display info on cart
add_filter( 'woocommerce_cart_item_name', 'vp_cart_display_pp_info', 10, 3 );
function vp_cart_display_pp_info( $item_name, $cart_item, $cart_item_key ) {
    if( ! is_cart() )
        return $item_name;

    if( $value = $cart_item['data']->get_meta('_venue') ) {
        $item_name .= '<br><small class="vp-ccode"><strong>' . __("Venue", "woocommerce") .
            ':</strong> ' . esc_html( $value ) . '</small>';
    }

    if( $value = $cart_item['data']->get_meta('_date') ) {
        $item_name .= '<br><small class="vp-ccode"><strong>' . __("Date", "woocommerce") .
            ':</strong> ' . esc_html( $value ) . '</small>';
    }

     if( $value = $cart_item['data']->get_meta('_time') ) {
        $item_name .= '<br><small class="vp-ccode"><strong>' . __("Time", "woocommerce") .
            ':</strong> ' . esc_html( $value ) . '</small>';
    }
    return $item_name;
}



// Frontend: Display info on checkout
add_filter( 'woocommerce_checkout_cart_item_quantity', 'vp_checkout_display_pp_info', 10, 3 );
function vp_checkout_display_pp_info( $item_qty, $cart_item, $cart_item_key ) {
    if( $value = $cart_item['data']->get_meta('_venue') ) {
        $item_qty .= '<br><small class="vp-ccode"><strong>' . __("Venue", "woocommerce") .
            ':</strong> ' . esc_html( $value ) . '</small>';
    }

    if( $value = $cart_item['data']->get_meta('_date') ) {
        $item_qty .= '<br><small class="vp-ccode"><strong>' . __("Date", "woocommerce") .
            ':</strong> ' . esc_html( $value ) . '</small>';
    }

    if( $value = $cart_item['data']->get_meta('_time') ) {
        $item_qty .= '<br><small class="vp-ccode"><strong>' . __("Time", "woocommerce") .
            ':</strong> ' . esc_html( $value ) . '</small>';
    }
    return $item_qty;
}




// Save PP Info to order items (and display it on admin orders)
add_filter( 'woocommerce_checkout_create_order_line_item', 'vp_order_item_save_pp_info', 10, 4 );
function vp_order_item_save_pp_info( $item, $cart_item_key, $cart_item, $order ) {
    if( $value = $cart_item['data']->get_meta('_venue') ) {
        $item->update_meta_data( '_venue', esc_attr( $value ) );
    }

     if( $value = $cart_item['data']->get_meta('_date') ) {
        $item->update_meta_data( '_date', esc_attr( $value ) );
    }

      if( $value = $cart_item['data']->get_meta('_time') ) {
        $item->update_meta_data( '_time', esc_attr( $value ) );
    }

    return $item_qty;
}

// Frontend & emails: Display PP Info on orders
add_action( 'woocommerce_order_item_meta_start', 'vp_order_item_display_pp_info', 10, 4 );
function vp_order_item_display_pp_info( $item_id, $item, $order, $plain_text ) {
    // Not on admin
    //if( is_admin() ) return;

    if( $value = $item->get_meta('_venue') ) {
        $value = '<strong>' . __("Venue", "woocommerce") . ':</strong> ' . esc_attr( $value );

        // On orders
        if( is_wc_endpoint_url() )
            echo '<div class="vp-ccode"><small>' . $value . '</small></div>';
        // On Emails
        else
            echo '<div style="font-size:11px;padding-top:6px">' . $value . '</div>';
    }


}
  • 写回答

1条回答 默认 最新

  • dter8514 2019-06-11 20:10
    关注

    I am unfamiliar with the hooks you use to declare your custom fields. Note the hooks are not the same for the main product and for product variations.

    To add custom fields to variations I use this:

    add_action( 'woocommerce_product_after_variable_attributes', function( $loop, $variation_data, $variation ) {
        echo '<div class="product_custom_field">';
        woocommerce_wp_text_input(
            array(
                'label'       => 'Vendor ID',
                'id'          => '_vendor_id[' . $variation->ID . ']',
                'value'       =>  get_post_meta( $variation->ID, META_PREFIX . 'vendor-product-id', true ),
            )
        );
        woocommerce_wp_text_input(
            array(
                'label'       => 'Updated on',
                'id'          => '_updated_on[' . $variation->ID . ']',
                'value'       =>  get_post_meta( $variation->ID, META_PREFIX . 'last-update', true ),
            )
        );
        echo '</div>';
    }, 10, 3 );
    

    To display the field, retrieve with a simple get_post_meta()

    get_post_meta( $variation->ID, META_PREFIX . 'vendor-product-id', true );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数