【问题标题】:Add dropdown to products and display the value on Woocommerce cart向产品添加下拉菜单并在 Woocommerce 购物车上显示价值
【发布时间】:2019-08-01 13:37:15
【问题描述】:

我无法将自定义下拉菜单添加到购物车元数据并将其显示在购物车中。下面的代码适用于单个产品页面上的数字字段,但由于某种原因不适用于下拉列表。有谁知道为什么? 代码如下:

// 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;
}

编辑:在我看来,下拉菜单有一个“选项”,但也有一个选项值。我是否必须单独引用它们才能使事情发挥作用?

【问题讨论】:

    标签: php woocommerce product cart checkout


    【解决方案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 文件中。经过测试并且可以工作。

    在购物车页面上:

    结帐页面:

    【讨论】:

    • 谢谢你,效果很好:) 是否也可以让客户在购物车的下拉显示中看到什么?如果客户选择“Spleis 400,-”,他/她在购物车中看到的也是?并将该选项的数值添加到购物车元数据中以计算价格?
    • 我想我可以创建一个与标签同名的属性?
    • @Paudun 也许,你需要先尝试一下,如果你有问题,再问一个新的与这个答案代码相关的问题……然后你可以在这里通知我。
    • 我正在复制下拉列表,因为绳子有两端。我是否也在第二个下拉列表中添加唯一键?($cart_item_data['unique_key'] = md5(microtime().rand());)
    • @Paudun 唯一键允许在将具有自定义数据的商品添加到购物车时进行分离,如果客户添加第二个相同商品,则避免使用数量增加的(分组)商品。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    • 2021-06-21
    • 1970-01-01
    • 2021-12-29
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多