[英]Creating dropdown with Woocommerce products and automatically add product to cart based on selection
[英]Add dropdown to products and display the value on Woocommerce cart
我無法將自定義下拉菜單添加到購物車元數據並將其顯示在購物車中。 下面的代碼適用於單個產品頁面上的數字字段,但由於某種原因不適用於下拉列表。 有誰知道為什么? 代碼如下:
// 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;
}
編輯:正如我所見,下拉菜單有一個“選項”,但也有一個選項值。 我是否必須單獨參考它們才能使事情正常進行?
您的代碼中存在一些錯誤和錯誤……請嘗試使用以下代碼(已注釋):
// 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 文件中。 測試和工作。
在購物車頁面:
在結賬頁面:
可以使客戶在購物車的下拉菜單中看到的內容,並且還需要一些價值來感謝您的頁面嗎?
謝謝 LoicTheAztecu。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.