ksanyok 3 Posted February 26, 2018 Report Share Posted February 26, 2018 Всем привет.Необходимо на определенные категории добавить в магазине кнопку Демоверсии после корзины.Есть такой код у меня. // Add demo preview button to single product page function isa_after_add_to_cart_button() { global $post; $demoslug = $post->post_name; $demourl = get_bloginfo('url').'/preview?theme='.$demoslug.''; $demotitle = esc_attr($post->post_title); echo '<a href="'.$demourl.'" title="'.$demotitle.'" class="sc-button demo-preview" rel="nofollow" target="_blank">Демо-сайт</a>'; } add_action('woocommerce_after_add_to_cart_button','isa_after_add_to_cart_button'); Но данный код добавляет кнопку на все товары.Ранее я делал дурацкий код для того что бы убрать (скрыть) кнопку с ненужных категорий и делал это так: // Hide shop items for services category function services_remove_woocommerce(){ global $product, $post; $target_category = 'services'; // Remove shop items from the Single Product page if ( is_product() ) { $terms = wp_get_post_terms( $post->ID, 'product_cat' ); foreach ( $terms as $term ) $categories***91;***93; = $term->slug; if ( in_array( $target_category, $categories ) ){ ?> <style type="text/css"> a.sc-button.demo-preview { display: none; } </style> <?php } } } add_action( 'wp_head', 'services_remove_woocommerce' ); Подскажите пожалуйста как просто установить кнопку на некотоыре категории. Кнопку нужно по сути добавить на товар из осного раздела и на его подкатегории. Link to post Share on other sites
kuzovbmw 587 Posted February 26, 2018 Report Share Posted February 26, 2018 Я охреневаю над задачами. Вы скинули солидный кусок кода, + тут нету полной логики WC это раз. Идите на фриланс и платите. Тут Вам никто такое за бесплатно делать не будет Link to post Share on other sites
ksanyok 3 Posted February 26, 2018 Author Report Share Posted February 26, 2018 5 минут назад, kuzovbmw сказал: Я охреневаю над задачами. Вы скинули солидный кусок кода, + тут нету полной логики WC это раз. Идите на фриланс и платите. Тут Вам никто такое за бесплатно делать не будет По вашему код из 7 строк это "солидный кусок кода"??? Link to post Share on other sites
kuzovbmw 587 Posted February 26, 2018 Report Share Posted February 26, 2018 Я так подозреваю что этих 7 строк не хватит на проверку Link to post Share on other sites
ksanyok 3 Posted February 26, 2018 Author Report Share Posted February 26, 2018 По сути эти 7 сторк нужно дополнить. Как я понимаю опираться стоит на $target_category = 'services'; Link to post Share on other sites
petroff 648 Posted February 26, 2018 Report Share Posted February 26, 2018 58 минут назад, ksanyok сказал: По сути эти 7 сторк нужно дополнить. Как я понимаю опираться стоит на $target_category = 'services'; Функция проверки поста на принадлежность категории in_category не работает? Link to post Share on other sites
VIP vituson 675 Posted February 26, 2018 VIP Report Share Posted February 26, 2018 По коду не подскажу, не силен, но попробуйте проверять код ID категории. Что-то вроде: if category id=...., то .... иначе ..... https://wp-kama.ru/function/get_cat_id Link to post Share on other sites
VIP campusboy 912 Posted February 26, 2018 VIP Report Share Posted February 26, 2018 Чисто теоретически написал: function after_cart_btn_add_preview_btn() { global $post; $term = array( 'term_slug_1', 'term_slug_2', 'term_slug_3' ); if ( is_product() && has_term( $term, 'product_cat', $post ) ) { $demourl = home_url() . "/preview?theme={$post->post_name}"; $demotitle = esc_attr( $post->post_title ); $class = 'sc-button demo-preview'; $link = '<a href="%s" title="%s" class="%s" rel="nofollow" target="_blank">Демо-сайт</a>'; printf( $link, $demourl, $demotitle, $class ); } } add_action( 'woocommerce_after_add_to_cart_button', 'after_cart_btn_add_preview_btn' ); ksanyok 1 Link to post Share on other sites
ksanyok 3 Posted February 27, 2018 Author Report Share Posted February 27, 2018 10 часов назад, campusboy сказал: Чисто теоретически написал: function after_cart_btn_add_preview_btn() { global $post; $term = array( 'term_slug_1', 'term_slug_2', 'term_slug_3' ); if ( is_product() && has_term( $term, 'product_cat', $post ) ) { $demourl = home_url() . "/preview?theme={$post->post_name}"; $demotitle = esc_attr( $post->post_title ); $class = 'sc-button demo-preview'; $link = '<a href="%s" title="%s" class="%s" rel="nofollow" target="_blank">Демо-сайт</a>'; printf( $link, $demourl, $demotitle, $class ); } } add_action( 'woocommerce_after_add_to_cart_button', 'after_cart_btn_add_preview_btn' ); Огромное спасибо. Сработало.) Link to post Share on other sites
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now