在 WooCommerce 3+ 中随处显示 ACF 产品自定义字段

问题描述 投票:0回答:2

我正在使用 WooCommerce 和高级自定义字段 (ACF) 插件构建一个网站。我创建了两个 ACF 自定义字段,名称分别为“plant”和“amount”。

我试图在前端显示它们,但目前只有自定义字段“植物”随处显示。我在网上查了一圈没有运气。

add_action( 'woocommerce_before_add_to_cart_button', 'add_custom_field', 0 );

function add_custom_field() {
    global $product;             // Changed this

    // Added this too (compatibility with WC +3) 
    $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;

    echo "<div class='produto-informacoes-complementares'>";
    echo get_field( 'plant', $product_id );
    echo "</div>";
    echo "<div class='produto-informacoes-complementares'>";
    echo get_field( 'amount', $product_id );
    echo "</div>";


    return true;
}


add_filter( 'woocommerce_add_cart_item_data', 'save_my_custom_product_field', 10, 2 );

function save_my_custom_product_field( $cart_item_data, $product_id ) {

    $custom_field_value = get_field( 'plant', $product_id, true );
     $custom_field_value = get_field( 'amount', $product_id, true );

    if( !empty( $custom_field_value ) ) 
    {
        $cart_item_data['plant'] = $custom_field_value;
         $cart_item_data['amount'] = $custom_field_value;

        // below statement make sure every add to cart action as unique line item
        $cart_item_data['unique_key'] = md5( microtime().rand() );
    }
    return $cart_item_data;
}

add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );

function render_meta_on_cart_and_checkout( $cart_data, $cart_item ) {
    $custom_items = array();
    // Woo 2.4.2 updates
    if( !empty( $cart_data ) ) {
        $custom_items = $cart_data;
    }
    if( isset( $cart_item['plant'] )) {
        $custom_items[] = array( "name" => "גודל עציץ", "value" => $cart_item['plant'] );
        $custom_items[] = array( "name" => "כמות במגש", "value" => $cart_item['amount'] );
        
    }
    return $custom_items;
}

function add_order_item_meta_acf( $item_id, $values ) {

    wc_add_order_item_meta( $item_id, 'גודל עציץ', $values [ 'plant' ] );
    wc_add_order_item_meta( $item_id, 'גודל עציץ', $values [ 'amount' ] );
  }
add_action( 'woocommerce_add_order_item_meta', 'add_order_item_meta_acf' , 10, 2);
php wordpress woocommerce product advanced-custom-fields
2个回答
3
投票

您的代码有点过时并且有一些错误。使用以下内容在各处显示产品 ACF 自定义字段(产品页面、购物车、结账、订单和电子邮件通知):

// Display on product page
add_action( 'woocommerce_before_add_to_cart_button', 'display_acf_single_product_pages', 1 );
function display_acf_single_product_pages() {
    global $product;

    $plant  = get_field( 'plant',  $product->get_id() );
    $amount = get_field( 'amount', $product->get_id() );

    if ( ! empty($plant) && ! empty($amount) ) {
        echo '<div class="produto-informacoes-complementares">';

        if ( ! empty($plant) ) {
            echo '<div class="plant"><strong>' . __("Size", "woocommerce") . '</strong>: ' . $plant . '</div>';
        }

        if ( ! empty($amount) ) {
            echo '<div class="amount"><strong>' . __("Amount", "woocommerce") . '</strong>: ' . $amount . '</div>';
        }

        echo '</div>';
    }
}

// Display on cart and checkout
add_filter( 'woocommerce_get_item_data', 'display_acf_on_cart_and_checkout', 10, 2 );
function display_acf_on_cart_and_checkout( $cart_data, $cart_item ) {
    $plant  = get_field( 'plant', $cart_item['product_id'] );
    $amount = get_field( 'amount', $cart_item['product_id'] );

    if ( ! empty($plant) ) {
        $custom_items[] = array( "name" => __("Size", "woocommerce"),  "value" => $plant  );
    }

    if ( ! empty($amount) ) {
        $custom_items[] = array( "name" => __("Amount", "woocommerce"), "value" => $amount );
    }
    return $custom_items;
}

// Display on orders and email notifications (save as custom order item meta data)
add_action( 'woocommerce_checkout_create_order_line_item', 'display_acf_on_orders_and_emails', 10, 4 );
function display_acf_on_orders_and_emails( $item, $cart_item_key, $values, $order ) {
    $plant  = get_field( 'plant', $values['product_id'] );
    $amount = get_field( 'amount', $values['product_id'] );

    if ( ! empty($plant) ) {
        $item->add_meta_data( __("Size", "woocommerce"), $plant );
    }

    if ( ! empty($amount) ) {
        $item->add_meta_data( __("Amount", "woocommerce"), $amount );
    }
}

代码位于活动子主题(或活动主题)的functions.php 文件中。已测试并有效。


0
投票

效果很好!但我只想在管理电子邮件中输出它。代码应该如何定制? 谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.