如果项目是分级定价的,以下将向订单添加自定义字段,并将其显示在管理订单页面中:
// 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文件中.经过测试,效果良好.
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>';
}
完成后,您可以将其删除或禁用.