arrays - 在 WooCommerce 3 中序列化购物车内容而不丢失数据

标签 arrays wordpress multidimensional-array woocommerce

我有一位客户想要将其各个 WooCommerce 商店中的所有购物车重新路由到一辆购物车。例如,商店 A、B、C、D 的购物车将被传递到商店 F 的支付网关。

我想出了一种方法来做到这一点,从任何商店获取购物车的内容并将其传递给在 WooCommerce 中下订单的函数,但我在这里遇到了问题,因为如果我 var_dump WC()->cart->get_cart() 它返回这个数组:

array(1) {
  ["98f13708210194c475687be6106a3b84"]=>
  array(10) {
    ["product_id"]=>
    int(20)
    ["variation_id"]=>
    int(0)
    ["variation"]=>
    array(0) {
    }
    ["quantity"]=>
    int(2)
    ["line_total"]=>
    float(3001.98)
    ["line_subtotal"]=>
    float(3001.98)
    ["line_tax"]=>
    float(0)
    ["line_subtotal_tax"]=>
    float(0)
    ["line_tax_data"]=>
    array(2) {
      ["total"]=>
      array(0) {
      }
      ["subtotal"]=>
      array(0) {
      }
    }
    ["data"]=>
    object(WC_Product_Simple)#8809 (12) {
      ["object_type":protected]=>
      string(7) "product"
      ["post_type":protected]=>
      string(7) "product"
      ["cache_group":protected]=>
      string(8) "products"
      ["data":protected]=>
      array(48) {
        ["name"]=>
        string(7) "Jet sky"
        ["slug"]=>
        string(7) "jet-sky"
        ["date_created"]=>
        object(WC_DateTime)#8801 (4) {
          ["utc_offset":protected]=>
          int(0)
          ["date"]=>
          string(26) "2017-06-23 15:25:27.000000"
          ["timezone_type"]=>
          int(1)
          ["timezone"]=>
          string(6) "+00:00"
        }
        ["date_modified"]=>
        object(WC_DateTime)#8804 (4) {
          ["utc_offset":protected]=>
          int(0)
          ["date"]=>
          string(26) "2017-06-23 15:25:27.000000"
          ["timezone_type"]=>
          int(1)
          ["timezone"]=>
          string(6) "+00:00"
        }
        ["status"]=>
        string(7) "publish"
        ["featured"]=>
        bool(false)
        ["catalog_visibility"]=>
        string(7) "visible"
        ["description"]=>
        string(278) "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo."
        ["short_description"]=>
        string(93) "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas."
        ["sku"]=>
        string(0) ""
        ["price"]=>
        string(7) "1500.99"
        ["regular_price"]=>
        string(7) "1500.99"
        ["sale_price"]=>
        string(0) ""
        ["date_on_sale_from"]=>
        NULL
        ["date_on_sale_to"]=>
        NULL
        ["total_sales"]=>
        int(0)
        ["tax_status"]=>
        string(7) "taxable"
        ["tax_class"]=>
        string(0) ""
        ["manage_stock"]=>
        bool(false)
        ["stock_quantity"]=>
        NULL
        ["stock_status"]=>
        string(7) "instock"
        ["backorders"]=>
        string(2) "no"
        ["sold_individually"]=>
        bool(false)
        ["weight"]=>
        string(0) ""
        ["length"]=>
        string(0) ""
        ["width"]=>
        string(0) ""
        ["height"]=>
        string(0) ""
        ["upsell_ids"]=>
        array(0) {
        }
        ["cross_sell_ids"]=>
        array(0) {
        }
        ["parent_id"]=>
        int(0)
        ["reviews_allowed"]=>
        bool(true)
        ["purchase_note"]=>
        string(0) ""
        ["attributes"]=>
        array(0) {
        }
        ["default_attributes"]=>
        array(0) {
        }
        ["menu_order"]=>
        int(0)
        ["virtual"]=>
        bool(false)
        ["downloadable"]=>
        bool(false)
        ["category_ids"]=>
        array(0) {
        }
        ["tag_ids"]=>
        array(0) {
        }
        ["shipping_class_id"]=>
        int(0)
        ["downloads"]=>
        array(0) {
        }
        ["image_id"]=>
        string(2) "18"
        ["gallery_image_ids"]=>
        array(1) {
          [0]=>
          int(19)
        }
        ["download_limit"]=>
        int(-1)
        ["download_expiry"]=>
        int(-1)
        ["rating_counts"]=>
        array(0) {
        }
        ["average_rating"]=>
        string(1) "0"
        ["review_count"]=>
        int(0)
      }
      ["supports":protected]=>
      array(1) {
        [0]=>
        string(16) "ajax_add_to_cart"
      }
      ["id":protected]=>
      int(20)
      ["changes":protected]=>
      array(0) {
      }
      ["object_read":protected]=>
      bool(true)
      ["extra_data":protected]=>
      array(0) {
      }
      ["default_data":protected]=>
      array(48) {
        ["name"]=>
        string(0) ""
        ["slug"]=>
        string(0) ""
        ["date_created"]=>
        NULL
        ["date_modified"]=>
        NULL
        ["status"]=>
        bool(false)
        ["featured"]=>
        bool(false)
        ["catalog_visibility"]=>
        string(7) "visible"
        ["description"]=>
        string(0) ""
        ["short_description"]=>
        string(0) ""
        ["sku"]=>
        string(0) ""
        ["price"]=>
        string(0) ""
        ["regular_price"]=>
        string(0) ""
        ["sale_price"]=>
        string(0) ""
        ["date_on_sale_from"]=>
        NULL
        ["date_on_sale_to"]=>
        NULL
        ["total_sales"]=>
        string(1) "0"
        ["tax_status"]=>
        string(7) "taxable"
        ["tax_class"]=>
        string(0) ""
        ["manage_stock"]=>
        bool(false)
        ["stock_quantity"]=>
        NULL
        ["stock_status"]=>
        string(7) "instock"
        ["backorders"]=>
        string(2) "no"
        ["sold_individually"]=>
        bool(false)
        ["weight"]=>
        string(0) ""
        ["length"]=>
        string(0) ""
        ["width"]=>
        string(0) ""
        ["height"]=>
        string(0) ""
        ["upsell_ids"]=>
        array(0) {
        }
        ["cross_sell_ids"]=>
        array(0) {
        }
        ["parent_id"]=>
        int(0)
        ["reviews_allowed"]=>
        bool(true)
        ["purchase_note"]=>
        string(0) ""
        ["attributes"]=>
        array(0) {
        }
        ["default_attributes"]=>
        array(0) {
        }
        ["menu_order"]=>
        int(0)
        ["virtual"]=>
        bool(false)
        ["downloadable"]=>
        bool(false)
        ["category_ids"]=>
        array(0) {
        }
        ["tag_ids"]=>
        array(0) {
        }
        ["shipping_class_id"]=>
        int(0)
        ["downloads"]=>
        array(0) {
        }
        ["image_id"]=>
        string(0) ""
        ["gallery_image_ids"]=>
        array(0) {
        }
        ["download_limit"]=>
        int(-1)
        ["download_expiry"]=>
        int(-1)
        ["rating_counts"]=>
        array(0) {
        }
        ["average_rating"]=>
        int(0)
        ["review_count"]=>
        int(0)
      }
      ["data_store":protected]=>
      object(WC_Data_Store)#8807 (4) {
        ["instance":"WC_Data_Store":private]=>
        object(WC_Product_Data_Store_CPT)#8806 (5) {
          ["internal_meta_keys":protected]=>
          array(39) {
            [0]=>
            string(11) "_visibility"
            [1]=>
            string(4) "_sku"
            [2]=>
            string(6) "_price"
            [3]=>
            string(14) "_regular_price"
            [4]=>
            string(11) "_sale_price"
            [5]=>
            string(22) "_sale_price_dates_from"
            [6]=>
            string(20) "_sale_price_dates_to"
            [7]=>
            string(11) "total_sales"
            [8]=>
            string(11) "_tax_status"
            [9]=>
            string(10) "_tax_class"
            [10]=>
            string(13) "_manage_stock"
            [11]=>
            string(6) "_stock"
            [12]=>
            string(13) "_stock_status"
            [13]=>
            string(11) "_backorders"
            [14]=>
            string(18) "_sold_individually"
            [15]=>
            string(7) "_weight"
            [16]=>
            string(7) "_length"
            [17]=>
            string(6) "_width"
            [18]=>
            string(7) "_height"
            [19]=>
            string(11) "_upsell_ids"
            [20]=>
            string(14) "_crosssell_ids"
            [21]=>
            string(14) "_purchase_note"
            [22]=>
            string(19) "_default_attributes"
            [23]=>
            string(19) "_product_attributes"
            [24]=>
            string(8) "_virtual"
            [25]=>
            string(13) "_downloadable"
            [26]=>
            string(9) "_featured"
            [27]=>
            string(19) "_downloadable_files"
            [28]=>
            string(16) "_wc_rating_count"
            [29]=>
            string(18) "_wc_average_rating"
            [30]=>
            string(16) "_wc_review_count"
            [31]=>
            string(22) "_variation_description"
            [32]=>
            string(13) "_thumbnail_id"
            [33]=>
            string(11) "_file_paths"
            [34]=>
            string(22) "_product_image_gallery"
            [35]=>
            string(16) "_product_version"
            [36]=>
            string(12) "_wp_old_slug"
            [37]=>
            string(10) "_edit_last"
            [38]=>
            string(10) "_edit_lock"
          }
          ["extra_data_saved":protected]=>
          bool(false)
          ["updated_props":protected]=>
          array(0) {
          }
          ["meta_type":protected]=>
          string(4) "post"
          ["object_id_field_for_meta":protected]=>
          string(0) ""
        }
        ["stores":"WC_Data_Store":private]=>
        array(18) {
          ["coupon"]=>
          string(24) "WC_Coupon_Data_Store_CPT"
          ["customer"]=>
          string(22) "WC_Customer_Data_Store"
          ["customer-download"]=>
          string(31) "WC_Customer_Download_Data_Store"
          ["customer-session"]=>
          string(30) "WC_Customer_Data_Store_Session"
          ["order"]=>
          string(23) "WC_Order_Data_Store_CPT"
          ["order-refund"]=>
          string(30) "WC_Order_Refund_Data_Store_CPT"
          ["order-item…

当我尝试循环它并存储序列化或编码值时,这就是我得到的:

{ ["98f13708210194c475687be6106a3b84"]=> array(10) 
{ ["product_id"]=> int(20) 
["variation_id"]=> int(0) 
["variation"]=> array(0) { } 
["quantity"]=> int(2) 
["line_total"]=> float(3001.98) 
["line_subtotal"]=> float(3001.98) 
["line_tax"]=> int(0) 
["line_subtotal_tax"]=> int(0) 
["line_tax_data"]=> array(2) {
 ["total"]=> array(0) { } 
["subtotal"]=> array(0) { } } 
["data"]=> array(0) { } } }`

我的问题:

  • 可能发生什么情况?
  • 为什么我会丢失这么多数据?
  • 如何获取缺失的数据?

最佳答案

您将丢失 ["data"],因为如您所见,它是一个 WC_product 对象,您可以直接访问数据,因为woocommerce 3+。

之后的所有其他数据都受到保护,因此您也无法获取它。

所以你需要这样使用 foreach 循环:

// iterating through each cart items
foreach ( WC()->cart->get_cart() as $item_key => $item_values ){

    // We set the cart item data in an array
    $wc_get_cart[$item_key] = $item_values;

    // We remove the WC_Product object from this array
    unset($wc_get_cart[$item_key]['data']);

    // We set the data with WC_Data get_data() method in a variable (array)
    $product_data = $item_values['data']->get_data();

    // We set back this data
    $wc_get_cart[$item_key]['data'] = $product_data;

}

现在您可以序列化 $wc_get_cart 多维数组,您将看到拥有所有需要的数据

So you can use WC_Data get_data() method to convert WooCommerce objects like Orders or Products in an array and then to access properties without using the classes methods.


关于arrays - 在 WooCommerce 3 中序列化购物车内容而不丢失数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44907363/

相关文章:

java - 在android中使用二维数组发送多个图像以使用改造将数据发布到服务器

javascript - 未捕获的类型错误 : Cannot read property '0' of undefined

php - 从多维数组中删除多个值

javascript - 使用 forEach 比较两个数组并根据条件添加不同的 html 元素

python - 制作一个二维字符串矩阵来匹配网格坐标

mysql - 我需要更改 Wordpress wp_ 前缀以阻止与服务器上其他站点的冲突

mysql - 从 WPDB 获取结果

php - 禁用跨域数据库

c - 将值分配给动态分配的二维指针数组时出现语法错误

c++ - 在 C++ 中使用数组