【问题标题】:WooCommerce 2.1 Detect Chosen Shipping MethodWooCommerce 2.1 检测选择的运输方式
【发布时间】:2014-03-13 15:38:25
【问题描述】:

我一直在使用以下内容来决定是否需要完成结帐字段...

if ($posted['shipping_method'] == "local_pickup_plus") {
}

自从更新到 WooCommerce 2.1 后,我的代码不再有效。

我尝试回显存储在 $posted['shipping_method'] 中的值,以查看我是否根据正确的值检查它,但似乎此变量中不再存储任何内容。

我一直在寻找检查所选运输方式的其他方法,但我没有走得太远。

任何帮助将不胜感激。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    找了几个小时,然后决定深入研究 WooCommerce 文件...

    这似乎对我有用:

     $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
     $chosen_shipping = $chosen_methods[0]; 
    

    我正在使用它在我的functions.php中使用它来设置本地交付的最小总数

    add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
    function woocommerce_custom_surcharge() {
    global $woocommerce;
    
    
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    return;
    
     $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
     $chosen_shipping = $chosen_methods[0]; 
    
     $min_spend = 25;
     $cart_total = $woocommerce->cart->cart_contents_total;
     if (($cart_total < 25) AND ($chosen_shipping == 'local_delivery')) {   
        $surcharge = $min_spend-$cart_total;    
        $woocommerce->cart->add_fee( 'Delivery Surchage', $surcharge, true, 'standard' );
    }
    
    }
    

    希望这对某人有所帮助。

    【讨论】:

    • 仍在工作。谢谢!
    • 这有点用,但有几件事:当您可以从 WC() 获得相同的信息时,您不习惯使用 global $woocommerce,请参见此处:businessbloomer.com/… 此外,返回的值由 " selected_shipping_methods" 不完全匹配,但它确实包含运输方式名称,因此您必须使用 strpos 而不是 ===
    【解决方案2】:

    来自 WooCommerce 2.1 的source code

    $this-&gt;posted['shipping_method'] = isset( $_POST['shipping_method'] ) ? $_POST['shipping_method'] : '';

    所以如果变量$_POST['shipping_method'] 没有设置,那么$posted['shipping_method'] 将是空字符串。我的猜测是您的form 没有将method 属性设置为post(它可能被省略,或者您尝试使用链接进行产品提交,而不是form)。

    【讨论】:

    • 感谢您的回复。表单方法属性设置为 post。有问题的表单是 WooCommerce Checkout。
    • 我一直在尝试 '$test = $posted['shipping_method']; $woocommerce->add_error( __('选择的运输方式是' . $test) );'并且错误消息区域中仅打印了“选择的运输方式”。
    • 我现在开始认为这是添加代码的优先级... 'add_action('woocommerce_after_checkout_validation', 'validate_collection_date', 20, 1);'所以需要进一步研究。
    • 如果您在 WooCommerce 中发现错误,最好将其发布在问题跟踪器上,以便开发人员能够修复它。
    • 会做...感谢您的帮助。
    【解决方案3】:

    $_POST['shipping_method'] 是一个数组,$whatever = $_POST['shipping_method'], $whatever[0] = value

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-29
      • 2018-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-08
      • 1970-01-01
      • 2018-12-04
      相关资源
      最近更新 更多