WooCommerce:在任何地方添加/显示产品或变体自定义字段

Mic*_*sby 4 php wordpress cart orders woocommerce

目前使用 WordPress 5.1.1 和 WooCommerce 3.5.7。我的 WooCommerce 商店拥有大约 500 种产品,由简单且可变的产品组成。

每个产品自然都有一个SKU,但每个产品也有一个唯一的ID代码,称为“商品代码”。我向特定行业销售特定产品。

我已在 Functions.php 文件中添加了简单和可变产品的自定义字段的代码,目前效果很好。

我的问题是,我试图让“商品代码”显示在购物车、结帐、发票和电子邮件中的产品标题下。

我在互联网上阅读了各种教程来帮助我,但我一无所知,因为每个教程都以不同的方式做类似的事情。大多数教程都假设用户已通过前端输入数据,但我的数据是预设的。

我用来帮助我的教程是:

<?php
// Add WooCommerce Custom Field for Commodity Code
function vp_add_commodity_code() {
  $args = array(
    'id' => 'vp_commodity_code',
    'label' => __( 'Commodity Code', 'woocommerce' ),
    'placeholder' => __( 'Enter Commodity Code here', 'woocommerce' ),
    'desc_tip' => true,
    'description' => __( 'This field is for the Commodity Code of the product.', 'woocommerce' ),
  );
  woocommerce_wp_text_input( $args );
}
add_action( 'woocommerce_product_options_sku', 'vp_add_commodity_code' );


// Save Commodity Code into WooCommerce Database
function vp_save_commodity_code( $post_id ) {
  // grab the Commodity Code
  $sku = isset( $_POST[ 'vp_commodity_code' ] ) ? sanitize_text_field( $_POST[ 'vp_commodity_code' ] ) : '';

  // grab the product
  $product = wc_get_product( $post_id );

  // save the Commodity Code custom field
  $product->update_meta_data( 'vp_commodity_code', $sku );
  $product->save();
}
add_action( 'woocommerce_process_product_meta', 'vp_save_commodity_code' );

// Display Commodity Code on the Frontend
function vp_display_commodity_code() {
    global $post;
    // Check for the Commodity Code value
    $product = wc_get_product( $post->ID );
    $title = $product->get_meta( 'vp_commodity_code' );
    if( $title ) {
    // Only display the field if we've got a value for the field title
    printf(
    '<div class="vpcommoditycode-wrapper"><strong>Commodity Code: </strong>%s</div>',
    esc_html( $title )
    );
    }
   }
   add_action( 'woocommerce_before_add_to_cart_button', 'vp_display_commodity_code' );


// Add custom field input @ Product Data > Variations > Single Variation

add_action( 'woocommerce_variation_options_pricing', 'comcode_add_custom_field_to_variations', 10, 3 ); 

function comcode_add_custom_field_to_variations( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input( array(
    'id' => 'custom_field[' . $loop . ']',
    'class' => 'short',
    'label' => __( 'Community Code', 'woocommerce' ),
    'value' => get_post_meta( $variation->ID, 'custom_field', true ),
    'placeholder' => __( 'Enter Commodity Code here', 'woocommerce' ),
    'desc_tip' => true,
    'description' => __( 'This field is for the Commodity Code of the product.', 'woocommerce' ),
)
);
}

// Save custom field on product variation

add_action( 'woocommerce_save_product_variation', 'comcode_save_custom_field_variations', 10, 2 );

function comcode_save_custom_field_variations( $variation_id, $i ) {
$custom_field = $_POST['custom_field'][$i];
if ( isset( $custom_field ) ) update_post_meta( $variation_id, 'custom_field', esc_attr( $custom_field ) );
}

// Store custom field value into variation data

add_filter( 'woocommerce_available_variation', 'comcode_add_custom_field_variation_data' );

function comcode_add_custom_field_variation_data( $variations ) {
$variations['custom_field'] = '<div class="woocommerce_custom_field"><strong>Commodity Code: </strong><span>' . get_post_meta( $variations[ 'variation_id' ], 'custom_field', true ) . '</span></div>';
return $variations;
}
?>
Run Code Online (Sandbox Code Playgroud)

是否有 PHP/WooCommerce 天才可以通过提供代码或向我指出一个可以帮助我的教程或命名可以执行此操作的第三方 WordPress 插件来帮助我。

Loi*_*tec 6

我已经重新访问并完成了您的现有代码,例如它不适用于产品变体\xe2\x80\xa6 这将:

\n\n
    \n
  • 在库存选项卡下的管理产品上显示自定义字段
  • \n
  • 在每个变体的变体选项卡下在管理产品上显示自定义字段
  • \n
  • 保存产品和产品变体的自定义字段值
  • \n
  • 在单个产品页面上显示自定义字段值(也适用于每个选定的变体)
  • \n
  • 在购物车和结帐页面上显示自定义字段值
  • \n
  • 将自定义字段值保存为订单项元数据
  • \n
  • 在管理订单上显示自定义字段值
  • \n
  • 在订单和电子邮件通知上显示自定义字段
  • \n
\n\n

代码:

