简体   繁体   English

WooCommerce 添加到购物车重定向基于点击的按钮

[英]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.

相关问题 Woocommerce添加到购物车按钮重定向到结帐 - Woocommerce add to cart button redirect to checkout Woocommerce:检测“添加到购物车”按钮的点击位置并运行不同的代码 - Woocommerce: Detect where Add to Cart button was clicked and run different code 将添加到购物车按钮重定向到 WooCommerce 中 1 个特定登录页面的结帐页面 - Redirect add to cart button to checkout page for 1 specific landing page in WooCommerce 更改商店页面中 woocommerce 商店中“添加到购物车”按钮的重定向 - Changing the redirect of the “add to cart” button in woocommerce store in Shop page Woocommerce 产品页面重定向中断添加到购物车按钮 - Woocommerce product page redirect breaks add to cart button 基于购物车的 WooCommerce 登录重定向 - WooCommerce login redirect based on cart 在结帐时添加一个按钮以清空购物车并重定向到Woocommerce中的商店页面 - Add a button in checkout to empty cart and redirect to shop page in Woocommerce 其他Woocommerce产品“添加到购物车”按钮,可重定向到结帐 - Additional Woocommerce product Add To Cart button that redirect to checkout 基于产品类型的 WooCommerce 的“添加到购物车”按钮旁边的自定义按钮 - Custom Button next to “ADD TO CART” button of WooCommerce based on Product Type 单击“添加到购物车”按钮时将用户重定向到其他网站 - Redirect user to other website on when clicked on Add To Cart button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM