Adding Extra Add to cart button below product summary in Woocommerce

纵饮孤独 提交于 2019-12-21 20:57:18

问题


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.

please suggest as what to do?


回答1:


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

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:



来源:https://stackoverflow.com/questions/47097572/adding-extra-add-to-cart-button-below-product-summary-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!