add_to_cart_url() public method

Get the add to url used mainly in loops.
public add_to_cart_url ( ) : string
return string
/**
 * Get the add to cart URL for a product
 **/
function woocommerce_product_add_to_cart_url($atts)
{
    if (empty($atts)) {
        return;
    }
    global $wpdb;
    if ($atts['id']) {
        $product_data = get_post($atts['id']);
    } elseif ($atts['sku']) {
        $product_id = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $atts['sku']));
        $product_data = get_post($product_id);
    } else {
        return;
    }
    if ($product_data->post_type !== 'product') {
        return;
    }
    $_product = new WC_Product($product_data->ID);
    return esc_url($_product->add_to_cart_url());
}
Example #2
0
global $mdf_loop;
?>
<div class="clearfix"></div>
<?php 
while ($mdf_loop->have_posts()) {
    $mdf_loop->the_post();
    ?>
<div class="aff_offer_links">
<?php 
    $i = 1;
    $product = new WC_Product(get_the_ID());
    global $product;
    ?>
    
<?php 
    $woolink = $product->product_type == 'external' ? $product->add_to_cart_url() : get_post_permalink(get_the_ID());
    $term_ids = wp_get_post_terms(get_the_ID(), 'product_tag', array("fields" => "ids"));
    if (!empty($term_ids)) {
        $term_brand = $term_ids[0];
        $term_brand_image = get_option("taxonomy_term_{$term_ids['0']}");
    } else {
        $term_brand_image = '';
    }
    ?>
<div class="rehub_feat_block table_view_block"><a name="woo-link-list"></a>
    <?php 
    if ($product->is_on_sale()) {
        ?>
<div class="vip_corner"><span class="vip_badge sale_badge">Sale!</span></div><?php 
    }
    ?>
    function widget($args, $instance)
    {
        extract($args);
        extract($instance);
        global $product;
        $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base);
        ?>
            <section class="section section-product">
                <div class="container">
	                <div class="row category">
		                <div class="col-xs-3 category-name">
			                <img src="<?php 
        echo get_template_directory_uri();
        ?>
/img/<?php 
        echo !empty($icon_class) ? $icon_class : 'category-woman';
        ?>
.png" class="img-responsive">
			                <?php 
        if (!empty($title)) {
            echo '<span>' . $title . '</span>';
        }
        ?>
		                </div>
		                <div class="col-xs-9 type-list">
			                <?php 
        //echo $cat;
        $args_product_cat = array('type' => 'product', 'child_of' => 1, 'parent' => $cat, 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => 1, 'hierarchical' => 1, 'exclude' => '', 'include' => '', 'number' => '', 'taxonomy' => 'product_cat', 'pad_counts' => false);
        $product_list_cats = get_categories($args_product_cat);
        // print_r($product_list_cat);
        ?>
			                <?php 
        if (!empty($product_list_cats)) {
            ?>
				                <ul class="type-list-ul">
					                <?php 
            foreach ($product_list_cats as $i => $product_list_cat) {
                if ($i > 3) {
                    break;
                } else {
                    ?>
							                <li class="type-list-item"><a href="<?php 
                    echo get_term_link($product_list_cat, 'product_cat');
                    ?>
 " cat-term="<?php 
                    echo $product_list_cat->term_id;
                    ?>
"><?php 
                    echo $product_list_cat->name;
                    ?>
</a>
							                </li>
						                <?php 
                }
            }
            ?>
					                <?php 
            if (count($product_list_cats) > 4) {
                ?>
						                <li class="type-list-item more">
							                <button type="button" class="btn dropdown-toggle more-btn"
							                        data-toggle="dropdown"
							                        aria-haspopup="true" aria-expanded="false">
								                <span class="more-title">SẢN PHẨM KHÁC</span>
								                <span class="caret"></span>
								                <span class="sr-only">Toggle Dropdown</span>
							                </button>
							                <ul class="dropdown-menu">
								                <?php 
                foreach ($product_list_cats as $i => $product_list_cat) {
                    if ($i > 3) {
                        ?>
										                <li><a href="<?php 
                        echo get_term_link($product_list_cat, 'product_cat');
                        ?>
 " cat-term="<?php 
                        echo $product_list_cat->term_id;
                        ?>
"><?php 
                        echo $product_list_cat->name;
                        ?>
</a>
										                </li>
	                                            <?php 
                    }
                }
                ?>
							                </ul>
						                </li>
					                <?php 
            }
            ?>
				                </ul>
			                <?php 
        }
        ?>
		                </div>
	                </div>

                    <?php 
        $product_widget_cat = new WP_Query(array('post_type' => 'product', 'showposts' => 2, 'tax_query' => array(array('taxonomy' => 'product_cat', 'field' => 'term_id', 'terms' => $cat))));
        ?>
                        <?php 
        if ($product_widget_cat->have_posts()) {
            ?>
                        <div class="row sample">
	                        <?php 
            while ($product_widget_cat->have_posts()) {
                $product_widget_cat->the_post();
                ?>
	                        <div class="col-xs-6 sample-item">
		                        <div class="thumbnail">
			                        <a href="<?php 
                the_permalink();
                ?>
">
				                        <?php 
                product_percent_2();
                ?>
				                        <span class="sale-title">SALE</span>
				                        <span class="new-title">NEW</span>
				                        <?php 
                the_post_thumbnail(array(236, 330, 'bfi_thumb' => true), array('class' => 'img-responsive'));
                ?>
			                        </a>

			                        <div class="caption clearfix">
				                        <a href="<?php 
                the_permalink();
                ?>
"><span class="caption-title"><?php 
                hhs_brand();
                ?>
</span></a>
				                        <hr/>
				                        <?php 
                $product = new WC_Product(get_the_ID());
                ?>
				                        <p class="caption-description"><?php 
                echo get_the_title() . $product->get_sku();
                ?>
</p>

				                        <div class="caption-price">
					                        <?php 
                $price = get_post_meta(get_the_ID(), '_regular_price', true);
                $sale = get_post_meta(get_the_ID(), '_sale_price', true);
                ?>
					                        <div class="price-info">
						                        <?php 
                echo '<p class="old-price">' . number_format($price, 0, '.', '.') . ' VNĐ</p>';
                ?>
						                        <?php 
                echo '<p class="new-price">' . number_format($sale, 0, '.', '.') . ' VNĐ</p>';
                ?>
					                        </div>
					                        <div class="cart">
						                        <a href="<?php 
                echo esc_url($product->add_to_cart_url());
                ?>
"><img src="<?php 
                echo get_template_directory_uri();
                ?>
/img/product-cart.png"></a>
					                        </div>
				                        </div>
			                        </div>
		                        </div>
	                        </div>
	                        <?php 
            }
            wp_reset_postdata();
            ?>
                        </div>
                        <?php 
        }
        ?>
                    </div>
                    <!-- /.row row-no-padding -->
                </div>
            </section>
            <!-- /.section section-product -->
        <?php 
    }
