在我的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
或使用会话?
答案 0 :(得分:0)
以下是如何实现这一目标的想法
您必须修改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中我检查相应的查询var,如果我找到一个,那么我们可以填写那个字段值属性。