• Resolved rafaelrafael

    (@rafaelrafael)


    Hello! I’m trying to create attributes and variations programmatically in WooCommerce and having an issue I don’t know how to resolve. I use the code that adds a local attribute ‘Color’ with the value ‘Red’ and creates a variation based on that value. When I click ‘Publish’ on a new product/post, nothing appears in the ‘Attributes’ and ‘Variations’ tabs on the editing page. However, on the client side, on the product page, both the color and the option ‘Red’ appear and in database I can see them too.

    Here’s the interesting part – afterward, no matter how many times I click the ‘Update’ button for the product/post, nothing appears on the editing page. But as soon as I refresh the page in the BROWSER (not the post refresh button, but refreshing the browser content), everything appears on the product editing page in the ‘Attributes’ and ‘Variations’ tabs and the number of variations equals the number of times I updated the product. Also, the values for the variations are not set; I have to set them manually.

    I don’t understand what the issue is and desperately need help. Please help me resolve this problem? Here is the code I use:

    $color = 'Red';
    $taxonomy = 'color'; 
    
    $attribute = new WC_Product_Attribute();
    $attribute->set_name('Color');
    $attribute->set_options(array(
        $color,
        ));
    
    $attribute->set_position(1);
    $attribute->set_visible(1);
    $attribute->set_variation(1);
    
    
    $product = wc_get_product($product_id);
    $product->set_attributes(array($attribute));
    $product->save();
    
    
    $attribute_term = get_term_by('name', $color, $taxonomy);
    
    $variation = new WC_Product_Variation();
    $variation->set_sku('yourSku_' . time());
    $variation->set_regular_price(127);
    $variation->set_parent_id($product_id);
    $variation->set_weight('50g');
    $variation->set_attributes(array($taxonomy => $attribute_term->term_id));
    $variation->set_status('publish');
    $variation->save();   
    
    $product->save();
    
    //echo the data
    $variation_data = $variation->get_data();
    echo '<pre>';
    print_r($variation_data);
    echo '</pre>';
    • This topic was modified 9 months, 1 week ago by rafaelrafael.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support ckadenge (woo-hc)

    (@ckadenge)

    Hi there @rafaelrafael, thank you for reaching out.

    Based on your description, it seems like the issue might be related to caching, as the changes you make are only visible after refreshing the browser.

    Meanwhile, I’d like to understand your site properly. Please share with us the necessary information below for us to investigate the issue further:

    System Status Report, which you can find via WooCommerce > Status > Get system report > Copy for support.
    You could copy and paste your reply here or paste it via https://gist.github.com/ and send the link here.

    Cheers!

    Thread Starter rafaelrafael

    (@rafaelrafael)

    I haven’t noticed any problems with caching, besides, when I update the post the page refreshes anyway, but I think it saves the page as it is – empty. But that’s only my guess. Here is the copied system report:

    Адрес WordPress (URL):https://rafmedia.test-handyhost.ruАдрес сайта (URL):https://rafmedia.test-handyhost.ruВерсия WooCommerce:8.4.0Пакет WooCommerce REST API: 8.4.0 /var/www/user9663/data/www/rafmedia.test-handyhost.ru/wp-content/plugins/woocommerce/includesПакет WooCommerce Blocks: 11.6.2 /var/www/user9663/data/www/rafmedia.test-handyhost.ru/wp-content/plugins/woocommerce/packages/woocommerce-blocks/Пакет планировщика действий: 3.7.0 /var/www/user9663/data/www/rafmedia.test-handyhost.ru/wp-content/plugins/woocommerce/packages/action-schedulerДиректория журнала перезаписываема: /var/www/user9663/data/www/rafmedia.test-handyhost.ru/wp-content/uploads/wc-logs/Версия WordPress:6.4.2WordPress режим сети:–WordPress ограничение памяти:300 МБWordPress режим отладки:WordPress cron:Язык:ru_RUВнешний кэш объектов:

    Plugin Support Beauty of Code (woo-hc)

    (@beautyofcode)

    Hey there ??

    It looks like your custom code is not functioning as expected, correct?

    Please note that help with custom development is outside?our scope of support. It means that we only offer guidance for the default features and functionality of our products that do not require any custom code to function or integrate.

    We have our developer resources portal that can help you get going, so I would recommend checking it further if you need more help with this:

    https://developer.woocommerce.com/
     
    Another option is to check our customizations page to hire an expert who can create a custom solution for you: 

    https://woocommerce.com/experts/

    On the other hand, we’ll leave this open for a bit to see if anyone in the community can chime in with a possible solution for you. ??

    Cheers!

    Thread Starter rafaelrafael

    (@rafaelrafael)

    Thank you, I’ll check the links. Yes, please, it would be awesome if you leave it open, because when I was googling people have different issues with this one

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Attributes and variations don’t appear on the edit product page’ is closed to new replies.