Php 将多个项目添加到购物车后重定向
我已经成功地将这段代码添加到了我的functions.php中,它非常有效 以下是正在运行的it的URL: 我非常希望在加载此内容时重定向到购物车内容,这样客户就不会看到绿色的“you have successfully added xyz to your cart”(您已成功将xyz添加到购物车)墙 这可能吗?我该怎么做 以下是相关代码:Php 将多个项目添加到购物车后重定向,php,woocommerce,Php,Woocommerce,我已经成功地将这段代码添加到了我的functions.php中,它非常有效 以下是正在运行的it的URL: 我非常希望在加载此内容时重定向到购物车内容,这样客户就不会看到绿色的“you have successfully added xyz to your cart”(您已成功将xyz添加到购物车)墙 这可能吗?我该怎么做 以下是相关代码: // adds support for multi add to cart for 3rd party cart plugin function w
// adds support for multi add to cart for 3rd party cart plugin
function woocommerce_maybe_add_multiple_products_to_cart() {
// Make sure WC is installed, and add-to-cart qauery arg exists, and contains at least one comma.
if ( ! class_exists( 'WC_Form_Handler' ) || empty( $_REQUEST['add-to-cart'] ) || false === strpos( $_REQUEST['add-to-cart'], ',' ) ) {
return;
}
// Remove WooCommerce's hook, as it's useless (doesn't handle multiple products).
remove_action( 'wp_loaded', array( 'WC_Form_Handler', 'add_to_cart_action' ), 20 );
$product_ids = explode( ',', $_REQUEST['add-to-cart'] );
$count = count( $product_ids );
$number = 0;
foreach ( $product_ids as $product_id ) {
if ( ++$number === $count ) {
// Ok, final item, let's send it back to woocommerce's add_to_cart_action method for handling.
$_REQUEST['add-to-cart'] = $product_id;
return WC_Form_Handler::add_to_cart_action();
}
$product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $product_id ) );
$was_added_to_cart = false;
$adding_to_cart = wc_get_product( $product_id );
if ( ! $adding_to_cart ) {
continue;
}
// only works for simple atm
if ( $adding_to_cart->is_type( 'simple' ) ) {
// quantity applies to all products atm
$quantity = empty( $_REQUEST['quantity'] ) ? 1 : wc_stock_amount( $_REQUEST['quantity'] );
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
if ( $passed_validation && false !== WC()->cart->add_to_cart( $product_id, $quantity ) ) {
wc_add_to_cart_message( array( $product_id => $quantity ), true );
}
}
}
}
add_action( 'wp_loaded', 'woocommerce_maybe_add_multiple_products_to_cart', 15 );