简体   繁体   English

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

[英]WooCommerce Add <custom> products to cart

I'm trying to crate a snipped to display a second "add to cart" button on all product single pages from a specific categorie.我正在尝试制作一个片段,以在特定类别的所有产品单页上显示第二个“添加到购物车”按钮。 This button should add 6 of the current product to the cart.此按钮应将 6 个当前产品添加到购物车。

UPDATE: With the info from @kmclaws and here additional add to cart button with fixed quantity in woocommerce single product pages I was able to get this to work.更新:使用来自@kmclaws 的信息和此处的附加添加到购物车按钮,在 woocommerce 单一产品页面中具有固定数量,我能够让它工作。 The only problem now is, that the IF in order to only display on "biere" product category does not work.现在唯一的问题是,为了只显示在“biere”产品类别上的 IF 不起作用。

Problem: Some Products are from "biere" or "cider" only and some are from "biere" AND "cider" and have some other categories.问题:有些产品仅来自“biere”或“cider”,有些产品来自“biere”和“cider”,还有一些其他类别。 Means, sometime there are more than one sometime only one category is defined.意味着,有时不止一种,有时只定义一个类别。 We want to display this always if "biere" OR/AND "cider" is one of the product category.如果“biere” OR/AND “cider” 是产品类别之一,我们希望始终显示此内容。

How can I code it that way, that if "biere" OR "cider" is the categorie to display that all?我怎样才能这样编码,如果“biere”或“cider”是显示所有内容的类别?

This is my current code:这是我当前的代码:

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

Thanks谢谢

It looks like you are not grabbing the ID of the current product in PHP correctly.看起来您没有正确获取 PHP 中当前产品的 ID。 Try this:尝试这个:

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

You need to replace example.com with either a static or relative path to your site您需要将 example.com 替换为 static 或您网站的相对路径

This is my solutions:这是我的解决方案:

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.

相关问题 在WooCommerce中添加两个不同的产品和自定义添加到购物车() - Add two different Products with Custom Add-To-Cart() in WooCommerce WooCommerce 将多个产品的自定义重定向添加到购物车 - WooCommerce Add to Cart custom redirection for multiple products Variations 在 Woocommerce 中的产品自定义循环上启用 Ajax 添加到购物车按钮 - Enable Ajax add to cart button on products custom loop in Woocommerce WooCommerce:如何以编程方式将一些产品添加到购物车? - WooCommerce: how to add a number of products to the cart programatically? WooCommerce AJAX 添加到购物车 - 仅限简单产品 - WooCommerce AJAX add to cart - simple products only Woocommerce添加到购物车产品仅限4种特定产品 - Woocommerce Add to cart product Restriction to 4 specific products 禁用特定 WooCommerce 产品的添加到购物车按钮 - Disabling Add to Cart Button for Specific WooCommerce Products 隐藏WooCommerce变量产品上的“添加到购物车”按钮 - Hiding 'Add to Cart' button on WooCommerce variable products 隐藏大多数产品的添加到购物车按钮-Woocommerce - Hide the add to cart button for most products - Woocommerce 向产品添加下拉菜单并在 Woocommerce 购物车上显示价值 - Add dropdown to products and display the value on Woocommerce cart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM