简体   繁体   English

向产品添加下拉菜单并在 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;
}

EDIT: 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;
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。

On cart page:在购物车页面:

在此处输入图片说明

On checkout page:在结账页面:

在此处输入图片说明

It's possible to make what the customer sees on the dropdown display in the cart and also need some value to thank you page?可以使客户在购物车的下拉菜单中看到的内容,并且还需要一些价值来感谢您的页面吗?

Thank you LoicTheAztecu.谢谢 LoicTheAztecu。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Woocommerce 产品创建下拉菜单,并根据选择自动将产品添加到购物车 - Creating dropdown with Woocommerce products and automatically add product to cart based on selection 在 Woocommerce 单个产品的“添加到购物车”下方显示特定的运输类别 - Display specific shipping class below "Add to cart" on Woocommerce single products 在 WooCommerce 简单产品的添加到购物车按钮中显示可用性 - Display availability in add to cart button for WooCommerce simple products WooCommerce 添加<custom>产品到购物车</custom> - WooCommerce Add <custom> products to cart Woocommerce 如何显示购物车中产品的售价 - Woocommerce How to Display Sale Price of Products in Cart WooCommerce-在结帐时显示购物车产品总数 - WooCommerce - Display total number of cart products in checkout WooCommerce:如何以编程方式将一些产品添加到购物车? - WooCommerce: how to add a number of products to the cart programatically? WooCommerce AJAX 添加到购物车 - 仅限简单产品 - WooCommerce AJAX add to cart - simple products only Woocommerce添加到购物车产品仅限4种特定产品 - Woocommerce Add to cart product Restriction to 4 specific products 禁用特定 WooCommerce 产品的添加到购物车按钮 - Disabling Add to Cart Button for Specific WooCommerce Products
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM