[英]WooCommerce add to cart redirect based on button clicked
I'd like to create two different add-to-cart buttons on the product page.我想在产品页面上创建两个不同的添加到购物车按钮。 Based on what button you click it has to redirect to the cart page or shop (archive) page.
根据您单击的按钮,它必须重定向到购物车页面或商店(存档)页面。
On the product page I created some extra custom fields (item data) the user have to fill in, before adding the product to their shopping bag.在产品页面上,我创建了一些额外的自定义字段(项目数据),用户必须在将产品添加到他们的购物袋之前填写这些字段。 There are multiple explanations for redirecting your customers to the page you want with the
woocommerce_add_to_cart_redirect
hook, but I'd like to customize it a bit based on the button you click.使用
woocommerce_add_to_cart_redirect
钩子将您的客户重定向到您想要的页面有多种解释,但我想根据您单击的按钮对其进行一些自定义。
I've added a second submit button to the add to cart form on the product page.我在产品页面的添加到购物车表单中添加了第二个提交按钮。 These buttons are inside the form, else the custom fields won't validate and products won't add to the cart.
这些按钮位于表单内,否则自定义字段将无法验证,产品也不会添加到购物车。 There are multiple explanations and hooks online to create extra add to cart buttons to your product page, but these won't work because of the validation that needed to be done.
有多种在线解释和挂钩可以为您的产品页面创建额外的添加到购物车按钮,但由于需要完成验证,这些都不起作用。
<button type="submit" name="add-to-cart" value="overview <?php echo esc_attr( $product->get_id() ); ?>" class="c-link"><?php _e('Save and go to shop', 'stackoverflowcom'); ?></button>
<button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" class="c-btn --gold single_add_to_cart_button button alt"><?php _e('Go to shopping bag', 'stackoverflowcom'); ?></button>
Second step is I created a function for the woocommerce_add_to_cart_redirect
hook, to check for the button that is clicked.第二步是我为
woocommerce_add_to_cart_redirect
挂钩创建了一个 function,以检查单击的按钮。
function custom_add_to_cart_redirect( $url ) {
if ($_POST['add-to-cart'] == 'overview') {
$url = get_permalink( 17 );
return $url;
} else {
$url = get_permalink( wc_get_page_id( 'cart' ));
return $url;
}} add_filter( 'woocommerce_add_to_cart_redirect', 'custom_add_to_cart_redirect' );
Big issue however, you can't have multiple values (or names) inside your button.但是,大问题是,您的按钮中不能有多个值(或名称)。 So the above function won't work.
所以上面的function是行不通的。 Does anyone have other solutions?
有人有其他解决方案吗? Does anyone know how I can create a secondary add to cart button with a different redirect?
有谁知道如何使用不同的重定向创建辅助添加到购物车按钮?
Thanks in advance!提前致谢!
My solution based on what CBroe says, I created a javascript function:我的解决方案基于 CBroe 所说的内容,我创建了一个 javascript function:
<script>
function overviewClick() {
document.getElementById('overview').value="overview"
document.getElementById('cart').submit();
}
</script>
Created a hidden input field with the name 'overview'.创建了一个名为“概览”的隐藏输入字段。 Then I created a PHP function that checks if the input field is empty or not:
然后我创建了一个 PHP function 检查输入字段是否为空:
function custom_add_to_cart_redirect( $url ) {
if( ! empty( $_POST['overview'] ) ) {
$url = get_permalink( 17 );
return $url;
} else {
$url = get_permalink( wc_get_page_id( 'cart' ));
return $url;
}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.