Вот пользовательская функция, которая будет показывать пользовательское сообщение на странице корзины, с категорией страниц архива ссылка на первый продукт был добавлен в корзину.
Если товар без категории добавлен в корзину, ничего не отображается.
Опционально вы также можете отобразить его на странице проверки (uncommenting last line).
Вот этот код:
function cart_custom_message() {
if(!WC()->cart->is_empty){
//Iterating each item in the cart
foreach (WC()->cart->get_cart() as $item) {
// Get the product categories object of the current item
$categories_obj = get_the_terms($item['product_id'], 'product_cat');
if($categories_obj) break; // when an item has a product category, stops the loop
}
if($categories_obj) {
//Iterating each category of the first item
foreach ($categories_obj as $category) {
break; // stop the loop to on the first category
}
$category_id = $category->term_id; // the category id
$category_name = $category->name; // the category name
$category_slug = $category->slug; // the category slug
$category_url = get_term_link($category_slug, 'product_cat'); // the link to category archive pages
// Your message (translatable)
$message = __("Add more T-shirts from <a href='$category_url'><strong>$category_name</strong></a> category and get a discount!", "your_theme_domain");
echo "<div class='woocommerce-info'>$message</div>";
}
}
}
// Display message on cart page
add_action('woocommerce_before_cart', 'cart_custom_message');
// Display message on checkout page (optionally, if needed, uncomment the line below)
// add_action('woocommerce_before_checkout_form', 'cart_custom_message', 5);
код идет в function.php файле Вашего активного ребенка темы (или темы), или же в любом файле плагина.
Этот код протестирован и полностью работоспособен.
Ссылки:
я не могу поверить, насколько хорошо это работает !! Огромное спасибо! – sebas
Зачем нужна «your_theme_domain» – sebas
Последний вопрос !! Как я могу воспроизвести одно и то же сообщение прямо под таблицей содержимого корзины? – sebas