$prd = wc_get_product( $clrpost );
$color = substr($prd->get_sku(), 8, 4);
$imgcss = 'border: 0px solid black;';
if ( $sku == $prd->get_sku() ) { $imgcss = 'border: 1px solid black;'; }
$imgcss = $imgcss . ' margin: 2px; padding: 2px;';
<?php
/**
* @version 1.0
*/
/*
Plugin Name: Выбор расцветок товара
Description: Плагин для отображения товаров других расцветок товара.
Author: Anatoliy Pychev (Rebuild for Maksym Demydow)
Version: 1.0
Author URI:
*/
class Hummel_Color_Upsells_Plugin {
private static $notices = array();
public static function init() {
register_activation_hook( __FILE__, array( __CLASS__, 'activate' ) );
register_deactivation_hook( __FILE__, array( __CLASS__, 'deactivate' ) );
register_uninstall_hook( __FILE__, array( __CLASS__, 'uninstall' ) );
add_action( 'init', array( __CLASS__, 'wp_init' ) );
add_action( 'admin_notices', array( __CLASS__, 'admin_notices' ) );
}
public static function wp_init() {
$active_plugins = get_option( 'active_plugins', array() );
$woocommerce_is_active = in_array( 'woocommerce/woocommerce.php', $active_plugins );
if ( !$woocommerce_is_active ) {
self::$notices[] = '<div class="error"><p><strong>Hummel Orders Exporter Plugin</strong> требуется наличие активнокго плагина Woocommerce.</p></div>';
} else {
// добавляем обработчик
add_action( 'woocommerce_before_add_to_cart_form', array( __CLASS__, 'display_color_upsells' ), 10 );
}
}
/**
* Выводит на фронт миниатюру этого же товар в другой расцветке
*/
public static function display_color_upsells() {
global $post, $woocommere, $product;
//error_log(__METHOD__ . PHP_EOL , 3, WP_PLUGIN_DIR . '/hummel-color-upsells/log.log');
$test = get_post_meta( $post->ID, '_newsku', true );
$newsku = $test;
if ( !$newsku || 'bndl' == substr( $newsku, 0, 4 ) ) { return; }
$article = substr( $newsku, 0, 7 );
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'numberposts' => -1,
'meta_query' => array(
array(
'key' => '_newsku',
'value' => $article,
'compare' => 'LIKE',
),
array(
'key' => '_stock_status',
'value' => 'instock'
)
)
) ;
$clrposts = get_posts($args);
//print_r( $args );
if ( $clrposts ){
if( $product->is_type( 'variable' ) ){
echo '<p id="linked" style="margin: 0px;"><strong>Этот товар в другом цвете</strong></p>';
echo '<div class="color-upsells" style="display:flex; display:-ms-flexbox;">';
}
elseif( $product->is_type( 'simple' ) ){
echo '';
}
foreach( $clrposts as $clrpost ){
$prd = wc_get_product( $clrpost );
//print_r( $clrpost );
$color = substr($prd->$test, 8, 4);
$imgcss = 'border: 0px solid black;';
if ( $newsku == $prd->$test ) { $imgcss = 'border: 1px solid black;'; }
$imgcss = $imgcss . ' margin: 2px; padding: 2px;';
$html = '<a href="' . get_permalink( $clrpost ) .
'" title="' . $color .
'" alt="' . esc_attr( get_the_title( $clrpost ) ) .
'">' . get_the_post_thumbnail($clrpost, array(45,45), array('style' => $imgcss) ) .
'</a>';
if( $product->is_type( 'variable' ) ){
echo $html;
}
elseif( $product->is_type( 'simple' ) ){
echo '';
}
}
if( $product->is_type( 'variable' ) ){
echo '</div>';
}
elseif( $product->is_type( 'simple' ) ){
echo '';
}
}
}
/**
* Plugin display notices.
*
*/
public static function admin_notices() {
if ( !empty( self::$notices ) ) {
foreach ( self::$notices as $notice ) {
echo $notice;
}
}
}
/**
* Plugin activation work.
*
*/
public static function activate() {
}
/**
* Plugin deactivation.
*
*/
public static function deactivate() {
}
/**
* Plugin uninstall.
*
*/
public static function uninstall() {
}
}
Hummel_Color_Upsells_Plugin::init();
<span itemprop="itemListElement" property="itemListElement" typeof="ListItem" itemscope itemtype="http://schema.org/ListItem"><a itemprop="item" property="item" typeof="WebPage" title="%title%." href="%link%" class="%type%"><span itemprop="name" property="name">%htitle%</span></a><meta itemprop="position" property="position" content="%position%"></span>
<span property="itemListElement" typeof="ListItem" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
<a itemprop="item" property="item" typeof="WebPage" title="%title%" href="%link%" class="%type%">
<span itemprop="name" property="name">%htitle%</span></a>
<meta itemprop="position" property="position" content="%position%">
</span>
<span itemprop="itemListElement" property="itemListElement" typeof="ListItem" itemscope itemtype="http://schema.org/ListItem"><a itemprop="item" property="item" typeof="WebPage" title="%title%." href="%link%" class="%type%"><span itemprop="name" property="name">%htitle%</span></a><meta itemprop="position" property="position" content="%position%"></span>
.woocommerce-page ul.products li.product .woocommerce-product-details__short-description {display:none}
.woocommerce-page ul.products li.product:hover .woocommerce-product-details__short-description{display:block}