【问题标题】:Woocommerce Hook for Custom Behaviour for Place Order用于下订单的自定义行为的 Woocommerce 钩子
【发布时间】:2016-09-07 19:13:55
【问题描述】:

我想知道是否有一个钩子可以在单击时更改“下单按钮”的行为。我正在尝试在下订单时更换/更改产品,因为产品选择(所有可用产品)也在结帐页面中。

到目前为止,我一直在尝试操纵woocommerce_checkout_order_review hook & 失败。

add_action('woocommerce_checkout_order_review', 'remove_woocommerce_product');
function remove_woocommerce_product(){
    if (isset($_POST['woocommerce_checkout_place_order'])){

        global $woocommerce;
        $woocommerce->cart->empty_cart(); // Empty the cart

        $selectedproduct = $_POST['selectedproductid']; // Get the selected product

        WC()->cart->add_to_cart( $selectedproduct ); // Insert the selected product in the the cart
        return esc_url( wc_get_checkout_url() ); // Redirect to Payment Gateway Page
    }
}

下订单时不会触发上述挂钩。也许我的代码有问题,或者我怀疑应用了错误的钩子。有什么想法吗?

【问题讨论】:

    标签: wordpress woocommerce hook checkout orders


    【解决方案1】:

    没关系...找到答案...

    add_action('woocommerce_checkout_process', 'change_product_upon_submission');
    function change_product_upon_submission() {
         if ( !empty( $_POST['_wpnonce'] ) && !empty($_POST['selectedproductid']) ) {
         $selectedproduct = $_POST['selectedproductid']; // Get the selected product
         WC()->cart->empty_cart(); //Empty the cart
         WC()->cart->add_to_cart( $selectedproduct ); // Insert the selected product in the cart
         }
    }
    

    更多解释见Woocommerce Replace Product in Cart Upon Place Order in Checkout Page

    【讨论】:

      猜你喜欢
      • 2016-05-07
      • 1970-01-01
      • 1970-01-01
      • 2016-03-03
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 2021-09-13
      • 1970-01-01
      相关资源
      最近更新 更多