\n\n
// Admin: Add custom field\nadd_action(\'woocommerce_product_options_sku\', \'vp_add_commodity_code\' );\nfunction vp_add_commodity_code(){\n\n    woocommerce_wp_text_input( array(\n        \'id\'          => \'_commodity_code\',\n        \'label\'       => __(\'Commodity Code\', \'woocommerce\' ),\n        \'placeholder\' => __(\'Enter Commodity Code here\', \'woocommerce\' ),\n        \'desc_tip\'    => true,\n        \'description\' => __(\'This field is for the Commodity Code of the product.\', \'woocommerce\' ),\n    ) );\n}\n\n// Admin: Save custom field value for simple product inventory options\nadd_action(\'woocommerce_admin_process_product_object\', \'vp_product_save_commodity_code\', 10, 1 );\nfunction vp_product_save_commodity_code( $product ){\n    if( isset($_POST[\'_commodity_code\']) )\n        $product->update_meta_data( \'_commodity_code\', sanitize_text_field($_POST[\'_commodity_code\']) );\n}\n\n// Admin: Add custom field in product variations options pricing\nadd_action( \'woocommerce_variation_options_pricing\', \'vp_add_variation_commodity_code\', 10, 3 );\nfunction vp_add_variation_commodity_code( $loop, $variation_data, $variation ){\n\n   woocommerce_wp_text_input( array(\n        \'id\'          => \'_commodity_code[\'.$loop.\']\',\n        \'label\'       => __(\'Commodity Code\', \'woocommerce\' ),\n        \'placeholder\' => __(\'Enter Commodity Code here\', \'woocommerce\' ),\n        \'desc_tip\'    => true,\n        \'description\' => __(\'This field is for the Commodity Code of the product.\', \'woocommerce\' ),\n        \'value\'       => get_post_meta( $variation->ID, \'_commodity_code\', true )\n    ) );\n}\n\n// Admin: Save custom field value from product variations options pricing\nadd_action( \'woocommerce_save_product_variation\', \'save_barcode_variations\', 10, 2 );\nfunction save_barcode_variations( $variation_id, $i ){\n    if( isset($_POST[\'_commodity_code\'][$i]) ){\n        update_post_meta( $variation_id, \'_commodity_code\', sanitize_text_field($_POST[\'_commodity_code\'][$i]) );\n    }\n}\n\n// Frontend: Display Commodity Code on product\nadd_action( \'woocommerce_before_add_to_cart_button\', \'vp_product_display_commodity_code\' );\nfunction vp_product_display_commodity_code() {\n    global $product;\n\n    if( $value = $product->get_meta( \'_commodity_code\' ) ) {\n        echo \'<div class="vp-ccode-wrapper"><strong>\' . __("Commodity Code", "woocommerce") .\n        \': </strong>\'.esc_html( $value ).\'</div>\';\n    }\n}\n\n// Frontend: Display Commodity Code on product variations\nadd_filter( \'woocommerce_available_variation\', \'vp_variation_display_commodity_code\', 10, 3 );\nfunction vp_variation_display_commodity_code( $data, $product, $variation ) {\n\n    if( $value = $variation->get_meta( \'_commodity_code\' ) ) {\n        $data[\'price_html\'] .= \'<p class="vp-ccode"><small><strong>\' . __("Commodity Code", "woocommerce") .\n        \': </strong>\'.esc_html( $value ).\'</small></p>\';\n    }\n\n    return $data;\n}\n\n// Frontend: Display Commodity Code on cart\nadd_filter( \'woocommerce_cart_item_name\', \'vp_cart_display_commodity_code\', 10, 3 );\nfunction vp_cart_display_commodity_code( $item_name, $cart_item, $cart_item_key ) {\n    if( ! is_cart() )\n        return $item_name;\n\n    if( $value = $cart_item[\'data\']->get_meta(\'_commodity_code\') ) {\n        $item_name .= \'<br><small class="vp-ccode"><strong>\' . __("Commodity Code", "woocommerce") .\n            \':</strong> \' . esc_html( $value ) . \'</small>\';\n    }\n    return $item_name;\n}\n\n// Frontend: Display Commodity Code on checkout\nadd_filter( \'woocommerce_checkout_cart_item_quantity\', \'vp_checkout_display_commodity_code\', 10, 3 );\nfunction vp_checkout_display_commodity_code( $item_qty, $cart_item, $cart_item_key ) {\n    if( $value = $cart_item[\'data\']->get_meta(\'_commodity_code\') ) {\n        $item_qty .= \'<br><small class="vp-ccode"><strong>\' . __("Commodity Code", "woocommerce") .\n            \':</strong> \' . esc_html( $value ) . \'</small>\';\n    }\n    return $item_qty;\n}\n\n// Save Commodity Code to order items (and display it on admin orders)\nadd_filter( \'woocommerce_checkout_create_order_line_item\', \'vp_order_item_save_commodity_code\', 10, 4 );\nfunction vp_order_item_save_commodity_code( $item, $cart_item_key, $cart_item, $order ) {\n    if( $value = $cart_item[\'data\']->get_meta(\'_commodity_code\') ) {\n        $item->update_meta_data( \'_commodity_code\', esc_attr( $value ) );\n    }\n    return $item_qty;\n}\n\n// Frontend & emails: Display Commodity Code on orders\nadd_action( \'woocommerce_order_item_meta_start\', \'vp_order_item_display_commodity_code\', 10, 4 );\nfunction vp_order_item_display_commodity_code( $item_id, $item, $order, $plain_text ) {\n    // Not on admin\n    //if( is_admin() ) return;\n\n    if( $value = $item->get_meta(\'_commodity_code\') ) {\n        $value = \'<strong>\' . __("Commodity Code", "woocommerce") . \':</strong> \' . esc_attr( $value );\n\n        // On orders\n        if( is_wc_endpoint_url() )\n            echo \'<div class="vp-ccode"><small>\' . $value . \'</small></div>\';\n        // On Emails\n        else\n            echo \'<div style="font-size:11px;padding-top:6px">\' . $value . \'</div>\';\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。

\n