簡體   English   中英

在 WooCommerce 電子郵件訂單項目中顯示可變產品的產品自定義字段

[英]Display a product custom field for variable products in WooCommerce email order items

我在產品中添加了一個自定義 SKU,用於供應商 SKU 的訂單確認電子郵件。 我創建的內容僅適用於簡單產品而不適用於可變產品。

自定義 SKU,截圖圖片

我將此添加到functions.php

function jk_add_custom_sku() {
    $args = array(
        'label' => __( 'Custom SKU', 'woocommerce' ),
        'placeholder' => __( 'Enter custom SKU here', 'woocommerce' ),
        'id' => 'jk_sku',
        'desc_tip' => true,
        'description' => __( 'This SKU is for internal use only.', 'woocommerce' ),
    );
    woocommerce_wp_text_input( $args );
}
add_action( 'woocommerce_product_options_sku', 'jk_add_custom_sku' );

function jk_save_custom_sku( $post_id ) {
    // grab the custom SKU from $_POST
    $custom_sku = isset( $_POST[ 'jk_sku' ] ) ? sanitize_text_field( $_POST[ 'jk_sku' ] ) : '';
    
    // grab the product
    $product = wc_get_product( $post_id );
    
    // save the custom SKU using WooCommerce built-in functions
    $product->update_meta_data( 'jk_sku', $custom_sku );
    $product->save();
}
add_action( 'woocommerce_process_product_meta', 'jk_save_custom_sku' );

接下來我修改了電子郵件模板 email-order-items.php 添加到 SKU 部分以檢查是否存在自定義 SKU。

<?php

// Show title/image etc.
if ( $show_image ) {
    echo wp_kses_post( apply_filters( 'woocommerce_order_item_thumbnail', $image, $item ) );
}

// Product name.
echo wp_kses_post( apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, false ) );

// SKU.
if ( $show_sku && $sku ) {
    echo wp_kses_post( ' (#' . $sku . ')' );
    // load the custom SKU
    $custom_sku = get_post_meta( $product->get_id(), 'jk_sku', true );
    if ( is_string( $custom_sku ) ) { // only show the custom SKU if it's set
        echo "<br>" . wp_kses_post( "Custom SKU: $custom_sku" ); // change this line if needed
    }
}

// allow other plugins to add additional product information here.
do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order, $plain_text );

wc_display_item_meta(
    $item,
    array(
        'label_before' => '<strong class="wc-item-meta-label" style="float: ' . esc_attr( $text_align ) . '; margin-' . esc_attr( $margin_side ) . ': .25em; clear: both">',
    )
);

// allow other plugins to add additional product information here.
do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order, $plain_text );

?>
</td>

自定義 SKU 僅顯示在簡單產品的電子郵件中。 我需要它來處理可變產品。 每個產品只有一個自定義 SKU,而不是每個可變產品。

我重新訪問了一些您的代碼,並將元鍵更改為_sku2 ,就像 WooCommerce 元鍵以下划線開頭一樣:

add_action( 'woocommerce_product_options_sku', 'add_product_sku2_custom_field' );
function add_product_sku2_custom_field() {
    $field_key = '_sku2';

    woocommerce_wp_text_input( array(
        'id'          => $field_key,
        'label'       => __( 'Custom SKU', 'woocommerce' ),
        'placeholder' => __( 'Enter custom SKU here', 'woocommerce' ),
        'desc_tip'    => true,
        'description' => __( 'This SKU is for internal use only.', 'woocommerce' ),
    ) );
}

add_action( 'woocommerce_admin_process_product_object', 'save_product_sku2_custom_field_value' );
function save_product_sku2_custom_field_value( $product ) {
    $field_key = '_sku2';

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

對於產品變體,您需要獲取父變量產品才能顯示“自定義 sku” (請參閱最后的代碼)

現在要為可變產品的產品變體啟用額外的自定義 SKU,請使用以下內容:

add_action( 'woocommerce_variation_options_pricing', 'add_product_variation_sku2_custom_field', 10, 3 );
function add_product_variation_sku2_custom_field( $loop, $variation_data, $variation ){
    $field_key = '_sku2';

    woocommerce_wp_text_input( array(
        'id'          => $field_key.'['.$loop.']',
        'label'       => __( 'Custom SKU', 'woocommerce' ),
        'placeholder' => __( 'Enter custom SKU here', 'woocommerce' ),
        'desc_tip'    => true,
        'description' => __( 'This SKU is for internal use only.', 'woocommerce' ),
        'value'       => get_post_meta( $variation->ID, $field_key, true )
    ) );
}

add_action( 'woocommerce_save_product_variation', 'save_product_variation_sku2_custom_field_value', 10, 2 );
function save_product_variation_sku2_custom_field_value( $variation_id, $i ){
    $field_key = '_sku2';

    if( isset($_POST[$field_key][$i]) ){
        update_post_meta( $variation_id, $field_key, sanitize_text_field($_POST[$field_key][$i]) );
    }
}

代碼位於活動子主題(或活動主題)的 functions.php 文件中。 測試和工作。

在您的模板自定義代碼中,您將替換:

// SKU.
if ( $show_sku && $sku ) {
    echo wp_kses_post( ' (#' . $sku . ')' );
    // load the custom SKU
    $custom_sku = get_post_meta( $product->get_id(), 'jk_sku', true );
    if ( is_string( $custom_sku ) ) { // only show the custom SKU if it's set
        echo "<br>" . wp_kses_post( "Custom SKU: $custom_sku" ); // change this line if needed
    }
}

通過以下(適用於可變產品或產品變體)

// SKU (and SKU2)
if ( $show_sku && $sku ) {
    echo wp_kses_post( ' (#' . $sku . ')' );
    // load the custom SKU
    $sku2 = $product->get_meta('_sku2');
    
    // For product variations with empty SKU (get the parent variable product SKU)
    if ( empty($sku2) && $product->is_type('variation') ) { 
        // Get parent variable product Id
        $parent_product_id = $product->get_parent_id(); 
        $parent_product    = wc_get_product($parent_product_id);
        $sku2              = $parent_product->get_meta('_sku2');
    }
    
    // only show the custom SKU if it's set (and product variation too)
    if ( ! empty($sku2) ) {
        echo "<br>" . wp_kses_post( sprintf( __("Custom SKU: %s", "woocommerce"), $sku2 ) ); // change this line if needed
    }
}

對於產品變體,如果沒有任何“自定義 sku”,它將嘗試獲取父變量產品“自定義 sku”。

它應該工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM