简体   繁体   English

使用 Woocommerce 产品创建下拉菜单,并根据选择自动将产品添加到购物车

[英]Creating dropdown with Woocommerce products and automatically add product to cart based on selection

Using Products dropdown from same category inside Woocommerce product short description answer code, I managed to create a products dropdown in WooCommerce.使用Woocommerce 产品简短描述答案代码中同一类别的产品下拉列表,我设法在 WooCommerce 中创建了一个产品下拉列表。

On the <select> tag I wanted to call a php function which adds the product to the cart based on the selected option from dropdown.<select>标签上,我想调用一个 php function,它根据下拉列表中的选定选项将产品添加到购物车。

I changed in the code the <select> tag with the following:我在代码中更改了<select>标记,如下所示:

<select name="products-select" id="products-select" onchange="'.add_to_cart().'">

Php function to add woocommerce products to cart: Php function 将 woocommerce 产品添加到购物车:

function add_to_cart(){
    WC()->cart->add_to_cart( 1147 ); 
}

For some reason the function is called when I load the page.由于某种原因,当我加载页面时会调用 function。

To get a dropdown of related products from same product category that can be added to cart, It requires to use javascript making some changes in the code like:要获得可添加到购物车的同一产品类别的相关产品的下拉列表,需要使用 javascript 对代码进行一些更改,例如:

add_shortcode( 'products_dropdown', 'wc_products_from_cat_dropdown' );
function wc_products_from_cat_dropdown( $atts ) {
    // Shortcode Attributes
    $atts = shortcode_atts( array(
        'product_id' => '',
    ), $atts, 'products_dropdown' );

    $product_id = is_product() ? get_the_id() : $atts['product_id'];

    if ( empty($product_id) )
        return;

    ob_start();

    $query = new WP_Query( array(
        'post_type'      => 'product',
        'post_status'    => 'publish',
        'posts_per_page' => '-1',
        'post__not_in'     => array( $product_id ),
        'tax_query' => array( array(
                'taxonomy' => 'product_cat',
                'field'    => 'term_id',
                'terms'    => wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'ids' ) ),
            ),
            array(
                'taxonomy' => 'product_type',
                'field'    => 'name',
                'terms'    => 'simple',
        ) ),
    ) );

    if ( $query->have_posts() ) :

    echo '<div class="products-dropdown"><select name="products-select" id="products-select">
    <option value="">'.__('Choose a related product').'</option>';

    while ( $query->have_posts() ) : $query->the_post();

    $type = wp_get_post_terms( get_the_ID(), 'product_type', ['fields','slug'] );
    print_pr($type);

    echo '<option value="?add-to-cart='.get_the_ID().'">'.get_the_title().'</option>';

    endwhile;

    echo '</select><br><a class="button disabled" href="">'.__("add to cart").'</a></div>';

    wp_reset_postdata();

    endif;

    ?>
    <script type='text/javascript'>
        jQuery(function($){
            var a = '.products-dropdown', b = a+' .button', d = 'disabled', s = a+' select';
            $(document.body).on('change', s, function(){
                var qv = $(this).val();
                $(b).attr('href', qv);
                if ( qv != '' ) {
                    $(b).removeClass(d).attr('href', $(this).val());
                } else if ( qv == '' && ! $(b).hasClass(d) ) {
                    $(b).addClass(d);
                }
            }).on('click', b, function(e){
                if ( $(b).attr('href') == '' ) {
                    e.preventDefault();
                }
            });

        });
    </script>
    <?php

    return ob_get_clean();
}

Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 Tested and works.测试和工作。

The code will show a dropdown of simple products (from same product category(ies)) that with an add to cart button.该代码将显示带有添加到购物车按钮的简单产品(来自相同产品类别)的下拉列表。


USAGE用法

1). 1)。 For single product pages: Just paste the following shortcode in the product short description (or descrition):对于单个产品页面:只需在产品简短描述(或描述)中粘贴以下短代码:

[products_dropdown]

2). 2)。 For single product pages, inside php code:对于单个产品页面,在 php 代码内:

echo do_shortcode("[products_dropdown]");

3). 3)。 on any post or page within the text editor, define the product_id argument (below the defined product id is 37 ) :在文本编辑器中的任何帖子或页面上,定义 product_id 参数(在定义的产品 id 下方为37

[products_dropdown product_id="37"]

The add to cart button is disabled until a product is selected:在选择产品之前,添加到购物车按钮被禁用:

在此处输入图像描述

When a product is selected in the dropdown, the add to cart button get activated, and the selected product can be added to cart:在下拉列表中选择产品时,将激活添加到购物车按钮,并且可以将所选产品添加到购物车:

在此处输入图像描述

暂无
暂无

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

相关问题 根据范围条件自动将产品添加到WooCommerce购物车 - Automatically add products to WooCommerce cart based on range conditions Woocommerce添加到购物车产品仅限4种特定产品 - Woocommerce Add to cart product Restriction to 4 specific products 向产品添加下拉菜单并在 Woocommerce 购物车上显示价值 - Add dropdown to products and display the value on Woocommerce cart 在访问WooCommerce时自动将多种产品添加到购物车 - Automatically add multiple products to cart on visit WooCommerce 自动添加或删除 Woocommerce 购物车中的免费产品,但购物车上没有订阅产品 - Add or remove automatically a free product in Woocommerce cart but not with subscription product on the cart 在 Woocommerce 购物车中自动添加或删除免费产品 - Add or remove automatically a free product in Woocommerce cart 根据 WooCommerce 中的产品属性添加到购物车验证 - Add to cart validation based on product attribute in WooCommerce 允许根据 WooCommerce 中的购物车总数将特定产品添加到购物车 - Allow add to cart for specific products based on cart total in WooCommerce 如何在woocommerce购物车页面中基于当前产品添加相关产品? - How to add related products based on current products in woocommerce cart page? WooCommerce AJAX放入购物车以获取可变产品,仍重定向到产品页面 - WooCommerce AJAX Add to Cart for Variable products, still redirecting to product page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM