在Woocommerce中产品摘要下方的Adding Extra Add to cart按钮 [英] Adding Extra Add to cart button below product summary in Woocommerce

查看:112
本文介绍了在Woocommerce中产品摘要下方的Adding Extra Add to cart按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WooCommerce中,我试图在产品摘要下方添加一个额外的添加到购物车按钮.我成功地在此代码之后添加了一个额外按钮,该按钮适用于单个产品:

In WooCommerce, I am trying to add an extra add to cart button below product summary. I successfully added an extra button following this code which works for single products:

add_action( 'woocommerce_single_product_summary', 'custom_button_after_product_summary', 30 );

function custom_button_after_product_summary() {
  global $product;
  echo "<a href='".$product->add_to_cart_url()."'>add to cart</a>";
}

但是,如果产品是变体,则无法使用.

But if the product is a variation it doesn't work.

请建议做什么?

推荐答案

我稍微回顾了一下您的代码,并为变量产品添加了第二个挂钩函数:

I have revisited your code a bit, and added a 2nd hooked function for variable products:

// For Simple products
add_action( 'woocommerce_single_product_summary', 'second_button_after_product_summary', 30 );
function second_button_after_product_summary() {
    global $product;

    if( ! $product->is_type( 'variable' ) )
        echo '<button type="submit" name="add-to-cart" value="'. esc_attr( $product->get_id() ).'" class="single_add_to_cart_button button alt">'. esc_html( $product->single_add_to_cart_text() ).'</button>';
}

// For Variable products
add_action( 'woocommerce_single_variation', 'second_button_single_variation', 30 );
function second_button_single_variation() {
    global $product;

    echo '<br>
        <button type="submit" class="single_add_to_cart_button button alt">'. esc_html( $product->single_add_to_cart_text() ).'</button>';
}

代码会出现在您活动的子主题(或主题)的function.php文件中,或者出现在任何插件文件中.

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

您将在可变产品上得到它:

You will get this on variable products:

这篇关于在Woocommerce中产品摘要下方的Adding Extra Add to cart按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