Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
5.8 kB
3
Indexable
Never
global $marketkingpro_public;
$position = get_option('marketking_offers_position_setting', 'belowproduct');
if ($position === 'belowproduct'){
	remove_action('woocommerce_after_single_product_summary', array($marketkingpro_public,'add_offers_product_page'), 9);
	add_action('woocommerce_after_single_product_summary', 'custom_offers_function_product_page', 9);

} else if ($position === 'insideproducttabs'){
	remove_action('marketking_show_other_offers', array($marketkingpro_public,'add_offers_product_page'), 10);
	add_action('marketking_show_other_offers', 'custom_offers_function_product_page', 10);

} else if ($position === 'belowproducttabs'){
	remove_action('woocommerce_after_single_product_summary', array($marketkingpro_public,'add_offers_product_page'), 10);
	add_action('woocommerce_after_single_product_summary', 'custom_offers_function_product_page', 10);

}

function custom_offers_function_product_page(){
	global $post;
	$product_id = $post->ID;

	if (is_product()){
		// if there are other offers 
		$otheroffers = marketking()->get_linkedproducts($product_id, 'array');
		if (!empty($otheroffers)){
			$offers_number = count($otheroffers);
			if ($offers_number > 1){
				$offers_number_text = esc_html__('offers','marketking');
			} else {
				$offers_number_text = esc_html__('offer','marketking');
			}
			?>
			<div id="marketking_product_other_offers">
				<h3 class="marketking_other_offers_header">
				    <?php esc_html_e('Other offers','marketking'); ?>
				    &nbsp;<small>(<?php echo esc_html($offers_number.' '.$offers_number_text); ?>)</small>
				</h3>
				<?php
				// for each offer, show here
				$offers_shown_default = get_option('marketking_offers_shown_default_number_setting', 1);

				// Sort offers based on priority
				$stockpriority = get_option('marketking_stock_priority_setting', 'instock');
				$vendorpriority = get_option('marketking_vendor_priority_setting', 'lowerprice');

				if ($stockpriority === 'instock'){
					// split offers into 2 groups, instock and out of stock
					$instockoffers = marketking()->split_linkedproducts($otheroffers, 'instock');
					$outofstockoffers = marketking()->split_linkedproducts($otheroffers, 'outofstock');

					// merge groups again
					$instockoffers = marketking()->sort_linkedproducts($instockoffers, $vendorpriority);
					$outofstockoffers = marketking()->sort_linkedproducts($outofstockoffers, $vendorpriority);
					$otheroffers = array_merge($instockoffers, $outofstockoffers);
				} else if ($stockpriority === 'none'){
					$otheroffers = marketking()->sort_linkedproducts($otheroffers, $vendorpriority);
				}

				$i = 0;
				foreach ($otheroffers as $offerproduct_id){
					$i++;
					$offerproduct = wc_get_product($offerproduct_id);
					$vendor_id = marketking()->get_product_vendor($offerproduct_id);
					$store_name = marketking()->get_store_name_display($vendor_id);
					$store_link = marketking()->get_store_link($vendor_id);
					$stock_status = $offerproduct->get_stock_status();
					$stocktext = $badge = '';
					if ($stock_status === 'instock'){
					    $badge = 'badge-green';
					    $stocktext = esc_html__('In stock', 'marketking');
					} else if ($stock_status === 'outofstock'){
					    $badge = 'badge-gray';
					    $stocktext = esc_html__('Out of stock', 'marketking');
					} else if ($stock_status === 'onbackorder'){
					    $badge = 'badge-gray';
					    $stocktext = esc_html__('On backorder', 'marketking');
					}

					$rating = marketking()->get_vendor_rating($vendor_id, 1);

					?>
					<div class="marketking_product_other_offer_container <?php if($i > $offers_shown_default){echo 'marketking_offer_hidden_initial';} ?>">
						<?php
						// product image here
						$src = wp_get_attachment_url( $offerproduct->get_image_id() );
						if (empty($src)){
						    $src = wc_placeholder_img_src();
						}
						echo '<a href="'.esc_attr($offerproduct->get_permalink()).'"><img class="marketking_other_offer_product_image" src="'.esc_attr($src).'"></a>';
						?>
						<div class="marketking_product_other_offer_first_column">
							<div class="marketking_product_other_offer_first_column_sold_by">
								<?php echo esc_html__('Product sold by','marketking').' <a class="marketking_offer_store_link" href="'.esc_attr($store_link).'">'.esc_html($store_name); 
								// if there's any rating
								if (intval($rating['count'])!==0){
									echo '&nbsp;<strong>&nbsp;'.esc_html($rating['rating']).'</strong><span class="dashicons dashicons-star-filled marketking_product_other_offer_first_column_sold_by_star"></span>';
								}
								echo '</a>';
								?>

								<br>
								<div class="marketking_product_other_offer_first_column_sold_by_stock">
									<?php  echo '<span class="'.esc_attr($badge).'">'.esc_html($stocktext).'</span>';?>

								</div>
							</div>							
						</div>
						<div class="marketking_product_other_offer_second_third_container">

							<div class="marketking_product_other_offer_second_column">
								<?php echo $offerproduct->get_price_html(); ?>
							</div>
							<div class="marketking_product_other_offer_third_column">
								<?php 
								wc_get_template(
									'single-product/add-to-cart/external.php',
									array(
										'product_url' => $offerproduct->add_to_cart_url(),
										'button_text' => $offerproduct->add_to_cart_text(),
									)
								);
								?>
							</div>
						</div>
					</div>
					<?php					
				}

				if($i > $offers_shown_default){
					// show more items
					?>
					<div class="marketking_offers_show_more"><span class="dashicons dashicons-arrow-down"></span><?php esc_html_e('Show more...','marketking');?></div>
					<?php
				}
				?>
			</div>
			<br><br>

			<?php

			
		}
	}
}
Leave a Comment