【问题标题】:updating woocommerce shipping values at runtime在运行时更新 woocommerce 运费值
【发布时间】:2014-05-29 14:26:16
【问题描述】:

我对 php 和 woocommerce 还很陌生。我正在开发一个使用 wordpress 和 woocommerce 的电子商务网站。该公司使用 API 根据购物车中物品的面积和尺寸以及物品数量来计算运费。我已经设法将所有需要的值解析到 API,并且确实得到了响应。我为提供的 3 种测试费率创建了一个自定义运输插件。 在我的脚本中,我创建了会话,其中包含每个运输方法的总数,如下所示:

    $_SESSION['overnight']= $quoteResponse->results[0]->rates[2]->total;

然后在我的自定义运输插件中,我启动会话并将成本设置为等于会话内的值:

    <?php
    session_start();

    * Check if WooCommerce is active
    */
    if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins',     get_option( 'active_plugins' ) ) ) ) {

    function WC_Overnight_Express_init() {
    if ( ! class_exists( 'WC_Overnight_Express' ) ) {
    class WC_Overnight_Express extends WC_Shipping_Method {
    /**
    * Constructor for your shipping class
    *
    * @access public
    * @return void
    */
   public function __construct() {
   $this->id = 'overnight express'; // Id for your shipping method. Should be uunique.
   $this->method_title = __( 'Overnight Express' ); // Title shown in admin
   $this->method_description = __( 'Overnight Express Shipping method' ); 

   $this->enabled = "yes"; // This can be added as an setting but for this example its forced enabled
   $this->title = "Overnight Express"; // This can be added as an setting but for this example its forced.

   $this->init();
   }

   /**
   * Init your settings
   *
   * @access public
   * @return void
   */
   function init() {
   // Load the settings API
   $this->init_form_fields(); // This is part of the settings API. Override the method to add your own settings
   $this->init_settings(); // This is part of the settings API. Loads settings you previously init.


   // Save settings in admin if you have any defined
   add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this,      'process_admin_options' ) );
   }

   /**
   * calculate_shipping function.
   *
   * @access public
   * @param mixed $package
   * @return void
   */
   public function calculate_shipping() {
   $rate = array(
   'id' => $this->id,
   'label' => $this->title,
   'cost' =>$_SESSION['overnight'],
   'calc_tax' => 'per_item'
   );

   // Register the rate
   $this->add_rate( $rate );
   }
   } 
   }
   }

   add_action( 'woocommerce_shipping_init', 'WC_Overnight_Express_init' );

   function add_overnight_method( $methods ) {
   $methods[] = 'WC_Overnight_Express'; 
   return $methods;
   }

   add_filter( 'woocommerce_shipping_methods', 'add_overnight_method' );
   }

   ?>

我的问题是,在从 API 返回报价后,他们不会更新显示“免费”的运输总额,并且只有在我打开购物车并返回结帐页面时才会更新。我想做的是在从 API 返回总数时在运行时更新它们。这是我在 stackoverlow 上的第一篇文章,任何帮助将不胜感激。谢谢

【问题讨论】:

    标签: php jquery ajax wordpress woocommerce


    【解决方案1】:

    首先,我认为您应该在 WC_Overnight_Express_init 函数中实例化该类,因为按原样,构造方法无法运行。

    然后,woocommerce 系统工具中有一个选项可以启用对运输方式的调试,在开发过程中激活它确实是一个好主意(抱歉,屏幕截图是法语,但你明白了,对吗?) :

    【讨论】:

    • 感谢您的回复已经为此奋斗了很长一段时间。虽然启用了运输调试模式但仍然无法正常工作,但看起来它没有改变任何东西
    【解决方案2】:

    编辑

    尝试将 add_action 和 add_filter 代码放在 if 子句之外。我知道这听起来很疯狂,但通常会有所帮助。

    经过一番思考,我很确定问题在于将数据存储在 $_SESSION 中。请听从我的建议,并在 calculate_shipping 方法中进行所有计算。当然,您可以将任何静态值放在那里以进行测试。或制作var_dump($_SESSION) 在 calculate_shipping 中确保一切都符合预期(在 ajax 上我经常使用 file_put_contents 或 flip/whoops)。

    运费计算

    接下来在 calculate_shipping 方法中,您可以获取购物车内容并施展魔法:

    function calculate_shipping() {
        $cart = WC_Cart::get_cart() 
        foreach($cart as $item){
            //magic
        }
    }
    

    旧答案

    正确的钩子

    您应该将您的运输逻辑挂钩到:

    add_action( 'woocommerce_shipping_init', 'tutsplus_shipping_method' );
    
    function add_tutsplus_shipping_method( $methods ) {
        $methods[] = 'WC_Overnight_Express';
        return $methods;
    }
    
    add_filter( 'woocommerce_shipping_methods', 'add_tutsplus_shipping_method' );
    

    运费计算

    接下来在 calculate_shipping 方法中,您可以获取购物车内容并施展魔法:

    function calculate_shipping(){
        $cart = WC_Cart::get_cart() 
        foreach($cart as $item){
            //magic
        }
    }
    

    这可能有助于实时更新购物车审查:

    https://stackoverflow.com/a/47133456/8888443

    更多帮助

    看看这个教程: https://code.tutsplus.com/tutorials/create-a-custom-shipping-method-for-woocommerce--cms-26098

    如果您不熟悉 wordpress 开发,我强烈建议您熟悉操作/挂钩。 woocommerce 和 wordpress 都有很好的参考。

    https://codex.wordpress.org/Plugin_API/Action_Reference

    https://docs.woocommerce.com/wc-apidocs/hook-docs.html

    【讨论】:

      猜你喜欢
      • 2015-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 2014-02-12
      相关资源
      最近更新 更多