我在将自定义下拉列表添加到购物车元并将其显示在购物车中时遇到麻烦。以下代码适用于也在单个产品页面上的数字字段,但由于某些原因不适用于下拉列表。有人知道为什么吗? 代码如下:
// 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;
}
编辑:据我所知,该下拉菜单有一个“选项”,还有一个选项值。为了使一切正常工作,我是否必须单独引用它们?
答案 0 :(得分:1)
您的代码中有一些错误和错误……请改用以下代码(注释):
// 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文件中。经过测试,可以正常工作。
在购物车页面上:
在结帐页面上: