无论是白天还是夜晚,
我想在woo commerce结账页面上显示特定类别(衬衫)的自定义产品字段值。我已成功检查类别,但打印产品属性的尝试失败。我会偏离轨道吗?
add_action(\'woocommerce_before_calculate_totals\', \'personalization_check_category_in_cart\');
function personalization_check_category_in_cart() {
// Set $cat_in_cart to false
$cat_in_cart = false;
// Loop through all products in the Cart
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// If Cart has category term, set $cat_in_cart to true
if ( has_term( \'Personalize\', \'product_cat\', $cart_item[\'product_id\'] ) ) {
$cat_in_cart = true;
break;
}
}
if ( $cat_in_cart ) {
// Print product
echo \'<div class="checkout-personalization-message-container><p>\';
// === ? Print product custom field value here ? ===
//global $product;
//echo wc_display_product_attributes( $product ); // doesn\'t work
//global $product;
//$product->get_attribute( \'product_personalization_note\' ); // doesn\'t work
//get_post_meta( $order->get_id(), \'product_personalization_note\', true ); // doesn\'t work
echo \'</p></div>\';
}
}
参考:可以找到生成自定义产品字段值的代码
here:
最合适的回答,由SO网友:RiddleMeThis 整理而成
试试这样,我现在无法测试。
add_action(\'woocommerce_before_calculate_totals\', \'personalization_check_category_in_cart\');
function personalization_check_category_in_cart() {
// loop through all products in the Cart
foreach (WC()->cart->get_cart() as $cart_item) {
// if cart has category term, set variable $cat_in_cart to true
if (has_term( \'Personalize\', \'product_cat\', $cart_item[\'product_id\'])) {
$product = $cart_item[\'data\']; // get product data
$product_id = $product->get_id(); // get product ID
$note = get_post_meta( $product->get_id(), \'product_personalization_note\', true ); // get meta
echo \'<div class="checkout-personalization-message-container"><p>\' . $note . \'</p></div>\';
}
}
}