WooCommerce 从购物车中获取产品自定义元数据

WooCommerce get product custom meta from cart

在我的 WooCommerce 商店中,客户只能购买一种产品。 他们可以多次将这个单一产品添加到购物车,但具有不同的自定义元值。 客户可以从购物车编辑他们的产品(WooCommerce 默认功能)并更改他们最初填写的元数据。

在我的 functions.php 中,我有以下代码:

function add_names_on_tshirt_field() {   
    global $post;
    $name_one_on_tshirt = "";
    $name_two_on_tshirt = "";
    $name_three_on_tshirt = "";

    foreach ( WC()->cart->cart_contents as $key => $value ) {
        if( $value["product_id"] == $post->ID && isset( $value["name_one_on_tshirt"] ) ) {
            $name_one_on_tshirt = $value["name_one_on_tshirt"];
        }
        if( $value["product_id"] == $post->ID && isset( $value["name_two_on_tshirt"] ) ) {
            $name_two_on_tshirt = $value["name_two_on_tshirt"];
        }
        if( $value["product_id"] == $post->ID && isset( $value["name_three_on_tshirt"] ) ) {
            $name_three_on_tshirt = $value["name_three_on_tshirt"];
        }
    }

    echo '<table class="variations" cellspacing="0">
      <tbody>
          <tr>
            <td class="label"><label for="color">Name 1 on T-Shirt</label></td>
            <td class="value">
                <input type="text" name="name-one-on-tshirt" value="'. esc_attr( $name_one_on_tshirt ) .'" />
            </td>
        </tr>
        <tr>
            <td class="label"><label for="color">Name 2 on T-Shirt</label></td>
            <td class="value">
                <input type="text" name="name-two-on-tshirt" value="'. esc_attr( $name_two_on_tshirt ) .'" />
            </td>
        </tr>
        <tr>
            <td class="label"><label for="color">Name 3 on T-Shirt</label></td>
            <td class="value">
                <input type="text" name="name-three-on-tshirt" value="'. esc_attr( $name_three_on_tshirt ) .'" />
            </td>
        </tr>
      </tbody>
    </table>';
}
add_action( 'woocommerce_before_add_to_cart_button', 'add_names_on_tshirt_field' );

问题出在我的客户尝试编辑购物车中的产品时。因为所有产品都有相同的 ID(我只有一个产品),最后添加到购物车的产品的元数据被加载到文本字段中。我目前使用产品 ID 在我的 foreach 中检索元数据,但该 ID 与我购物车中的所有其他产品相同。是否有另一种方法可以在不使用 product_id/$post->ID 的情况下检索元数据,例如 cart_item_key 或使用会话?

这是实现该目标的方法

您将必须编辑您的 your-theme/woocommerce/cart.php(尤其是产品缩略图 TD 和产品名称 TD)

<?php

$query_str = "";

if( isset( $cart_item["name_one_on_tshirt"] ) ) {
    $query_str .= "?name_one_on_tshirt=" . $cart_item["name_one_on_tshirt"];
}

if( isset( $cart_item["name_two_on_tshirt"] ) ) {
    if( $query_str == "" ) {
        $query_str .= "?name_two_on_tshirt=" . $cart_item["name_two_on_tshirt"];
    } else {
        $query_str .= "&name_two_on_tshirt=" . $cart_item["name_two_on_tshirt"];
    }
}

if( isset( $cart_item["name_three_on_tshirt"] ) ) {
    if( $query_str == "" ) {
        $query_str .= "?name_three_on_tshirt=" . $cart_item["name_three_on_tshirt"];
    } else {
        $query_str .= "&name_three_on_tshirt=" . $cart_item["name_three_on_tshirt"];
    }
}
?>

<td class="product-thumbnail">
<?php
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );

if ( ! $_product->is_visible() ) {
    echo $thumbnail;
} else {    
    printf( '<a href="%s">%s</a>', esc_url( $_product->get_permalink( $cart_item ) . $query_str ), $thumbnail );    
}
?>
</td>

<td class="product-name" data-title="<?php _e( 'Product', 'woocommerce' ); ?>">
    <?php
        if ( ! $_product->is_visible() ) {
            echo apply_filters( 'woocommerce_cart_item_name', $_product->get_title(), $cart_item, $cart_item_key ) . ' ';
        } else {         
            echo apply_filters( 'woocommerce_cart_item_name', sprintf( '<a href="%s">%s</a>', esc_url( $_product->get_permalink( $cart_item ) . $query_str ), $_product->get_title() ), $cart_item, $cart_item_key );                                        
        } 
        // Meta data
        echo WC()->cart->get_item_data( $cart_item );

            // Backorder notification
        if ( $_product->backorders_require_notification() && $_product->is_on_backorder( $cart_item['quantity'] ) ) {
            echo '<p class="backorder_notification">' . esc_html__( 'Available on backorder', 'woocommerce' ) . '</p>';
        }
    ?>
</td>

然后像这样更新您的 add_name_on_tshirt_field 函数

function add_names_on_tshirt_field() {
    global $post;
    $name_one_on_tshirt = "";
    $name_two_on_tshirt = "";
    $name_three_on_tshirt = "";

    if( isset( $_GET["name_one_on_tshirt"] ) ) {
        $name_one_on_tshirt = $_GET["name_one_on_tshirt"];
    }

    if( isset( $_GET["name_two_on_tshirt"] ) ) {
        $name_two_on_tshirt = $_GET["name_two_on_tshirt"];
    }

    if( isset( $_GET["name_three_on_tshirt"] ) ) {
        $name_three_on_tshirt = $_GET["name_three_on_tshirt"];
    }

    echo '<table class="variations" cellspacing="0">
      <tbody>
          <tr>
            <td class="label"><label for="color">Name 1 on T-Shirt</label></td>
            <td class="value">
                <input type="text" name="name-one-on-tshirt" value="'. esc_attr( $name_one_on_tshirt ) .'" />
            </td>
        </tr>
        <tr>
            <td class="label"><label for="color">Name 2 on T-Shirt</label></td>
            <td class="value">
                <input type="text" name="name-two-on-tshirt" value="'. esc_attr( $name_two_on_tshirt ) .'" />
            </td>
        </tr>
        <tr>
            <td class="label"><label for="color">Name 3 on T-Shirt</label></td>
            <td class="value">
                <input type="text" name="name-three-on-tshirt" value="'. esc_attr( $name_three_on_tshirt ) .'" />
            </td>
        </tr>
      </tbody>
    </table>';
}
add_action( 'woocommerce_before_add_to_cart_button', 'add_names_on_tshirt_field' );

所以基本上我所做的是,我将它们添加为 cart.php 中的查询变量,并在 add_name_on_tshirt_field 中检查相应的查询变量,如果我找到一个,那么我们可以填充该字段值属性。