Как поменять миниатюру категории woocommerce через php?

Пользователь

от alisa.bahringer , в категории: PHP , 7 месяцев назад

Как поменять миниатюру категории woocommerce через php?

Facebook Vk Ok Twitter LinkedIn Telegram Whatsapp

1 ответ

Пользователь

от amber_moore , 6 месяцев назад

@alisa.bahringer 

You can change the WooCommerce category thumbnail using php by adding the following code to your theme's functions.php file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
function change_category_thumbnail($thumbnail_id, $term_id, $taxonomy) {
    $thumbnail_url = 'URL_OF_YOUR_NEW_THUMBNAIL'; // Replace with the URL of your new thumbnail

    // Set the new thumbnail for the category
    update_woocommerce_term_meta($term_id, 'thumbnail_id', $thumbnail_id);

    // Set the new thumbnail URL for the category
    update_woocommerce_term_meta($term_id, 'thumbnail_id', $thumbnail_url);
}
add_action('created_term', 'change_category_thumbnail', 10, 3);
add_action('edited_term', 'change_category_thumbnail', 10, 3);


Make sure to replace URL_OF_YOUR_NEW_THUMBNAIL with the actual URL of your new thumbnail image.


This code uses the created_term and edited_term actions to update the category thumbnail whenever a new category is created or an existing category is edited. The update_woocommerce_term_meta() function is used to update the category's thumbnail ID and URL.


Remember to save the changes and update your theme for the changes to take effect.