发布带有数据的woocommerce add_to_cart

时间:2013-12-13 15:18:53

标签: php wordpress woocommerce

当我尝试将数据设置到我的项目中时出现问题:

add_filter('woocommerce_add_cart_item_data', '_add_custom_item_data', 10, 2);
add_filter('woocommerce_get_cart_item_from_session', '_get_cart_item_from_session', 10, 2);

function _add_custom_item_data($cart_item_data, $product_id){
    if(!empty($cart_item_data)){
        $cart_item_data['custom_field'] = 'aaaa';
    }
    return $cart_item_data;
}
function _get_cart_item_from_session($cart_item, $values){
    if (isset($values['custom_field'])) {
            $cart_item['custom_field'] = $values['custom_field'];
        }
    return $cart_item;
}

当我使用时:

$woocommerce->cart->add_to_cart($id,'1','','',array('_my_data','000000000000000000000000000000'));

_add_custom_item_data函数也被执行,我有_my_data变量。

但是我试着用我的产品获取变量:

$items = $woocommerce->cart->get_cart();
foreach($items as $item){
    echo $woocommerce->cart->get_item_data( $item ) . "<br>";
}

我没看到我的变量......

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

最后一个功能尝试以下来获取数据

<?php 
                $cart = $woocommerce->cart->get_cart();
                $items = 0;
                $total = 0;
                foreach ($cart as $p) {
                    $total += $p['line_total'];
                    $items += $p['quantity'];
                }
                echo $items;
                echo $total;
            ?>

答案 1 :(得分:0)

而不是直接将数据分配给索引,您应首先将其分配给require 'uri' require 'net/http' module PvWatch class Client def login url = URI("http://pvwatch.xxx.xxx.xxx.net/api/v1/login") http = Net::HTTP.new(url.host, url.port) request = Net::HTTP::Post.new(url) request["content-type"] = 'application/x-www-form-urlencoded' request["access-key"] = 'FORGET ABOUT THIS' request["authorization"] = 'uri=api/v1/login' request.body = "username=case&password=123" response = http.request(request) puts response.read_body end end end ,例如,您可以尝试此操作

product_meta