我无法在可变产品上显示自定义帖子。知道如何解决吗?

时间:2019-06-08 15:32:20

标签: php woocommerce

我在woocommerce产品上添加了一些自定义字段。我使用下面的代码在Woocommerce流程上显示自定义字段:查看;订单和电子邮件。但是我不能让它们出现在可变产品过程中。它完美地适用于单个产品。谁能帮我修复我的代码?非常感谢:)

/**
 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 个答案:

答案 0 :(得分:0)

我不熟悉用于声明自定义字段的钩子。请注意,挂钩与主要产品和产品型号不同。

要将自定义字段添加到变体中,我使用以下方法:

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

要显示该字段,请使用简单的get_post_meta()

进行检索
get_post_meta( $variation->ID, META_PREFIX . 'vendor-product-id', true );