Php WooCommerce Add<;定制>;要购物车的产品

Php WooCommerce Add<;定制>;要购物车的产品,php,wordpress,woocommerce,cart,Php,Wordpress,Woocommerce,Cart,我试图在一个特定类别的所有产品单页上显示第二个“添加到购物车”按钮。此按钮应将当前产品的6%添加到购物车中 更新: 有了@kmclaws提供的信息,我就可以让它正常工作了。现在唯一的问题是,如果为了只显示在“biere”产品类别上,IF不起作用 问题: 有些产品仅来自“比尔”或“苹果酒”,有些来自“比尔”和“苹果酒”,还有一些其他类别。也就是说,有时有多个类别,有时只定义一个类别。如果“biere”或/和“苹果酒”是产品类别之一,我们希望始终显示这一点 如果“比尔”或“苹果酒”是显示这一切的类别

我试图在一个特定类别的所有产品单页上显示第二个“添加到购物车”按钮。此按钮应将当前产品的6%添加到购物车中

更新: 有了@kmclaws提供的信息,我就可以让它正常工作了。现在唯一的问题是,如果为了只显示在“biere”产品类别上,IF不起作用

问题: 有些产品仅来自“比尔”或“苹果酒”,有些来自“比尔”和“苹果酒”,还有一些其他类别。也就是说,有时有多个类别,有时只定义一个类别。如果“biere”或/和“苹果酒”是产品类别之一,我们希望始终显示这一点

如果“比尔”或“苹果酒”是显示这一切的类别,我怎么能这样编码呢

这是我当前的代码:

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>';
            }
        }
}
add_动作('woocommerce_后加_至_购物车按钮','additional_simple_至_购物车',20);
功能附加\u简单\u添加到\u购物车(){
全球$产品;
//仅适用于简单的产品类型
如果(!$product->is_type('simple')返回;
//仅适用于“比尔”和/或“苹果酒”类别的产品
如果(有术语('biere','cider','product'u cat')){
//“比尔”或“苹果酒”产品的输出信息文本
echo'

信息文本块; //仅当超过6个可用时显示 如果($product->get_stock_QUOTE()>=6){ $href='?添加到购物车='。esc_attr($product->get_id())。&quantity=6'; $class='ingle_add_to_cart_button-12 button alt'; $style='显示:内联块;页边距顶部:12px;'; $button_text=uuuuuuuuuuuuuuuuuuuu6件装的贝斯泰伦(bestellen)和woocommerce; //输出添加到购物车按钮 回声“
”; } } }


谢谢

看起来您没有正确获取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>';
    }    
}
add_动作('woocommerce_后加_至_cart_数量','add_数量_6');
函数add_quantity_6(){
全球$产品;
$id=$product->get_id();
如果(是产品类别(“牛仔裤”)){
回声';
}    
}
您需要将example.com替换为指向站点的静态或相对路径

这是我的解决方案:

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>';
            }
        }
}
add_动作('woocommerce_后加_至_购物车按钮','additional_simple_至_购物车',20);
功能附加\u简单\u添加到\u购物车(){
全球$产品;
//仅适用于简单的产品类型
如果(!$product->is_type('simple')返回;
//仅适用于“比尔”和/或“苹果酒”类别的产品
if(有_项(数组('biere','cider'),'product_cat',$product->get_id()){
//“比尔”或“苹果酒”产品的输出信息文本
echo'

信息文本块; //仅当超过6个可用时显示 如果($product->get_stock_QUOTE()>=6){ $href='?添加到购物车='。esc_attr($product->get_id())。&quantity=6'; $class='ingle_add_to_cart_button-12 button alt'; $style='显示:内联块;页边距顶部:12px;'; $button_text=uuuuuuuuuuuuuuuuuuuu6件装的贝斯泰伦(bestellen)和woocommerce; //输出添加到购物车按钮 回声“
”; } } }


您尝试过这个吗?对我从那里得到了所有的信息。我很难理解密码。在我给出答案后,你改变了原来的问题。对于您的第二个问题:“如果“biere”或/和“cider”是产品类别之一,我们希望始终显示此项”-您需要更改我的回答的第6行->
如果(有_项(数组('biere','cider'),'product_cat',$id){
以后,不要大幅编辑您的初始问题。问一个新问题。