在WooCommerce中为特定产品添加额外添加到购物车按钮

问题描述 投票:3回答:3

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

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

这可能吗?我需要改变什么......

php jquery wordpress woocommerce product
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文件中。经过测试和工作。


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>';

0
投票

谢谢你的所有澄清。我将只有单一的产品类型,但我会有一些产品,我需要添加到6瓶的购物车和其他一些,12瓶和一些。我想如果我可以在产品页面中有一个选项,例如像自定义字段,例如wine-box,我可以填写6或12,这可以显示为额外添加到购物车按钮(添加6个案例或添加12个案例的案例)。谢谢

© www.soinside.com 2019 - 2024. All rights reserved.