Ejemplo n.º 1
0
/**
 * Render fields
 *
 * @since 1.0
*/
function edd_csau_render_fields()
{
    $upsell_heading = get_post_meta(get_the_ID(), '_edd_csau_upsell_heading', true);
    $cross_sell_heading = get_post_meta(get_the_ID(), '_edd_csau_cross_sell_heading', true);
    $products = get_posts(array('post_type' => 'download', 'nopaging' => true, 'orderby' => 'title', 'order' => 'ASC', 'exclude' => get_the_ID()));
    ?>
	<p><strong><label for="edd_csau_upsell_heading"><?php 
    _e('Upsell Heading', 'edd-csau');
    ?>
</label></strong></p>

	<p><input type="text" class="widefat" name="edd_csau_upsell_heading" id="edd_csau_upsell_heading" value="<?php 
    echo esc_attr($upsell_heading);
    ?>
" size="30" /></p>
	<p><?php 
    printf(__('Shown with upsell %s on the single %s page. If not set, the default heading in the plugin\'s settings will be used instead', 'edd-csau'), strtolower(edd_get_label_plural()), strtolower(edd_get_label_singular()));
    ?>
</p>

	<p><strong><label for="edd_csau_upsell_products"><?php 
    printf(__('Upsell %s', 'edd-csau'), edd_get_label_plural());
    ?>
</label></strong></p>

	<select name="edd_csau_upsell_products[]" id="edd_csau_upsell_products" data-placeholder="<?php 
    printf(__('Select %s', 'edd-csau'), edd_get_label_plural());
    ?>
" multiple class="edd-csau-select">
		<?php 
    if ($products) {
        // get upsell product IDs from DB
        $upsell_products = edd_csau_get_products(get_the_ID(), 'upsell');
        foreach ($products as $product) {
            $selected = in_array($product->ID, $upsell_products) ? $product->ID : '';
            ?>
		<option value="<?php 
            echo absint($product->ID);
            ?>
" <?php 
            echo selected($selected, $product->ID, false);
            ?>
><?php 
            echo esc_html(get_the_title($product->ID));
            ?>
</option>

		<?php 
        }
        ?>

		<?php 
    } else {
        ?>

			<option><?php 
        printf(__('No %s found', 'edd-csau'), edd_get_label_plural());
        ?>
</option>

		<?php 
    }
    ?>

	</select>
	<p><?php 
    printf(__('Select the %s to upsell to the customer on the single %s page', 'edd-csau'), strtolower(edd_get_label_plural()), strtolower(edd_get_label_singular()));
    ?>
</p>


	<p><strong><label for="edd_csau_cross_sell_heading"><?php 
    _e('Cross-sell Heading', 'edd-csau');
    ?>
</label></strong></p>
	
	<p><input type="text" class="widefat" name="edd_csau_cross_sell_heading" id="edd_csau_cross_sell_heading" value="<?php 
    echo esc_attr($cross_sell_heading);
    ?>
" size="30" /></p>
	<p><?php 
    printf(__('Shown with cross-sells at checkout, but only if the selected cross-sells below are shown on their own. If not set, or cross-sells from other %s are shown together, the default heading in the plugin\'s settings will be used instead', 'edd-csau'), strtolower(edd_get_label_plural()));
    ?>
</p>

	<p><strong><label for="edd_csau_cross_sell_products"><?php 
    printf(__('Cross-sell %s', 'edd-csau'), edd_get_label_plural());
    ?>
</label></strong></p>

	<select name="edd_csau_cross_sell_products[]" id="edd_csau_cross_sell_products" data-placeholder="<?php 
    printf(__('Select %s', 'edd-csau'), edd_get_label_plural());
    ?>
" multiple class="edd-csau-select">
		<?php 
    if ($products) {
        // get cross-sell product IDs from DB
        $cross_sell_products = edd_csau_get_products(get_the_ID(), 'cross_sell');
        foreach ($products as $product) {
            $selected = in_array($product->ID, $cross_sell_products) ? $product->ID : '';
            ?>
		<option value="<?php 
            echo absint($product->ID);
            ?>
" <?php 
            echo selected($selected, $product->ID, false);
            ?>
><?php 
            echo esc_html(get_the_title($product->ID));
            ?>
</option>

		<?php 
        }
        ?>

		<?php 
    } else {
        ?>

			<option><?php 
        printf(__('No %s found', 'edd-csau'), edd_get_label_plural());
        ?>
</option>

		<?php 
    }
    ?>

	</select>
	<p><?php 
    printf(__('Select the %s to be shown to the customer at checkout when this %s is added to the cart', 'edd-csau'), strtolower(edd_get_label_plural()), strtolower(edd_get_label_singular()));
    ?>
</p>

<?php 
    wp_nonce_field('edd_csau_nonce', 'edd_csau_nonce');
    ?>

<?php 
}
Ejemplo n.º 2
0
/**
 * Display Cross-sell/Upsell products
 *
 * @since 1.0
*/
function edd_csau_html($columns = '3')
{
    global $post, $edd_options;
    // upsell products for the single download page
    if (is_singular('download')) {
        $products = edd_csau_get_products(get_the_ID(), 'upsell');
    } elseif (edd_is_checkout()) {
        // get contents on the cart
        $cart_items = edd_get_cart_contents();
        // return if there's nothing in the cart
        if (!$cart_items) {
            return;
        }
        $cart = array();
        // create new products array with the cart items cross sell products
        if ($cart_items) {
            foreach ($cart_items as $cart_item) {
                $download_id = $cart_item['id'];
                // create $cart array with IDs
                $cart[] = (int) $cart_item['id'];
                // create $product_list array with cross sell products
                $product_list[] = get_post_meta($download_id, '_edd_csau_cross_sell_products', false);
            }
        }
        $products = $product_list;
        // clean the array
        $products = array_filter($products);
        // return if no cross sell products after clean
        if (!$products) {
            return;
        }
        // merge into single level array
        $products = call_user_func_array('array_merge', $products);
        // remove duplicate IDs
        $products = array_unique($products);
    } else {
        return;
    }
    if ($products) {
        ?>
		
		<?php 
        if (edd_is_checkout()) {
            $posts_per_page = isset($edd_options['edd_csau_cross_sell_number']) && !empty($edd_options['edd_csau_cross_sell_number']) ? $edd_options['edd_csau_cross_sell_number'] : '3';
        } elseif (is_singular('download')) {
            $posts_per_page = isset($edd_options['edd_csau_upsell_number']) && !empty($edd_options['edd_csau_upsell_number']) ? $edd_options['edd_csau_upsell_number'] : '3';
        }
        $query = array('post_type' => 'download', 'posts_per_page' => $posts_per_page, 'orderby' => 'date', 'order' => 'DESC', 'post__in' => $products);
        $query = apply_filters('edd_csau_query', $query);
        $downloads = new WP_Query($query);
        if ($downloads->have_posts()) {
            // upsell heading
            if (is_singular('download')) {
                $upsell_heading = get_post_meta(get_the_ID(), '_edd_csau_upsell_heading', true);
                // show singular heading
                if ($upsell_heading) {
                    $heading = esc_attr($upsell_heading);
                } elseif (isset($edd_options['edd_csau_upsell_heading'])) {
                    $heading = esc_attr($edd_options['edd_csau_upsell_heading']);
                } else {
                    $heading = __('You may also like', 'edd-csau');
                }
            } elseif (edd_is_checkout()) {
                $ids = edd_csau_get_cart_trigger_ids();
                if (count($ids) == 1) {
                    $heading = esc_attr(get_post_meta($ids[0], '_edd_csau_cross_sell_heading', true));
                } elseif (isset($edd_options['edd_csau_cross_sell_heading'])) {
                    $heading = esc_attr($edd_options['edd_csau_cross_sell_heading']);
                } else {
                    $heading = __('You may also like', 'edd-csau');
                }
            }
            // end is_checkout
            $i = 1;
            global $wp_query;
            //$download_count = $downloads->found_posts > 3 ? 3 : $downloads->found_posts;
            $classes = array();
            $classes = apply_filters('edd_csau_classes', $classes);
            // default classes
            $classes[] = 'edd-csau-products';
            // columns
            if ($columns) {
                $classes[] = 'col-' . $columns;
            }
            // filter array and remove empty values
            $classes = array_filter($classes);
            $classes = !empty($classes) ? implode(' ', $classes) : '';
            $class_list = !empty($classes) ? 'class="' . $classes . '"' : '';
            ob_start();
            ?>
 
			<div <?php 
            echo $class_list;
            ?>
>

			<h2><?php 
            echo esc_attr($heading);
            ?>
</h2>

				<?php 
            while ($downloads->have_posts()) {
                $downloads->the_post();
                ?>
					<div itemscope itemtype="http://schema.org/Product" class="<?php 
                echo apply_filters('edd_download_class', 'edd_download', '', '');
                ?>
" id="edd_download_<?php 
                echo get_the_ID();
                ?>
">
						<div class="edd_download_inner">
						
							<?php 
                do_action('edd_csau_download_before');
                $show_excerpt = apply_filters('edd_csau_show_excerpt', true);
                $show_price = apply_filters('edd_csau_show_price', true);
                $show_button = apply_filters('edd_csau_upsell_show_button', true);
                edd_get_template_part('shortcode', 'content-image');
                edd_get_template_part('shortcode', 'content-title');
                if ($show_price) {
                    edd_get_template_part('shortcode', 'content-price');
                }
                if ($show_excerpt) {
                    edd_get_template_part('shortcode', 'content-excerpt');
                }
                // if the download is not in the cart, show the add to cart button
                if (edd_is_checkout()) {
                    if (!edd_item_in_cart(get_the_ID())) {
                        $text = apply_filters('edd_csau_cross_sell_add_to_cart_text', __('Add to cart', 'edd-csau'));
                        $price = apply_filters('edd_csau_cross_sell_show_button_price', false);
                        if ($show_button) {
                            ?>
 
									
									<div class="edd_download_buy_button">
										<?php 
                            echo edd_get_purchase_link(array('download_id' => get_the_ID(), 'text' => $text, 'price' => $price));
                            ?>
									</div>
									<?php 
                        }
                        ?>

								<?php 
                    } else {
                        echo apply_filters('edd_csau_added_to_cart_text', '<span class="edd-cart-added-alert"><i class="edd-icon-ok"></i> ' . __('Added to cart', 'edd-csau') . '</span>');
                    }
                } else {
                    $text = apply_filters('edd_csau_upsell_add_to_cart_text', __('Add to cart', 'edd-csau'));
                    $price = apply_filters('edd_csau_upsell_show_button_price', false);
                    $show_button = apply_filters('edd_csau_upsell_show_button', true);
                    if ($show_button) {
                        ?>
								<div class="edd_download_buy_button">
									<?php 
                        echo edd_get_purchase_link(array('download_id' => get_the_ID(), 'text' => $text, 'price' => $price));
                        ?>
								</div>
								<?php 
                    }
                    ?>
							<?php 
                }
                do_action('edd_csau_download_after');
                ?>
						</div>
					</div>
					<?php 
                if ($columns && $i % $columns == 0) {
                    ?>
<div style="clear:both;"></div><?php 
                }
                ?>
				<?php 
                $i++;
            }
            ?>

				<?php 
            wp_reset_postdata();
            ?>
			</div>
			<?php 
            $html = ob_get_clean();
            return apply_filters('edd_csau_html', $html, $downloads, $heading, $columns, $class_list);
        }
        ?>

		<?php 
    }
    ?>

<?php 
}