簡體   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