繁体   English   中英

WooCommerce 添加<custom>产品到购物车</custom>

[英]WooCommerce Add <custom> products to cart

我正在尝试制作一个片段,以在特定类别的所有产品单页上显示第二个“添加到购物车”按钮。 此按钮应将 6 个当前产品添加到购物车。

更新:使用来自@kmclaws 的信息和此处的附加添加到购物车按钮,在 woocommerce 单一产品页面中具有固定数量,我能够让它工作。 现在唯一的问题是,为了只显示在“biere”产品类别上的 IF 不起作用。

问题:有些产品仅来自“biere”或“cider”,有些产品来自“biere”和“cider”,还有一些其他类别。 意味着,有时不止一种,有时只定义一个类别。 如果“biere” OR/AND “cider” 是产品类别之一,我们希望始终显示此内容。

我怎样才能这样编码,如果“biere”或“cider”是显示所有内容的类别?

这是我当前的代码:

add_action( 'woocommerce_after_add_to_cart_button', 'additional_simple_add_to_cart', 20 );
function additional_simple_add_to_cart() {
    
    global $product;

    // Only for simple product type
    if( ! $product->is_type('simple') ) return;
                
        // Only for products from category "biere" AND/OR "Cider"
        if ( has_term( 'biere', 'cider', 'product_cat' ) ) {
    
            // Output Info text for "biere" or "cider" products
            echo '<p class="rtrn">Infotext block</p>';
                
            // Only display when more than 6 are available
            if( $product->get_stock_quantity() >= 6 ) {

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

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

谢谢

看起来您没有正确获取 PHP 中当前产品的 ID。 尝试这个:

add_action( 'woocommerce_after_add_to_cart_quantity', 'add_quantity_6' );

function add_quantity_6() {
    global $product;
    $id = $product->get_id();
    if ( is_product_category( 'jeans' ) ) {
        echo '<a href="https://example.com/?add-to-cart' . $id . '&quantity=6" class="button">Add 6 to Cart</a>';
    }    
}

您需要将 example.com 替换为 static 或您网站的相对路径

这是我的解决方案:

add_action( 'woocommerce_after_add_to_cart_button', 'additional_simple_add_to_cart', 20 );
function additional_simple_add_to_cart() {
    
    global $product;

    // Only for simple product type
    if( ! $product->is_type('simple') ) return;
                
        // Only for products from category "biere" AND/OR "Cider"
        if ( has_term( array('biere', 'cider'), 'product_cat', $product->get_id() ) ) {

    
            // Output Info text for "biere" or "cider" products
            echo '<p class="rtrn">Infotext block</p>';
                
            // Only display when more than 6 are available
            if( $product->get_stock_quantity() >= 6 ) {

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

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM