php - 向 WooCommerce 中的特定产品添加额外的添加到购物车按钮

标签 php jquery wordpress woocommerce product

在 Woocommerce 中,我想添加一个具有特定数量的额外添加到购物车按钮。

答案 additional add to cart button with fixed quantity in woocommerce single product pages 大部分不是,我只需要为任何产品类型的特定产品启用该代码。

这可能吗?我必须改变什么......

最佳答案

要处理产品变体(在可变产品上)要复杂得多,需要添加一些 JavaScript/jQuery 代码。

我在自定义条件函数中将产品设置限制与主要函数分开,您可以在两种方式之间进行选择:

1) 您可以使用产品 ID 限制:

// PRODUCT IDs based ::: Conditional function
function enable_additional_add_to_cart_button( $product_id ) {
    // HERE define your Product Ids in the array
    $product_ids = array(37, 40, 53);

    return in_array( $product_ids, $product_id ) ? true : false;
}

2) 或者您可以使用产品类别限制:

// PRODUCT CATEGORY based ::: Conditional function
function enable_additional_add_to_cart_button( $product_id ) {
    // HERE define your Product Category terms in the array (can be term Ids, names or slugs)
    $terms = array('t-shirts', 'socks', 'glasses');

    return has_term( $terms, 'product_cat', $product_id ) ? true : false;
}

下面是主要函数,它将显示一个额外的“添加到购物车”按钮,并与上面的条件函数之一一起使用。

// Additional add to cart button with fixed bulk quantity
add_action( 'woocommerce_after_add_to_cart_button', 'additional_add_to_cart_button', 20 );
function additional_add_to_cart_button() {
    global $product;

    if( ! enable_additional_add_to_cart_button( $product->get_id() ) ) return;

    $qty     = 12;
    $href    = '?add-to-cart=' . esc_attr( $product->get_id() ) . '&quantity='.$qty;
    $class   = 'single_add_to_cart_button-12 button alt';
    $style   = 'display: inline-block; margin-top: 12px;';
    $btn_txt = __( "Add a case of 12", "woocommerce" );

    ## ---------------------- For non variable products ----------------------- ##

    if( ! $product->is_type('variable') ) :

    // Output
    echo '<br><a href"'.$href.'"= rel="no-follow" class="'.$class.'" style="'.$style.'">'.$btn_txt.'</a>';

    ## ---------- For variable products (handling product variations) --------- ##

    else :

    // Output
    echo '<br><a rel="no-follow" class="'.$class.'" style="'.$style.'">'.$btn_txt.'</a>';

    $data = array();

    // Loop through available variations data
    foreach( $product->get_available_variations() as $variation_data ){
        if( $variation_data['is_in_stock'] && $variation_data['is_purchasable'] ) {
            $variation_id   = $variation_data['variation_id'];
            $attributes_str = '';

            // Loop through variation attributes
            foreach ( $variation_data['attributes'] as $attribute_taxonomy => $term_slug ) {
                $attributes_str .= '&'.$attribute_taxonomy.'='.$term_slug;
            }
            // Set product attributes pairs for variations
            $data[$variation_id] = $href . '&variation_id=' . $variation_id . $attributes_str;
        }
    }
    ?>
    <script type="text/javascript">
    jQuery(function($){
        var a = <?php echo json_encode($data); ?>,  b = '.single_add_to_cart_button-12',
            c = 'wc-variation-selection-needed',    d = 'disabled',
            f = 'form.variations_form',             h = $(c).attr('href'),
            s = '.variations select',               i = 'input.variation_id';

        // On show and hide variation event
        $(f).on('show_variation', function() {
            var found = false;

            $.each(a, function(j,k){
                if( $(i).val() == j ) {
                    found = true;
                    if( $(b).hasClass(c) && $(b).hasClass(d) )
                        $(b).removeClass(c).removeClass(d).attr('href',k);
                }
            });
            if( ! found && ! ( $(b).hasClass(c) && $(b).hasClass(d) ) )
                $(b).addClass(c).addClass(d).removeAttr("href");

        }).on('hide_variation', function() {
            if( ! ( $(b).hasClass(c) && $(b).hasClass(d) ) )
                $(b).addClass(c).addClass(d).removeAttr("href");
        });
    });
    </script>
    <?php
    endif;
}

代码进入事件子主题(或事件主题)的 function.php 文件。经过测试并有效。

关于php - 向 WooCommerce 中的特定产品添加额外的添加到购物车按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55649302/

相关文章:

PHP 5.6 编码 latin1 MySQL 编码

php - MySQL 从数组中搜索

javascript - 如何在 jQuery 中获取最后打开的 Bootstrap 模式对话框的 ID?

WordPress 插件,用于使用推荐 ID 进行用户注册

wordpress 子主题不工作

php - “添加图片”选项到图片库?

php - 如果当前日期和时间大于 Mysql 中存储的日期和时间,则回显一些文本

jquery - 使用 jQuery 滚动列表

javascript - 数据类型 'application/json' 与 'json'

javascript - 用户生成的过滤器和内容,我怎样才能写得更好