Example #4
0
/**
 * Display a single prodcut price + cart button
 *
 * @access public
 * @param array $atts
 * @return string
 */
function etheme_product_add_to_cart($atts)
{
    if (empty($atts)) {
        return;
    }
    global $wpdb, $woocommerce;
    if (!isset($atts['style'])) {
        $atts['style'] = '';
    }
    if ($atts['id']) {
        $product_data = get_post($atts['id']);
    } elseif ($atts['sku']) {
        $product_id = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $atts['sku']));
        $product_data = get_post($product_id);
    } else {
        return;
    }
    if ($product_data->post_type == 'product') {
        $product = $woocommerce->setup_product_data($product_data);
        ob_start();
        ?>
		<p class="product-add-to-cart" style="<?php 
        echo $atts['style'];
        ?>
">

			<?php 
        echo $product->get_price_html();
        ?>

			<?php 
        woocommerce_template_loop_add_to_cart();
        ?>

		</p><?php 
        return ob_get_clean();
    } elseif ($product_data->post_type == 'product_variation') {
        $product = new WC_Product($product_data->post_parent);
        $GLOBALS['product'] = $product;
        $variation = new WC_Product_Variation($product_data->ID);
        ob_start();
        ?>
		<p class="product-add-to-cart product-variation" style="<?php 
        echo $atts['style'];
        ?>
">

			<?php 
        echo $product->get_price_html();
        ?>

			<?php 
        $link = $product->add_to_cart_url();
        $label = apply_filters('add_to_cart_text', __('Add to cart', ETHEME_DOMAIN));
        $link = add_query_arg('variation_id', $variation->variation_id, $link);
        foreach ($variation->variation_data as $key => $data) {
            if ($data) {
                $link = add_query_arg($key, $data, $link);
            }
        }
        printf('<a href="%s" rel="nofollow" data-product_id="%s" class="button add_to_cart_button product_type_%s">%s</a>', esc_url($link), $product->id, $product->product_type, $label);
        ?>

		</p><?php 
        return ob_get_clean();
    }
}
									<div class="caption-price">
										<?php 
        $price = get_post_meta(get_the_ID(), '_regular_price', true);
        $sale = get_post_meta(get_the_ID(), '_sale_price', true);
        ?>
										<div class="price-info">
											<?php 
        echo '<p class="old-price">' . number_format($price, 0, '.', '.') . ' VNĐ</p>';
        ?>
											<?php 
        echo '<p class="new-price">' . number_format($sale, 0, '.', '.') . ' VNĐ</p>';
        ?>
										</div>
										<div class="cart">
											<a href="<?php 
        echo esc_url($product->add_to_cart_url());
        ?>
"><img src="<?php 
        echo get_template_directory_uri();
        ?>
/img/product-cart.png"></a>
										</div>
									</div>
								</div>
							</div>

						<?php 
    }
    // end of the loop.
    ?>
Example #6
0
							</a>
						</th>
					</tr>
					<?php 
while ($products->have_posts()) {
    ?>
						<tr>
							<?php 
    $products->the_post();
    ?>
							<?php 
    $p = new WC_Product(get_the_ID());
    ?>
							<td><div>
								<a href="<?php 
    echo $p->add_to_cart_url();
    ?>
">
									<?php 
    echo $p->get_image(array(30, 30)) . ' ';
    the_title();
    ?>
								</a>
							</div></td>
							<td><div><?php 
    echo $p->get_price_html();
    ?>
</div></td>
							<td class="add"><a data-shop="<?php 
    the_ID();
    ?>