向产品添加下拉菜单,并在Woocommerce购物车上显示价值 [英] Add dropdown to products and display the value on Woocommerce cart
问题描述
我无法将自定义下拉列表添加到购物车元并将其显示在购物车中。以下代码适用于也在单个产品页面上的数字字段,但由于某些原因不适用于下拉列表。有人知道为什么吗?
代码如下:
I'm having trouble adding a custom dropdown to the cart meta and displaying it in the cart. The code below works for a number field that is also on the single product page, but not for the dropdown for some reason. Does anyone know why? The code is as follows:
// Dropdowns-in-ends starts here
add_action( 'woocommerce_before_add_to_cart_button', 'func_dropdown_in_ends');
function func_dropdown_in_ends() {
printf(
'<div class="class_dropdown_ends"><label for="id_dropdown_one_end">I andre enden av tauet</label><id = id_dropdown_one_end name=dropdown_one_end>
<select>
<option value="0">Ingenting</option>
<option value="250">Øye 250,-</option>
<option value="350">Sjakkel 350,-</option>
<option value="400">Spleis 400,-</option>
</select></div>'
);
}
//Add dropdown_one_end to cart meta when user clicks add to cart
add_filter( 'woocommerce_add_cart_item_data', 'func_add_dropdown_ene_enden', 10, 4 );
function func_add_dropdown_ene_enden( $cart_item_data, $product_id, $variation_id) {
if( ! empty( $_POST['dropdown_ene_one_end'] ) ) {
// Add the item data
$cart_item_data['one_end'] = $_POST['dropdown_one_end'];
}
return $cart_item_data;
}
// Show dropdown_one_end in cart
add_filter( 'woocommerce_cart_item_name', 'func_cart_item_name_ene_enden', 10, 3 );
function func_cart_item_name_ene_enden( $name, $cart_item, $cart_item_key ) {
if( isset( $cart_item['one_end'] ) ) {
$name .= sprintf(
'<p>I ene enden: %s</p>',
esc_html( $cart_item['one_end'])
);
}
return $name;
}
编辑: 正如我所看到的,下拉菜单有一个选项,还有一个选项值。为了使一切正常工作,我必须单独引用它们吗?
As I see it the dropdown has an 'option', but also an option value. Do I have to reference them individually to get things to work?
推荐答案
您的代码中有一些错误和错误……尝试改为使用以下代码(注释):
There are some errors and mistakes in you code… Try the following code instead (commented):
// Display a custom dropdown in single product pages before add_to_cart button
add_action( 'woocommerce_before_add_to_cart_button', 'display_dropdown_in_ends');
function display_dropdown_in_ends() {
echo '<div class="class_dropdown_ends">
<label for="id_dropdown_one_end">I andre enden av tauet</label>
<select id ="id_dropdown_one_end" name="dropdown_one_end">
<option value="0">Ingenting</option>
<option value="250">Øye 250,-</option>
<option value="350">Sjakkel 350,-</option>
<option value="400">Spleis 400,-</option>
</select>
</div>';
}
// Add dropdown value as custom cart item data, on add to cart
add_filter( 'woocommerce_add_cart_item_data', 'add_dropdown_value_to_cart_item_data', 10, 4 );
function add_dropdown_value_to_cart_item_data( $cart_item_data, $product_id, $variation_id) {
if( isset($_POST['dropdown_one_end']) && ! empty($_POST['dropdown_one_end']) ) {
// Add the dropdown value as custom cart item data
$cart_item_data['one_end'] = esc_attr($_POST['dropdown_one_end']);
$cart_item_data['unique_key'] = md5(microtime().rand()); // Make each added item unique
}
return $cart_item_data;
}
// Cart page: Display dropdown value after the cart item name
add_filter( 'woocommerce_cart_item_name', 'display_dropdown_value_after_cart_item_name', 10, 3 );
function display_dropdown_value_after_cart_item_name( $name, $cart_item, $cart_item_key ) {
if( is_cart() && isset($cart_item['one_end']) ) {
$name .= '<p>'.__("I ene enden:") . ' ' . esc_html($cart_item['one_end']) . '</p>';
}
return $name;
}
// Checkout page: Display dropdown value after the cart item name
add_filter( 'woocommerce_checkout_cart_item_quantity', 'display_dropdown_value_after_cart_item_quantity', 10, 3 );
function display_dropdown_value_after_cart_item_quantity( $item_qty, $cart_item, $cart_item_key ) {
if( isset($cart_item['one_end']) ) {
$item_qty .= '<p>'.__("I ene enden:") . ' ' . esc_html($cart_item['one_end']) . '</p>';
}
return $item_qty;
}
代码进入活动子主题(或活动主题)的function.php文件)。经过测试并有效。
Code goes in function.php file of your active child theme (or active theme). Tested and works.
在购物车页面上:
在结帐页面:
这篇关于向产品添加下拉菜单,并在Woocommerce购物车上显示价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!