Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Ajax update product in admin panel (WooCommerce)

can you please help me, I have task:
I need to write code that update WooCommerce product (on admin page) without page reload, I want to use ajax.
So, what I did: I’m serialize data and put it in object formData. And now I have $_POST[‘formData’] with all data of the default WordPress submit form.

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

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

But i’m need your suggest, which tools to use better. I know that woocommerce offer wc_product class. But in my $_POST[‘formData’] i have next output:

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"

And we can see properties which offer us woocommerce

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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,
    );

That is not match with default post form of product page by submit.

Question: Do i need to use that wc class (wc_product) or i need to use something different to solve my issue?

>Solution :

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

You can use product classes like WC_Product_Grouped, WC_Product_External, WC_Product_Variable… for corresponding product types.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading