• Никакой политики на форуме. Иначе - бан!
  • Вопрос без рабочей ссылки на проблему считается риторическим. Без ссылки и скриншота - провокацией!
  • Темы озаглавленные с маленькой буквы или капсом удаляются без предупреждения!

Изменение названия товара если заполнены атрибуты

Kycher

Новичок
При наличии заполненных атрибутов - название товара выводить как атрибут
Подробнее:
У товара есть много атрибутов, включая vendor и model. Если данные атрибуты заполнены, то название товара нужно вывести как «vendor»+пробел+«model». Если данные атрибуты не заполнены, то выводить обычное название товара.
Есть вот такой код, на странице товара всё работает, как можно теперь везде заменять название товара?

<?php the_post(); the_content(); ?>
<?php if ( $product->get_attribute('5ec76b1834812') and $product->get_attribute('5ec76b169583b') ) {
$pr=$product->get_attribute('5ec76b169583b').' '.$product->get_attribute('5ec76b1834812') ;}
else {
$pr=get_the_title();}
echo $pr;
?>

1693479837233.png
<?php get_header();
$cat_id = woocommerce_category_data();

$user_email = '';
$user_firstname = '';
$user_lastname = '';
$phone = '';
$address1 = '';
if(is_user_logged_in()){
$current_user = wp_get_current_user();
$user_email = $current_user->user_email;
$user_firstname = $current_user->user_firstname;
$user_lastname = $current_user->user_lastname;
$phone = get_user_meta($current_user->ID, 'phone', true);
$address1 = get_user_meta($current_user->ID, 'address1', true);
}
$current_user_roles = array();
foreach ($current_user->roles as $user_role) {
$current_user_roles[] = $user_role;
}

?>


<main>
<div class="container">
<div class="crumbs">
<ul>
<li><a href="<?php echo get_permalink(6); ?>">Главная</a></li>
<li><a href="<?php echo get_term_link($cat_id, 'product_cat'); ?>">Каталог</a></li>
<li><a href="<?php echo get_permalink(get_the_ID()); ?>"><?php echo get_the_title(); ?></a></li>
</ul>
</div><!-- end crumbs -->
<div class="main-info">

<h2 class="main-headline">

<?php the_post(); the_content(); ?>
<?php if ( $product->get_attribute('5ec76b1834812') and $product->get_attribute('5ec76b169583b') ) {
$pr=$product->get_attribute('5ec76b169583b').' '.$product->get_attribute('5ec76b1834812') ;}
else {
$pr=get_the_title();}
echo $pr;
?>

</h2>
<div class="about-product">
<div class="gallery">
<?php
$_product = wc_get_product( get_the_ID() );
$attachment_ids = $_product->get_gallery_image_ids();
if(count($attachment_ids) > 0){
foreach( array_slice( $attachment_ids, 0,3 ) as $attachment_id ) {
$thumbnail_url = wp_get_attachment_image_src( $attachment_id, 'full' )[0];
echo'<img src="'.$thumbnail_url.'" alt="'.strip_tags(get_the_title()).'">';
} //endforeach
} //endif count
elseif(get_the_post_thumbnail_url( $_product->id )){
echo '<img src="'.get_the_post_thumbnail_url( $_product->id, "full").'" alt="'.strip_tags(get_the_title()).'">';
}
else{
$img = get_field( "img_default", 6 );
echo'<img src="'.$img["url"].'" alt="'.strip_tags(get_the_title()).'">';
}
?>
</div><!-- end gallery -->
<div class="description">


<div class="sum">
<h2 id="price" data-price="<?php echo $_product->get_price(); ?>" data-quantidi="<?php echo $_product->get_stock_quantity(); ?>" data-lowstock="<?php echo $_product->get_low_stock_amount(); ?>"><?php echo $_product->get_price(); ?> р.</h2>
<div class="count-product">
<label for="count">Количество :</label>
<input type="number" class="pp-number" id="count" value="1">
</div>
</div><!-- end sum -->
<div class="buy-product">
<div class="button to-basket">
<a href="javascript:void(0);" data-product_add="<?php echo get_the_ID(); ?>"><img src="<?= get_template_directory_uri().'/img/red-basket.png'; ?>" alt="red" class="img-basc"><img src="<?= get_template_directory_uri().'/img/white-basket.png'; ?>" alt="white" class="img-basc">В корзину</a>
</div>
<div class="button-red buy-now">
<a href="javascript:void(0);" data-oneclick="<?php echo get_the_ID(); ?>" class="buy-one-click">Купить в 1 клик</a>
</div>
</div><!-- end buy product -->
</div><!-- end description -->
</div><!-- end about product -->
</div><!-- end main info -->

