Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

php - WooCommerce checkout for one single product

Currently I am trying to create multiple check-out pages within my WordPress WooCommerce webshop. I created a shortcode to do the following:

  • Empty the cart
  • Add a product to the cart
  • Display the checkout form

The expected behaviour: The checkout form should display, with having the added product in the cart.

Found error: The 'Place order' button is redirecting to the front page. Most likely this is due to the fact that the page on which the shortcode is used is not set as the 'checkout page'. That probably results in WooCommerce not loading the correct files.

I already tried to use the WooCommerce One Page Checkout plug-in, and I tried to define WOOCOMMERCE_CHECKOUT to true (in multiple ways).

My current code is as follows:

function webob_single_checkout( $atts ){

add_filter('woocommerce_is_checkout', '__return_true');

$atts = shortcode_atts( array(
    'id' => 392
), $atts );

$pId = $atts['id'];

//Remove all products from cart
global $woocommerce;
$woocommerce->cart->empty_cart();

//Add product to cart
$woocommerce->cart->add_to_cart($pId);

return do_shortcode('[woocommerce_checkout]');

}
add_shortcode( 'webob_single_checkout', 'webob_single_checkout' );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...