php 如何保存和获取WooCommerce项目元数据?[副本]

xytpbqjk  于 12个月前  发布在  PHP
关注(0)|答案(1)|浏览(86)

此问题已在此处有答案

Display and save added custom cart item data on Woocommerce Cart, Checkout and Orders(3个答案)
2天前关闭。
我试图找出如何设置和收集元数据从我的WooCommerce自定义字段。我创建了一个新产品,其中包含一些我创建的自定义字段,只有“addressed_to”和“message”。我能够为我的订单项设置元数据(您将看到,可以这么说),但似乎无法再次提取它。
我设置上述数据的代码如下:

// set the price to add to the cart
add_action('woocommerce_before_calculate_totals', 'custom_set_price_to_cart', 10, 1);

function custom_set_price_to_cart($cart) {

    // loop through the cart items
    foreach($cart->get_cart() as $cart_item_key => $cart_item) {

        // get the product id
        $product_id = $cart_item['product_id'];

        // get the product
        $product = wc_get_product($product_id);

        // get the product name
        $product_name = $product->get_name();

        // check if the product is a Custom Item
        if($product_name == 'Custom Item') {

            // set addressed-to
            $addressed_to = $cart_item['addressed-to'];

            // set message
            $message = $cart_item['message'];

            // set addressed-to
            $cart_item['data']->add_meta_data('addressed-to', $addressed_to, true);

            // set message
            $cart_item['data']->add_meta_data('message', $message, true);

            error_log(print_r($cart_item['data'], true));

        } else {
                
            // get the price
            $price = $cart_item['data']->get_price();

            // set the price
            $cart_item['data']->set_price($price);

        }

    }

}

有了这个,当我在最后错误记录它时,我实际上得到了我的新元数据:

[meta_data:protected] => Array
        (
            [0] => WC_Meta_Data Object
                (
                    [current_data:protected] => Array
                        (
                            [key] => addressed-to
                            [value] => Someone
                        )

                    [data:protected] => Array
                        (
                            [key] => addressed-to
                            [value] => Someone
                        )

                )

            [1] => WC_Meta_Data Object
                (
                    [current_data:protected] => Array
                        (
                            [key] => message
                            [value] => We hope that this will now work
                        )

                    [data:protected] => Array
                        (
                            [key] => message
                            [value] => We hope that this will now work
                        )

                )

        )

然而,我在实际访问这些变量时遇到了困难,我尝试了get_meta()wc_get_order_item_meta()的变体,但没有用。
所以我尝试了以下方法:

// When an order is made with a bauble, create a new bauble
add_action('woocommerce_thankyou', 'custom_item_create_post', 10, 1);

function custom_item_create_post() {

    // get the order id
    $order_id = wc_get_order_id_by_order_key($_GET['key']);

    // get the order
    $order = wc_get_order($order_id);

    // get the order items
    $order_items = $order->get_items();

    // loop through items
    foreach($order_items as $order_item_id => $order_item) {
        
        // get the product id
        $product_id = $order_item->get_product_id();

        // get the product
        $product = wc_get_product($product_id);

        // get the product name
        $product_name = $product->get_name();

        if ($product_name == 'Custom Item') {


            // get addressed-to
            $addressed_to = get_post_meta($order_item_id, 'addressed-to', true);

            // get message
            $message = get_post_meta($order_item_id, 'message', true);

            // get name of the person who ordered
            $order_billing_first_name = $order->get_billing_first_name();
            $order_billing_last_name = $order->get_billing_last_name();
            $order_billing_full_name = $order_billing_first_name . ' ' . $order_billing_last_name;

            // get the price of the custom_item
            $custom_item_price = $order_item->get_total();

            // count how many custom_items currently exist
            $args = array(
                'post_type' => 'custom_post',
                'post_status' => 'publish',
                'posts_per_page' => -1
            );
            $custom_items = new WP_Query($args);
            $custom_item_count = $custom_posts->found_posts;

            // create the custom_post
            $custom_item = array(
                'post_title' => $addressed_to,
                'post_type' => 'custom_post',
                'post_status' => 'publish'
            );

            // insert the bauble
            $custom_item_id = wp_insert_post($custom_item);

            // set acf order_number to the order number
            update_field('order_number', $order_id, $bauble_id);

            // set acf addressed_to to the addressed_to
            update_field('addressed_to', $addressed_to, $bauble_id);

            // set acf message to the message
            update_field('message', $message, $bauble_id);
        }

    }

}

我试图使用这些数据是为了在购买物品后设置一个自定义帖子。我可以用它来创建帖子,但它不会填充项目字段。我用的是ACF,这是另一个有趣的小部分。
任何和所有的帮助将不胜感激!

编辑

我在最后一段代码中添加了一些额外的错误日志,看起来在woocommerce_thankyou钩子的点上,元数据丢失了。所以我相信我保存数据的方式也有问题。

uz75evzq

uz75evzq1#

看来我想通了
我只是添加了一个新函数,以确保为元数据添加了行。

// add custom metadata to item
add_filter('woocommerce_checkout_create_order_line_item', 'custom_add_custom_metadata', 10, 4);

function custom_add_custom_metadata($item, $cart_item_key, $values, $order) {

    // get the product
    $product = wc_get_product($item->get_product_id());

    // get the product name
    $product_name = $product->get_name();

    // check if the product is a bauble
    if($product_name == 'Custom Item') {

        // set addressed-to
        $item->add_meta_data('addressed-to', $values['addressed-to'], true);

        // set message
        $item->add_meta_data('message', $values['message'], true);

    }

    return $item;

}

相关问题