【问题标题】:Error with get_cart() method in woocommerce_order_item_meta_end hookwoocommerce_order_item_meta_end 挂钩中的 get_cart() 方法出错
【发布时间】:2017-03-01 11:20:36
【问题描述】:

我有这个代码,它的功能是在 woocommerce 订单详细信息电子邮件模板中添加一列,但是当我发送发票时,我收到以下错误消息:

致命错误:未捕获的错误:在 http:\mysite.com\functions.php 第 1245 行调用一个成员函数 get() on null

使用此代码时:

add_action( 'woocommerce_order_item_meta_end', 'order_custom_field_in_item_meta_end', 10, 4 );
function order_custom_field_in_item_meta_end( $item_id, $item, $order, $cart_item) {
    global $woocommerce;

    do_action( 'woocommerce_review_order_before_cart_contents' );

    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );

        if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_checkout_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
            echo '<td class="td">'.$_product->get_price_html().'</td>';
        }
    }

    do_action( 'woocommerce_review_order_after_cart_contents' );

}

我的问题是,当我从订单中发送发票或任何其他电子邮件通知时,会出现错误。

我做错了什么以及如何解决这个问题?

谢谢

【问题讨论】:

  • 请告诉我你在function.php中的第1245行
  • @DhruvinMoradiya 这是一行:foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
  • 你能告诉我你的要求是什么吗?
  • 实际上该错误仅在我通过订单发送电子邮件发票并将其指向该行时发生,我使用了 $product->get_cart() 并且它也不起作用
  • 我只需要在电子邮件模板上显示每个产品的价格,见上图

标签: php wordpress woocommerce orders email-notifications


【解决方案1】:

抱歉,您不能将 WC()-&gt;cart 对象用于订单或电子邮件,因为购物车已在结帐时处理并清空。相反,您可以使用函数在 woocommerce_order_item_meta_end 中挂钩时所具有的变量参数,即 $item_id$item$order$plain_text

您不需要任何 foreach 循环来获取 订单商品数据,因为您可以直接使用 $item 参数来获取您的产品 ID。

下面是适用于简单或可变产品的正确代码(但请参阅最后)

add_action( 'woocommerce_order_item_meta_end', 'order_custom_field_in_item_meta_end', 10, 4 );
function order_custom_field_in_item_meta_end( $item_id, $item, $order, $plain_text ) {

    do_action( 'woocommerce_review_order_before_cart_contents' );

    if( $item['variation_id'] > 0 ){
        $product = wc_get_product($item['variation_id']); // variable product
    } else {
        $product = wc_get_product($item['product_id']); // simple product 
    }

    // Be sure to have the corresponding "Cost each" column before using <td> tag
    echo '<td class="td">'.$product->get_price_html().'</td>';

    do_action( 'woocommerce_review_order_after_cart_contents' );

}

如果您之前没有创建(或定义)“成本”列,则不能使用 html &lt;td&gt; 标签

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

代码已经过测试并且可以工作。

【讨论】:

    【解决方案2】:

    问题是 woocommerce_order_item_meta_end 操作仅在下订单后才会触发。以便 WC()->cart 的范围不存在于您的代码 sn-p 中。

    您可以使用 $order->get_items() 来获取订单商品。

    请以这种方式修改您的代码以使其正常工作

    add_action( 'woocommerce_order_item_meta_end', 'order_custom_field_in_item_meta_end', 10, 4 );
    function order_custom_field_in_item_meta_end( $item_id, $item, $order) {
        do_action( 'woocommerce_review_order_before_cart_contents' );
    
        foreach ( $order->get_items() as $cart_item_key => $cart_item ) {
            // Do something here
        }
    
        do_action( 'woocommerce_review_order_after_cart_contents' );
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多