我目前正在尝试在创建订单时将元添加到产品中,以便其在订单管理屏幕中可见。
我有点困惑,因为这应该是一个相对简单的任务,但它根本就不是在玩球。
以下函数通过
woocommerce_new_order
调用
public function process_new_order($order_id) {
$order = wc_get_order($order_id);
// Loop over order items (products)
foreach ($order->get_items() as $item_id => $item) {
$product = $item->get_product();
// Check if the product has variations
if ($product->is_type('variation')) {
// Get the variation attributes (including "License Type")
$variation_attributes = $product->get_variation_attributes();
// Check for "License Type" variation
if (isset($variation_attributes['attribute_license-type'])) {
$license_type = $variation_attributes['attribute_license-type'];
if ($license_type == 'Simple') {
$this->generate_and_save_license_keys($order_id, $item, 1);
} elseif ($license_type == 'Business') {
$this->generate_and_save_license_keys($order_id, $item, 10);
}
}
}
}
}
我知道这段代码可以工作,调用generate_and_save_license_keys,它会正确生成一个密钥,然后将其添加到数据库中,并且元存在并根据此代码通过错误日志正确记录:
private function generate_and_save_license_keys($order_id, $item, $count) {
global $wpdb;
$product_id = $item->get_product_id(); // Get the product ID (plugin_id)
$subscription_id = 0; // For simplicity, we assume no subscriptions for now
// Initialize an array to hold the generated license keys
$license_keys = [];
// Generate the required number of license keys
for ($i = 0; $i < $count; $i++) {
$license_key = $this->generate_license_key();
// Add license key to the array
$license_keys[] = $license_key;
// Insert the license key into the database
$wpdb->insert(
$wpdb->prefix . 'llm_licenses',
[
'license_code' => $license_key,
'staging_url' => null, // Set staging URL as NULL
'subscription_id' => $subscription_id,
'plugin_id' => $product_id,
'order_id' => $order_id
]
);
}
// Store the license keys as a single meta field// Debug to check meta data
$item->add_meta_data('_license_keys', implode(', ', $license_keys), true);
error_log('Meta keys for item ' . $item->get_id() . ': ' . print_r($item->get_meta('_license_keys', true), true));
}
但是,此数据不会显示在管理单一订单屏幕上的产品下方。
我尝试使用一个通过
woocommerce_order_item_meta_end
调用的函数手动将其添加到其中,但似乎只有当我将顺序更改为“处理”而不是在每个页面加载时才会触发,即使如此,它也不会触发显示 t 并且错误日志显示 get_meta 的 0 数据,即使它是在通过generate_and_save_license_keys 使用 get_meta 时记录的。
public function display_license_keys_on_order_screen($item_id, $item, $order, $plain_text) {
// Retrieve the meta data
$license_keys = $item->get_meta('_license_keys', true);
var_dump($license_keys);
print_r("fired");
// Log the meta data for debugging
error_log('Displaying license keys for item ' . $item_id . ': ' . print_r($license_keys, true));
if ($license_keys) {
// Display license keys on the order item page
echo '<p><strong>' . __('License Keys', 'text-domain') . ':</strong> ' . esc_html($license_keys) . '</p>';
} else {
// Log if no license keys found for the item
error_log('No license keys found for item ' . $item_id);
}
}
您的代码中缺少的是在项目对象上添加项目元数据以及最后在订单对象上添加项目元数据后的保存方法用法。
尝试以下代码替换:
public function process_new_order($order_id) {
$order = wc_get_order($order_id);
$use_save = false; // Initializing
// Loop over order items (products)
foreach ($order->get_items() as $item_id => $item) {
$product = $item->get_product();
// Check if the product has variations
if ($product->is_type('variation')) {
// Get the variation attributes (including "License Type")
$variation_attributes = $product->get_variation_attributes();
// Check for "License Type" variation
if (isset($variation_attributes['attribute_license-type'])) {
$license_type = $variation_attributes['attribute_license-type'];
if ($license_type == 'Simple') {
$this->generate_and_save_license_keys($order_id, $item, 1);
$use_save = true;
} elseif ($license_type == 'Business') {
$this->generate_and_save_license_keys($order_id, $item, 10);
$use_save = true;
}
}
}
}
if ( $use_save ) {
$order->save(); // Update order data
}
}
private function generate_and_save_license_keys($order_id, $item, $count) {
global $wpdb;
$product_id = $item->get_product_id(); // Get the product ID (plugin_id)
$subscription_id = 0; // For simplicity, we assume no subscriptions for now
// Initialize an array to hold the generated license keys
$license_keys = [];
// Generate the required number of license keys
for ($i = 0; $i < $count; $i++) {
$license_key = $this->generate_license_key();
// Add license key to the array
$license_keys[] = $license_key;
// Insert the license key into the database
$wpdb->insert(
$wpdb->prefix . 'llm_licenses',
[
'license_code' => $license_key,
'staging_url' => null, // Set staging URL as NULL
'subscription_id' => $subscription_id,
'plugin_id' => $product_id,
'order_id' => $order_id
]
);
}
// Store the license keys as a single meta field// Debug to check meta data
$item->add_meta_data('_license_keys', implode(', ', $license_keys), true);
$item->save(); // save metadata
error_log('Meta keys for item ' . $item->get_id() . ': ' . print_r($item->get_meta('_license_keys', true), true));
}
应该可以。