admin管理员组文章数量:1434916
Good day or night,
I would like to display a custom product field value of a specific category (Shirts) on a woo commerce checkout page. I've succeeded in checking for the category, but my attempts to print the product attribute have failed. Where am I going off track?
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>';
}
}
For reference: the code that produced the custom product field value can be found here:
Good day or night,
I would like to display a custom product field value of a specific category (Shirts) on a woo commerce checkout page. I've succeeded in checking for the category, but my attempts to print the product attribute have failed. Where am I going off track?
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>';
}
}
For reference: the code that produced the custom product field value can be found here:
Share Improve this question edited Apr 5, 2019 at 15:10 hamburger asked Apr 5, 2019 at 15:01 hamburgerhamburger 351 silver badge8 bronze badges 1- Are you getting inside your first If statement? if ( has_term( 'Personalize'... – RiddleMeThis Commented Apr 5, 2019 at 16:14
1 Answer
Reset to default 1Try something like this, I am unable to test this at the moment.
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>';
}
}
}
版权声明:本文标题:php - How to display a custom product field value of a specific category on a Woo Commerce checkout page? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745623477a2666827.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论