从 WooCommerce 自定义元框中的 SKU 检索产品 ID

问题描述 投票:0回答:1

我正在尝试使用 Woocommerce 中的自定义元框保存 **产品 ** 的元数据。该表格采用 SKU 和属于该产品的商品的数量。我还希望添加与输入的 SKU 对应的产品 ID,而不是帖子的 SKU。

我在哪里执行此操作以及如何执行?是否应该在 wizpart_save_meta_box( $post_id ) 函数中,获取发布的 SKU 并将产品 ID 添加到元数据中?如果是这样我该怎么做?

代码如下,输出如附图所示;仅供参考,代码位于“Snippets”插件中,而不是子主题的functions.php中。

预期的最终代码将管理作为产品一部分列出的 SKU 的库存;即产品包含 16 个 GA1 单位 - 将调整 SKU GA1 的库存水平。

<?php
// Meta Box Inventory Project


/*
Plugin Name: WIZ Custom Meta Box Plugin
Plugin URI: 
Description: 
Version: 1.0
Author: David
Author URI:
License:
*/
// check to see if post type is 'product'
// make checks to see if the product is tagged with '_hasparts'



add_action( 'add_meta_boxes', 'wizpart_meta_box_init' );

// meta box functions for adding the meta box and saving the data
function wizpart_meta_box_init() {  
    // create custom meta box
    add_meta_box( 'wizpart_meta', 'Parts Information', 'wizpart_meta_box', 'product', 'normal', 'high' );

}

function wizpart_meta_box( $post ) {
// Leading HTML

    
    // retrieve the custom meta box values
    $wizpart_meta_full = get_post_meta( $post->ID, '_wizpart_data');
    echo var_dump($wizpart_meta_full).'<br />';
            if (! empty($wizpart_meta_full)) {
                foreach( $wizpart_meta_full as $entry){ 
                // print out each part

                    print 'Product comprises '.$entry["quant"].' units of '.$entry["sku"].' <br />';
                    
                }
            }   
    // returning entered values
    $wizpart_meta = get_post_meta( $post->ID, '_wizpart_data');
    $wizpart_meta = end($wizpart_meta);
    $wizpart_sku = (! empty( $wizpart_meta['sku'])) ? $wizpart_meta['sku'] : 'NA';
    $wizpart_quant = (! empty( $wizpart_meta['quant'])) ? $wizpart_meta['quant'] : 'NA';

    //nonce for security
    wp_nonce_field( 'meta_box_save', 'wizpart_plugin' );

   // display stored values 
    echo '<div class = wizpart>';
    echo '<table>';
    echo '<tr>';
    echo '<td> SKU: </td>';
    echo '<td> <input type="text" name="wizpart[sku]" value = "'.esc_attr( $wizpart_sku ).'" size="5" > </td>';
    echo '</tr>';
    
    echo '<tr>';
    echo '<td> Quant: </td>';
    echo '<td> <input type="text" name="wizpart[quant]" value = "'.esc_attr( $wizpart_quant ).'" size="5" > </td>';
    echo '</tr>';
    echo '</table>';
    echo '</div>';  
}

// hook to save meta box data
add_action( 'save_post', 'wizpart_save_meta_box' );

function wizpart_save_meta_box( $post_id ) {

    // process form data if $_POST is set

        // if auto saving skip saving our meta box data
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
            return;
        //check nonce for security
        wp_verify_nonce( 'meta_box_save', 'wizpart_plugin' );
        // store data in an array
        $wizpart_data = $_POST['wizpart'];
        // use array map function to sanitize options
        $wizpart_data = array_map( 'sanitize_text_field', $wizpart_data) ;

        // save the meta box data as post meta using the post ID as a unique prefix
        // using add_post_meta to increment the array with values
        add_post_meta( $post_id, '_wizpart_data', $wizpart_data );
    }
?>

输出

我尝试恢复 $wizpart_sku 发布值的 ID,然后将其添加到数组 $wizpart_data - 但我看不到如何从 $_POST['wizpart'] 访问发布数据的 SKU。

我尝试在表单中添加隐藏字段,成功添加发布产品页面 ID - 但这不能携带尚未发布的 SKU 的 ID 值!

wordpress woocommerce metadata product
1个回答
0
投票

这似乎可行,尽管看起来不太优雅:

// Get the post ID from the from SKU
    $wizpart_data = $_POST['wizpart'];
    $sku = $wizpart_data[0];
    $query = new WC_Product_Query( array(
    'sku' => $sku,) );
    $products = $query->get_products();
    $product = reset( $products );
    $id = $product->get_id();
    $wizpart_data =  $wizpart_data + array("ID" => $id);

输出

© www.soinside.com 2019 - 2024. All rights reserved.