我使用WooCommerce Tiered Pricing Table plugin,我希望能够在管理面板中看到它们中的哪一个是由这个插件购买的(订单信息的细节). 我已经修改了这个代码,但没有工作,只显示否. 请查一下,帮我查一下.

如有任何帮助,我们不胜感激.

/*Track purchases made through WooCommerce Tiered Pricing Table plugin.*/
function track_purchases_from_tiered_pricing_table($order_id) {
    // Get the order object
    $order = wc_get_order($order_id);
    
    // Loop through the order items
    foreach ($order->get_items() as $item_id => $item) {
        // Get the product object
        $product = $item->get_product();
        
        // Check if the Tiered Pricing Table plugin file is being loaded
        if (class_exists('Tier_Pricing_Table')) {
            // Update the order meta to indicate the purchase from the plugin
            update_post_meta($order_id, '_purchased_from_tiered_pricing_table', 'yes');
            break; // Exit the loop if a product from the plugin is found
        }
    }
}
add_action('woocommerce_checkout_order_processed', 'track_purchases_from_tiered_pricing_table');

 //Display the purchased from Tiered Pricing Table plugin information in the admin panel
function display_purchased_from_tiered_pricing_table_info($order) {
    // Get the order ID
    $order_id = $order->get_id();
    
    // Get the purchased from Tiered Pricing Table plugin meta value
    $purchased_from_tiered_pricing_table = get_post_meta($order_id, '_purchased_from_tiered_pricing_table', true);
    
    if ($purchased_from_tiered_pricing_table === 'yes') {
        echo '<p><strong>Purchased from Tiered Pricing Table plugin: </strong>Yes</p>';
    } else {
        echo '<p><strong>Purchased from Tiered Pricing Table plugin: </strong>No</p>';
    }
}
add_action('woocommerce_admin_order_data_after_billing_address', 'display_purchased_from_tiered_pricing_table_info');

推荐答案

如果项目是分级定价的,以下将向订单添加自定义字段,并将其显示在管理订单页面中:

// Track purchases made through WooCommerce Tiered Pricing Table plugin.
add_action( 'woocommerce_checkout_create_order', 'track_purchases_from_tiered_pricing_table', 10, 2 );
function track_purchases_from_tiered_pricing_table( $order, $data ) {
    $has_tiered_pricing = false; // Initializing
    
    // Loop through the cart items
    foreach ( WC()->cart->get_cart() as $item ) {
        // Loop  through extra meta keys from the product
        foreach ( wp_list_pluck( $item['data']->get_meta_data(), 'key' ) as $key ) {
            // Check for a meta_key that has 'tiered_pric' substring
            if ( strpos($key, 'tiered_pric') !== false ) {
                $has_tiered_pricing = true;
                break;
            }
        }
        // If an tiered priced item is found, flag the order
        if ( $has_tiered_pricing ) {
            $order->update_meta_data( '_has_tiered_pricing', '1' );
            break;
        }
    }
}

 // Display the purchased from Tiered Pricing Table plugin information in the admin panel
add_action('woocommerce_admin_order_data_after_billing_address', 'display_purchased_from_tiered_pricing_table_info');
function display_purchased_from_tiered_pricing_table_info($order) {
    // Display if the order has tiered priced items
    printf( '<p><strong>Purchased from Tiered Pricing Table plugin: </strong>%s</p>', 
         $order->get_meta( '_has_tiered_pricing' ) ? 'Yes' : 'No' 
    );
}

代码放在活动子主题(或活动主题)的函数.php文件中.经过测试,效果良好.


Addition:只瞄准折扣阶梯价格项目

如果您只想添加此订单元数据,则对于已计算出阶梯价格(即折扣式阶梯价格)的项目,请改用以下选项:

// Track purchases made through WooCommerce Tiered Pricing Table plugin.
add_action( 'woocommerce_checkout_create_order', 'track_purchases_from_tiered_pricing_table', 10, 2 );
function track_purchases_from_tiered_pricing_table( $order, $data ) {
    $has_tiered_pricing = false; // Initializing
    
    // Loop through the cart items
    foreach ( WC()->cart->get_cart() as $item ) {
        // Loop  through extra meta key /value pairs from the product
        foreach ( wp_list_pluck( $item['data']->get_meta_data(), 'value', 'key' ) as $key => $value ) {
            // Check for 'tiered_pricing_cart_price_calculated' meta key  
            if ( $key === 'tiered_pricing_cart_price_calculated' && $value === 'yes' ) {
                $has_tiered_pricing = true;
                break;
            }
        }
        // If an tiered priced item is found, flag the order
        if ( $has_tiered_pricing ) {
            $order->update_meta_data( '_has_tiered_pricing', '1' );
            break;
        }
    }
}

 // Display the purchased from Tiered Pricing Table plugin information in the admin panel
add_action('woocommerce_admin_order_data_after_billing_address', 'display_purchased_from_tiered_pricing_table_info');
function display_purchased_from_tiered_pricing_table_info($order) {
    // Display if the order has tiered priced items
    printf( '<p><strong>Purchased from Tiered Pricing Table plugin: </strong>%s</p>', 
         $order->get_meta( '_has_tiered_pricing' ) ? 'Yes' : 'No' 
    );
}

代码放在活动子主题(或活动主题)的函数.php文件中.经过测试,效果良好.

enter image description here


Addition 2:

如何查看购物车项目(产品)保护的额外元数据(键/值对).

这个插件专门为购物车项目(产品)生成额外的元数据.

下面的代码将在购物车和 checkout 页面(only for admins)的底部显示额外的受保护元数据,这样您就可以使用正确的键/值,使我的代码(accordingly to your product settings with this plugin)正常工作:

// Displaying cart items (product) extra protected metadata in the bottom of cart and checkout pages
add_action('woocommerce_after_cart', 'display_product_extra_metadata_for_admins'); // cart
add_action('woocommerce_after_checkout_form', 'display_product_extra_metadata_for_admins'); // checkout
function display_product_extra_metadata_for_admins() {
    if( ! current_user_can('administrator') ) return; // Only admins

    echo '<pre>';
    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $item ) {
        echo '<p>Name: '.$item['data']->get_name() . ' | Quantity:' .  $item['quantity'] . '</p>';
        print_r( wp_list_pluck( $item['data']->get_meta_data(), 'value', 'key' ) ); 
        echo '<br>';
    }
    echo '</pre>';
}

完成后,您可以将其删除或禁用.

Php相关问答推荐

在不刷新页面的情况下无法使用JQuery更新id

将产品类别添加到WooCommerce产品附加信息

在Google云存储对象上从PHP设置缓存控制TTL会更改错误的元数据

有没有办法为整个PHP应用程序启用严格类型

如何实现对数据库表中多列的唯一约束?

WooCommerce在购买后多次重定向

如何在Livewire 3 Form类中做依赖注入?

在PHP中处理Linux输入事件(/dev/input/Event*)

根据WooCommerce中的自定义用户元数据值更改用户角色

使用 PHP,我可以将 foreach 语句放入瞬态中吗?

Woocommerce API - 图像问题

如何将不同的 Woocommerce 费用合并为一个名称?

PHP Laravel 迁移文件不创建关系

过滤会员计划标题和标签

WooCommerce 按产品运输插件:仅收取较高的运输费用

使用动态地址和动态文件创建zip文件并获取zip文件链接

在特征中使用类的属性

Woocommerce单个添加到购物车页面中的产品动态定价

将样式文件或脚本引入WordPress模板

PHP - 如何获取发送到脚本的查询参数