Всем привет. Есть у меня магазин на wordpress+woocommerce. В файле кофигурации добавляю хук с нужным мне функционалом
if (!class_exists('MAD_WOOCOMMERCE_CONFIG')) {
class MAD_WOOCOMMERCE_CONFIG {
function __construct() {
$this->woocommerce_add_hooks();
}
public function woocommerce_add_hooks() {
add_action('woocommerce_single_product_summary', array(&$this, 'mad_woocommerce_free_gifts'), 16);
}
public static function get_free_products( $options = array(), $limit = 15 )
{
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => $limit,
'cache_results' => false,
'no_found_rows' => true
);
//merge default and user options
$args = array_merge($args, $options);
$products = new WP_Query( $args );
wp_reset_postdata();
return $products;
}
public function get_free_product_details( $product_id )
{
$options = array( 'p' => $product_id );
$product_details = self::get_free_products( $options );
$wfg_product_details = array();
if( !empty($product_details) && !empty($product_details->posts) ) {
$wfg_product_details['detail'] = $product_details->post;
$product_image = wp_get_attachment_image_src( get_post_thumbnail_id( $product_details->post->ID ), 'thumbnail' );
$wfg_product_details['image'] = isset($product_image[0]) ? $product_image[0] : false;
}
return (object) $wfg_product_details;
}
function mad_woocommerce_free_gifts() {
global $product;
$wfg_product_details = array();
$wfg_free_products[] = get_free_product_details( $product );
var_dump($wfg_free_products);
}
}
}
Поправил по рекомендациям - как-то так должно быть