沃梦达 / 编程问答 / php问题 / 正文

将产品自定义字段添加到购物车中,并在WooComm

Add product custom fields to cart and display them everywhere in WooCommerce(将产品自定义字段添加到购物车中,并在WooCommerce中的任何位置显示它们)

本文介绍了将产品自定义字段添加到购物车中,并在WooCommerce中的任何位置显示它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了带有两个复选框的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;

    $colors  = get_field( 'colors',  $product->get_id() );
    $sizes = get_field( 'sizes', $product->get_id() );

    
    
    if( $colors ): ?>
<div class="vorcartbutton">
    <div class="vorcartbuttonlable">
<label for="cars">Farbe wählen:</label>
<select name="farbe" id="farbe">
    <?php foreach( $colors as $color ): ?>
        <option><span class="color"><?php echo $color['label']; ?></span></option>
    <?php endforeach; ?>
</select>
    </div>
<?php endif; ?>
<br>
<?php

if( $sizes ): ?>
    <div class="vorcartbuttonlable">
<label for="cars">Größe wählen:</label>
<select name="groesse" id="groesse">
    <?php foreach( $sizes as $sizes ): ?>
        <option><span class="groesse"><?php echo $sizes['label']; ?></span></option>
    <?php endforeach; ?>
</select>
    </div>
</div>
<?php endif; ?>
<br>
<?php

   
}

// 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 ) {
    $colors  = get_field( 'colors', $cart_item['product_id'] );
    $sizes = get_field( 'sizes', $cart_item['product_id'] );
    
    
 if ( ! empty($colors) ) {
        $custom_items[] = array( "name" => __("Farbe", "woocommerce"),  "value" => $colors  );
    }

    if ( ! empty($sizes) ) {
        $custom_items[] = array( "name" => __("Größe", "woocommerce"), "value" => $sizes );
    }
   
    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 ) {
    $colors  = get_field( 'colors', $values['product_id'] );
    $sizes = get_field( 'sizes', $values['product_id'] );

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

    if ( ! empty($sizes) ) {
        $item->add_meta_data( __("color", "woocommerce"), $sizes );
    }
}
购物车中的值仅显示数组:

有人能把我推向正确的方向,以便在购物车、结账、邮件和订单中获得正确的价值吗?

最初取自此处Display ACF product custom fields everywhere in WooCommerce 3+

推荐答案

代码中存在错误和某些缺失内容。请改用以下重新访问的代码:

// 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;

    $colors = get_field( 'colors',  $product->get_id() );
    if ( ! empty($colors) ) {
        echo '<div class="vorcartbutton">
        <div class="vorcartbuttonlable">
        <label for="farbe">'. __("Farbe wählen") .':</label>
        <select name="farbe" id="farbe">';

        foreach ( $colors as $color ) {
            echo '<option value="'. $color['label'] .'"><span class="color">'. $color['label'] .'</span></option>';
        }
        echo '</select>
        </div><br>';
    }

    $sizes = get_field( 'sizes', $product->get_id() );
    if ( ! empty($sizes) ) {
        echo '<div class="vorcartbutton">
        <div class="vorcartbuttonlable">
        <label for="groesse">'. __("Größe wählen") .':</label>
        <select name="groesse" id="groesse">';

        foreach ( $sizes as $size ) {
            echo '<option value="'. $size['label'] .'"><span class="color">'. $size['label'] .'</span></option>';
        }
        echo '</select>
        </div><br>
        </div>';
    }
}

// Add as custom cart item data
add_filter( 'woocommerce_add_cart_item_data', 'save_acf_as_cart_item_data', 10, 2 );
function save_acf_as_cart_item_data( $cart_item_data, $cart_item ) {
    if ( isset($_POST['farbe']) || isset($_POST['groesse']) ) {
        if ( isset($_POST['farbe']) ) {
            $cart_item_data['farbe'] = esc_attr($_POST['farbe']);
        }
        if ( isset($_POST['groesse']) ) {
            $cart_item_data['groesse'] = esc_attr($_POST['groesse']);
        }
        $cart_item_data['unique_key'] = md5( microtime().rand() );
    }
    return $cart_item_data;
}

// 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 ) {
    if ( isset($cart_item['farbe']) ) {
        $custom_items[] = array( 'name' => __("Farbe", "woocommerce"), 'value' => $cart_item['farbe'] );
    }

    if ( isset($cart_item['groesse']) ) {
        $custom_items[] = array( 'name' => __("Größe", "woocommerce"), 'value' => $cart_item['groesse'] );
    }
    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 ) {
    $colors  = get_field( 'colors', $values['product_id'] );
    $sizes = get_field( 'sizes', $values['product_id'] );

    if ( isset($values['farbe']) ) {
        $item->add_meta_data( 'farbe', $values['farbe'] );
    }

    if ( isset($values['groesse']) ) {
        $item->add_meta_data( 'groesse', $values['groesse'] );
    }
}

// Change displayed label for specific custom order item meta keys
add_filter('woocommerce_order_item_display_meta_key', 'filter_wc_order_item_display_meta_key', 10, 3 );
function filter_wc_order_item_display_meta_key( $display_key, $meta, $item ) {

    // Change displayed label for specific order item meta key
    if( is_admin() && $item->get_type() === 'line_item' ) {
        if( $meta->key === 'farbe' ) {
            $display_key = __("Farbe", "woocommerce");
        }
        if( $meta->key === 'groesse' ) {
            $display_key = __("Größe", "woocommerce");
        }
    }
    return $display_key;
}

代码放在活动子主题(或活动主题)的函数.php文件中。已测试并正常工作。

这篇关于将产品自定义字段添加到购物车中,并在WooCommerce中的任何位置显示它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将产品自定义字段添加到购物车中,并在WooComm

基础教程推荐