在WooCommerce中为特定产品添加额外的“加入购物车”按钮

3
3个回答

1

处理产品变体(对于可变产品)需要添加一些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文件中。已测试并可用。

我面临着一个类似的困境,我只需要添加10个产品,并使用与通常的WooCommerce按钮不同的按钮,但它只添加了一个产品。是否可能在不使用JavaScript的情况下实现这个功能?我的查询显示了这个讨论。 - undefined
谢谢。起初你在代码中犯了一个错误,(!),所以我试图找出代码中是否还有其他错误,并告诉你。但是,我理解一段php代码的速度无法与那些只需看一眼就知道其含义的人相比。正如我在两个评论中所说,我进行了一些测试,当然我会接受你的问题,我甚至可以对得到的答案提供奖励。但是你很快就删除了答案,这是你的权利。如果这一切曾经冒犯或打扰到你,我只是感到抱歉。 - undefined
答案就在你的问题中...请查看:https://stackoverflow.com/questions/77228702/how-to-add-10-units-of-a-product-with-a-new-button-in-the-woocommerce-catalog/77229052#77229052 - undefined

0

感谢您提供的所有澄清信息。 我只会有单一产品类型,但是我会有一些产品需要添加到购物车中的6瓶,另外一些产品需要添加到购物车中的12瓶。 我在想是否可以在产品页面上添加一个选项,例如自定义字段,例如wine-box,我可以填写6或12,并显示为额外的添加到购物车按钮(添加6箱文本或添加12箱文本)。 谢谢



0

您可以使用 WC_prroduct->get_id() 函数来检查产品 ID

// 仅适用于 ID 为16的产品 if( $product->get_id() != 16 ) return;

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接