Skip to content
Advertisement

Issue with WooCommerce WC_Product set_category_ids() method in ACF “acf/save_post” hook

I have a problem assigning a product category to a Woocommerce product that I create with Php. (Environment is WordPress 5.6.1, Woocommerce 5.0, Php 7.4.14, ACF Pro 5.9.5, Oxygen 3.6.1)

All other attributes for the product gets saved while creating it, and I can see that the product object get updated with the category id prior to saving.

However, when saving – no product category gets assigned to the product. On the contrary – the product don´t even get the “Uncategorized” category. It gets none!

This is how I create the product.

$product = new WC_Product_Simple();

$product->set_status("publish");

$product->set_name($title);

$product->set_category_ids([$product_category]);

$product->set_stock_status('instock');

$product->set_catalog_visibility('visible');

$product->set_sold_individually(false);

$product->set_regular_price('300');

$product->set_description('Product description');

$product->set_image_id($logo_id);

$product_id=$product->save();

The code runs from my own plugin, and gets called from this action:

add_action('acf/save_post', 'my_create_product_from_form_submit', 20);

The idea is that a custom post type using an ACF form is used to fill the data for the product. After the custom post type post is published and data saved with ACF, my code triggers and creates a product from the data entered into the form.

Everything works fine – the custom post is created, as is the product. All attributes are saved to the product. The only problem is that the product never becomes assigned to a product category.

I have no errors in any log. It´s like the system completely ignores this line:

 $product->set_category_ids([$product_category]);

This is a dump of the product object, after the product->save() command is run. Note that on line 59, the category_ids are there.

Its, just like everything is set to work – but the category id is not written to the db.

Any suggestions on how to solve this is highly appreciated.

   'object_type' => 'product',
   'post_type' => 'product',
   'cache_group' => 'products',
   'data' =>
  array (
    'name' => 'Name',
    'slug' => '',
    'date_created' =>
    WC_DateTime::__set_state(array(
       'utc_offset' => 0,
       'date' => '2021-02-16 23:58:03.000000',
       'timezone_type' => 3,
       'timezone' => 'Europe/Stockholm',
    )),
    'date_modified' => NULL,
    'status' => 'publish',
    'featured' => false,
    'catalog_visibility' => 'visible',
    'description' => 'Product description',
    'short_description' => '',
    'sku' => '',
    'price' => '300',
    'regular_price' => '300',
    '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 (
      0 => 33,
    ),
    'tag_ids' =>
    array (
    ),
    'shipping_class_id' => 0,
    'downloads' =>
    array (
    ),
    'image_id' => false,
    'gallery_image_ids' =>
    array (
    ),
    'download_limit' => -1,
    'download_expiry' => -1,
    'rating_counts' =>
    array (
    ),
    'average_rating' => 0,
    'review_count' => 0,
  ),
   'supports' =>
  array (
    0 => 'ajax_add_to_cart',
  ),
   'id' => 956,
   'changes' =>
  array (
  ),
   'object_read' => true,
   'extra_data' =>
  array (
  ),
   'default_data' => (ignoring the rest)

Advertisement

Answer

Mandatory: The product category that you set on the product, need to be an existing product category term ID (and doesn’t work with WordPress category terms Ids).

I have tested your code and it works if variable $product_category is an integer, so try to replace:

$product->set_category_ids([$product_category]);

with:

$product->set_category_ids( array_map('intval', [$product_category]) );

This should work.

Documentation: array_map() and intval() functions.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement