Ajax 在管理面板中更新产品 (WooCommerce)

Ajax update product in admin panel (WooCommerce)

你能帮帮我吗,我有任务: 我需要编写代码来更新 WooCommerce 产品(在管理页面上)而不重新加载页面,我想使用 ajax。 所以,我做了什么:我序列化数据并将其放入对象 formData 中。现在我有 $_POST['formData'] 默认 WordPress 提交表单的所有数据。

        var formData = new FormData();
        var formData = $(this).serialize();

        $.ajax({
          url: ajax_data.ajax_url,
          type: 'POST',
          data: {
            action     : 'save_post',
            formData   : formData,
          },

但我需要你的建议,哪些工具更好用。我知道 woocommerce 提供 wc_product class。但是在我的 $_POST['formData'] 中我有下一个输出:

array(91) {
  ["_wpnonce"]=>
  string(10) "d625c90e10"
  ["_wp_http_referer"]=>
  string(38) "/wp-admin/post.php?post=11&action=edit"
  ["user_ID"]=>
  string(1) "1"
  ["action"]=>
  string(8) "editpost"
  ["originalaction"]=>
  string(8) "editpost"
  ["post_author"]=>
  string(1) "1"
  ["post_type"]=>
  string(7) "product"
  ["original_post_status"]=>
  string(7) "publish"
  ["referredby"]=>
  string(61) "http://167.71.45.242:8081/wp-admin/edit.php?post_type=product"
  ["_wp_original_http_referer"]=>
  string(61) "http://167.71.45.242:8081/wp-admin/edit.php?post_type=product"
  ["post_ID"]=>
  string(2) "11"
  ["meta-box-order-nonce"]=>
  string(10) "7165f917f6"
  ["closedpostboxesnonce"]=>
  string(10) "5ebee8cce7"
  ["original_post_title"]=>
  string(32) "Тестовый товар №1"
  ["post_title"]=>
  string(32) "Тестовый товар №1"
  ["samplepermalinknonce"]=>
  string(10) "aa695f62d0"
  ["content"]=>
  string(0) ""
  ["wp-preview"]=>
  string(0) ""
  ["hidden_post_status"]=>
  string(7) "publish"
  ["post_status"]=>
  string(7) "publish"
  ["hidden_post_password"]=>
  string(0) ""
  ["hidden_post_visibility"]=>
  string(6) "public"
  ["visibility"]=>
  string(6) "public"
  ["post_password"]=>
  string(0) ""
  ["mm"]=>
  string(2) "04"
  ["jj"]=>
  string(2) "28"
  ["aa"]=>
  string(4) "2022"
  ["hh"]=>
  string(2) "10"
  ["mn"]=>
  string(2) "58"
  ["ss"]=>
  string(2) "07"
  ["hidden_mm"]=>
  string(2) "04"
  ["cur_mm"]=>
  string(2) "05"
  ["hidden_jj"]=>
  string(2) "28"
  ["cur_jj"]=>
  string(2) "02"
  ["hidden_aa"]=>
  string(4) "2022"
  ["cur_aa"]=>
  string(4) "2022"
  ["hidden_hh"]=>
  string(2) "10"
  ["cur_hh"]=>
  string(2) "12"
  ["hidden_mn"]=>
  string(2) "58"
  ["cur_mn"]=>
  string(2) "36"
  ["current_visibility"]=>
  string(7) "visible"
  ["current_featured"]=>
  string(2) "no"
  ["_visibility"]=>
  string(7) "visible"
  ["original_publish"]=>
  string(16) "Обновить"
  ["tax_input"]=>
  array(2) {
    ["product_cat"]=>
    array(2) {
      [0]=>
      string(1) "0"
      [1]=>
      string(2) "15"
    }
    ["product_tag"]=>
    string(0) ""
  }
  ["newproduct_cat"]=>
  string(46) "Название новой категории"
  ["newproduct_cat_parent"]=>
  string(2) "-1"
  ["_ajax_nonce-add-product_cat"]=>
  string(10) "e9bf3be16e"
  ["newtag"]=>
  array(1) {
    ["product_tag"]=>
    string(0) ""
  }
  ["post_type_nonce"]=>
  string(10) "bd08f5b974"
  ["product_type"]=>
  string(8) "frequent"
  ["created_date_nonce"]=>
  string(10) "2211e50c85"
  ["created_date"]=>
  string(10) "2022-04-27"
  ["_thumbnail_id"]=>
  string(2) "-1"
  ["meta_box_nonce"]=>
  string(10) "4aef179cf0"
  ["test_image"]=>
  string(0) ""
  ["woocommerce_meta_nonce"]=>
  string(10) "9e72d885d4"
  ["product_image_gallery"]=>
  string(0) ""
  ["product-type"]=>
  string(6) "simple"
  ["_product_url"]=>
  string(0) ""
  ["_button_text"]=>
  string(0) ""
  ["_regular_price"]=>
  string(4) "1000"
  ["_sale_price"]=>
  string(0) ""
  ["_sale_price_dates_from"]=>
  string(0) ""
  ["_sale_price_dates_to"]=>
  string(0) ""
  ["_download_limit"]=>
  string(0) ""
  ["_download_expiry"]=>
  string(0) ""
  ["_sku"]=>
  string(5) "test1"
  ["_stock"]=>
  string(1) "0"
  ["_original_stock"]=>
  string(1) "0"
  ["_backorders"]=>
  string(2) "no"
  ["_low_stock_amount"]=>
  string(0) ""
  ["_stock_status"]=>
  string(7) "instock"
  ["_weight"]=>
  string(0) ""
  ["_length"]=>
  string(0) ""
  ["_width"]=>
  string(0) ""
  ["_height"]=>
  string(0) ""
  ["product_shipping_class"]=>
  string(2) "-1"
  ["attribute_taxonomy"]=>
  string(0) ""
  ["_purchase_note"]=>
  string(0) ""
  ["menu_order"]=>
  string(1) "0"
  ["meta"]=>
  array(4) {
    [31]=>
    array(2) {
      ["key"]=>
      string(20) "product_created_date"
      ["value"]=>
      string(10) "2022-04-27"
    }
    [27]=>
    array(2) {
      ["key"]=>
      string(10) "test_image"
      ["value"]=>
      string(0) ""
    }
    [8]=>
    array(2) {
      ["key"]=>
      string(11) "total_sales"
      ["value"]=>
      string(1) "0"
    }
    [30]=>
    array(2) {
      ["key"]=>
      string(15) "type_of_product"
      ["value"]=>
      string(8) "frequent"
    }
  }
  ["_ajax_nonce"]=>
  string(10) "bf9cefe09d"
  ["metakeyselect"]=>
  string(6) "#NONE#"
  ["metakeyinput"]=>
  string(0) ""
  ["metavalue"]=>
  string(0) ""
  ["_ajax_nonce-add-meta"]=>
  string(10) "0187a6acae"
  ["post_name"]=>
  string(34) "тестовый-товар-№1-2"
  ["excerpt"]=>
  string(0) ""
  ["add_comment_nonce"]=>
  string(10) "8e3ae8767d"
  ["_ajax_fetch_list_nonce"]=>
  string(10) "56a7278515"

我们可以看到为我们提供 woocommerce 的属性

protected $data = array(
        'name'               => '',
        'slug'               => '',
        'date_created'       => null,
        'date_modified'      => null,
        'status'             => false,
        'featured'           => false,
        'catalog_visibility' => 'visible',
        'description'        => '',
        'short_description'  => '',
        'sku'                => '',
        'price'              => '',
        'regular_price'      => '',
        'sale_price'         => '',
        'date_on_sale_from'  => null,
        'date_on_sale_to'    => null,
        'total_sales'        => '0',
        'tax_status'         => 'taxable',
        'tax_class'          => '',
        'manage_stock'       => false,
        'stock_quantity'     => null,
        'stock_status'       => 'instock',
        'backorders'         => 'no',
        'low_stock_amount'   => '',
        'sold_individually'  => false,
        'weight'             => '',
        'length'             => '',
        'width'              => '',
        'height'             => '',
        'upsell_ids'         => array(),
        'cross_sell_ids'     => array(),
        'parent_id'          => 0,
        'reviews_allowed'    => true,
        'purchase_note'      => '',
        'attributes'         => array(),
        'default_attributes' => array(),
        'menu_order'         => 0,
        'post_password'      => '',
        'virtual'            => false,
        'downloadable'       => false,
        'category_ids'       => array(),
        'tag_ids'            => array(),
        'shipping_class_id'  => 0,
        'downloads'          => array(),
        'image_id'           => '',
        'gallery_image_ids'  => array(),
        'download_limit'     => -1,
        'download_expiry'    => -1,
        'rating_counts'      => array(),
        'average_rating'     => 0,
        'review_count'       => 0,
    );

这与提交的产品页面的默认 post 形式不匹配。

问题:我需要使用那个 wc class (wc_product) 还是我需要使用不同的东西来解决我的问题?

$product = new WC_Product_Simple($product_id);
$product->set_regular_price(15);
$product->save();

您可以使用产品 类,例如 WC_Product_GroupedWC_Product_ExternalWC_Product_Variable... 对应的产品类型。