<div class="delivery">
<?php
//получить точки самовывоза товаров и оставить только уникальные
if (in_array('user_magazin', $current_user_roles)) {
$pickupPosts = get_all_pickup_with_coord($current_user->ID);
} else {
$pickupPosts = get_all_pickup_with_coord();
}
$defaultPoint = get_user_meta($current_user->ID, 'default_pickup', true);
if(count($pickupPosts) > 0) :?>
<?php
$pickupPoints = array();
foreach ($pickupPosts as $pickupPost) {
$pickupPost->the_post();
$pointMeta = get_post_meta(get_the_ID());
array_push($pickupPoints, array(
'active_point' => (isset($pointMeta["active_point"]) && $pointMeta["active_point"][0] == '1') ? true : false,
'title' => get_the_title(),
'coords' => $pointMeta["coords"][0]
));
}
?>
<div class="pickup">
<div class="adress">
<h3>
<?php
if (in_array('user_magazin', $current_user_roles)) {
echo 'Ваши точки самовывоза';
} else {
echo 'Выберите точку самовывоза';
}
?>
</h3>
<? if (!in_array('user_magazin', $current_user_roles)): ?>
<a href="#" id="filter-pickup1" class="pickup-link">Показать доступные точки самовывоза</a>

<a href="<?= $_SERVER["REQUEST_URI"] ?>"
id="checkCLickN"
class="pickup-link">Показать все точки самовывоза</a>
<? endif ?>
<select id="pickup" name="pickup">
<option disabled value="" data-point-coords="">Выбрать точку самовывоза</option>
<?php foreach($pickupPoints as $pickupPoint) :?>
<?php if($pickupPoint["active_point"]):?>
<?php $selected = ($defaultPoint == $pickupPoint['title']) ? 'selected' : '';?>
<option data-point-coords="<?=$pickupPoint['coords']?>" label="<?=$pickupPoint['title']?>" value="<?=$pickupPoint['title']?>" <?=$selected?>><?= $pickupPoint["title"] ?></option>
<?php endif;?>
<?php endforeach;?>
</select>
</div><!-- end adres -->
<?php // <div class="map-basket" id="map-basket"></div>?>
<div id="basket-map"></div>
</div><!-- end pickup -->
<?php endif; ?>
</div><!-- end delivery -->
<?php
$crosssell_ids = get_post_meta( get_the_ID(), '_crosssell_ids' );
// var_dump($crosssell_ids);
//$crosssell_ids=$crosssell_ids[0];
if($crosssell_ids){
$crosssell_ids=$crosssell_ids[0];
?>
<div class="similar-products">
<h2 class="main-headline">С этим товаром чаще всего выбирают</h2>
<div class="all-similar">
<?php
$pc = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => 4,
'post__in' => $crosssell_ids,
'orderby' => 'rand',
));
if($pc->have_posts()){
while ($pc->have_posts()) {
$pc->the_post();
?>
<div class="one-product-catalog">
<div class="image" onclick="document.location.href='<?php echo get_the_permalink(); ?>'">
<img src="<?php echo get_the_post_thumbnail_url( get_the_ID(), 'full' ); ?>" alt="<?php echo strip_tags(get_the_title()); ?>">
</div>
<h3 onclick="document.location.href='<?php echo get_the_permalink(); ?>'"><?php echo get_the_title(); ?></h3>
<div class="more-info">
<p>подробнее</p>
<?php
$_product2 = wc_get_product( get_the_ID() );
echo'<h3 id="cross_price'.get_the_ID().'" data-cross_price="'.$_product2->get_price().'">'.$_product2->get_price().' р.</h3>';
?>
</div><!-- end more info -->
<div class="buy-bask">
<input type="number" class="pp-cross-number" id="cross_product<?php echo get_the_ID(); ?>" data-cross_product="<?php echo get_the_ID(); ?>" data-quantity="<?php echo $_product2->get_stock_quantity(); ?>" value="1">
<div class="button">
<a href="javascript:void(0);" data-cross_add="<?php echo get_the_ID(); ?>"> <img src="<?= get_template_directory_uri().'/img/red-basket.png'; ?>" alt="red" class="img-basc"><img src="<?= get_template_directory_uri().'/img/white-basket.png'; ?>" alt="white" class="img-basc">
В корзину</a>
</div>
</div><!-- end buy bask -->
</div><!-- end one product -->

<?php
}
wp_reset_postdata();
} //endif
?>
</div><!-- end all similar -->
</div><!-- end similar -->
<?php
} //endif count crossseills
?>
</div><!-- end container -->
</main>
<?php get_footer(); ?>
 
Последнее редактирование:
Сверху Снизу