【问题标题】:Creating dropdown with Woocommerce products and automatically add product to cart based on selection使用 Woocommerce 产品创建下拉菜单并根据选择自动将产品添加到购物车
【发布时间】:2021-06-21 00:20:29
【问题描述】:

使用Products dropdown from same category inside Woocommerce product short description 回答代码,我设法在 WooCommerce 中创建了一个产品下拉列表。

<select> 标签上,我想调用一个 php 函数,该函数根据从下拉列表中选择的选项将产品添加到购物车中。

我在代码中更改了<select> 标记,内容如下:

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

将 woocommerce 产品添加到购物车的 PHP 函数:

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

由于某种原因,当我加载页面时会调用该函数。

【问题讨论】:

  • 抱歉,我已经编辑了问题
  • 请阅读How to Ask。一个简单的“我想要”在这里不被认为是一个合适的问题。

标签: php wordpress woocommerce dropdown product


【解决方案1】:

要获得可以添加到购物车的同一产品类别的相关产品的下拉列表,需要使用 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();
}

代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

代码将显示带有添加到购物车按钮的简单产品(来自相同产品类别)的下拉列表。


用法

1)。对于单个产品页面:只需在产品简短描述(或描述)中粘贴以下短代码:

[products_dropdown]

2)。对于单个产品页面,在 php 代码中:

echo do_shortcode("[products_dropdown]");

3)。在文本编辑器中的任何帖子或页面上,定义 product_id 参数(在定义的产品 ID 下方是 37

[products_dropdown product_id="37"]

在选择产品之前,添加到购物车按钮会被禁用:

当在下拉列表中选择产品时,添加到购物车按钮被激活,并且可以将所选产品添加到购物车:

【讨论】:

  • 当我从下拉列表中选择产品时,可以使用我在问题中编写的添加到购物车方法自动将产品添加到购物车?
  • @CuldaSergiu 不,它不能,onchange 属性事件只能调用 javascript 函数,你不能用它调用 PHP 函数,因为它是客户端事件。由于我的代码有效并回答了您最初的问题,您可以请accept 回答,谢谢。
猜你喜欢
  • 2014-11-21
  • 1970-01-01
  • 2021-08-22
  • 2015-03-31
  • 2020-10-15
  • 2019-08-01
  • 2014-02-06
  • 1970-01-01
  • 2017-05-11
相关资源
最近更新 更多