首页 > 解决方案 > 编辑可变产品的“添加到购物车”按钮

问题描述

我的网站中有此代码的三个版本,用于生产 3 个按钮(1x 数量、3x 数量和 10x 数量)。我的产品是可变的。直到我删除了这一行代码才起作用if( ! $product->is_type('simple') ) return;。删除此行后,是否应将其替换为可变产品的其他内容以确保代码正确完整?

add_action( 'woocommerce_after_add_to_cart_button', 'additional_simple_add_to_cart_10', 20 );

function additional_simple_add_to_cart_10() {
    global $product;

// Only for simple product type
    if( ! $product->is_type('simple') ) return;  // Removed this line 


$href = '?add-to-cart=' . esc_attr( $product->get_id() ) . '&quantity=10';
$class = 'ingle_add_to_cart_button-10 button alt';
$style = 'display: inline-block; margin-top: 12px;';
$button_text = __( "10 Tickets", "woocommerce" );

// Output
echo '<br><a rel="no-follow" href="'.$href.'" class="'.$class.'" style="'.$style.'">'.$button_text.'</a>';

}

// Redirect to checkout after add to basket 

add_filter( 'woocommerce_add_to_cart_redirect', 'misha_skip_cart_redirect_checkout' );

function misha_skip_cart_redirect_checkout( $url ) {
    return wc_get_checkout_url();
}

标签: wordpresswoocommerce

解决方案


推荐阅读