WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
Generate link to product id (9 posts)

  1. jjbbrr
    Member
    Posted 1 year ago #

    I am running two shops in parallel since we sell products in two currencies and each currency wants to set their prices independently.

    All customers will visit a product page of the USD shop, but there will also be an 'add to cart' button that adds the same product to the GBP cart.

    I'd like to know if there is a way to automatically generate an 'add to cart' button that links to the same product id but with a subdomain for the url instead of the domain the shop is on. E.g. the shop is on example.com but the GBP button would link to an identical shop on shop.example.com

    I could manually create these buttons on every product page but if there is a way for this to happen automatically I'd prefer that.

    http://wordpress.org/extend/plugins/woocommerce/

  2. helgatheviking
    Member
    Posted 1 year ago #

    bear in mind that i haven't test this.... just thinking a little stream of consciousness. you can add a second add to cart button I think by duplicating the existing button and adding it to the woocommerce_after_add_to_cart_form hook. then maybe do a string replace on the URL. this is a duplicate of the add to cart form for a "simple" product type.

    totally untested, but i think it is in the right direction based on what you asked. good luck.

    function kia_add_duplicate_button(){ 
    
    	<?php
    
    		/* edit these two variables*/
    		$domain = "example.com";
    		$subdomain = "sub.example.com";
    
    		$cart_url = str_replace( $domain, $subdomain, $product->add_to_cart_url() );
    	?>
    
    	<form action="<?php echo esc_url( $cart_url ); ?>" class="cart" method="post" enctype='multipart/form-data'>
    
    	 	<?php do_action('woocommerce_before_add_to_cart_button'); ?>
    
    	 	<?php
    	 		if ( ! $product->is_sold_individually() )
    	 			woocommerce_quantity_input( array( 'min_value' => 1, 'max_value' => $product->backorders_allowed() ? '' : $product->get_stock_quantity() ) );
    	 	?>
    
    	 	<button type="submit" class="single_add_to_cart_button button alt"><?php echo apply_filters('single_add_to_cart_text', __( 'Add to cart', 'woocommerce' ), $product->product_type); ?></button>
    
    	 	<?php do_action('woocommerce_after_add_to_cart_button'); ?>
    
    	</form>
    <?php }
    add_action('woocommerce_after_add_to_cart_form','kia_add_duplicate_button');
  3. jjbbrr
    Member
    Posted 1 year ago #

    Thanks very much for taking the time to look at this. I've tried the above code but get this error:

    Fatal error: Call to a member function add_to_cart_url() on a non-object in C:\xampp\htdocs\wordpress\wp-content\themes\childtheme\functions.php on line 50

    Line 50 is:
    $cart_url = str_replace( $domain, $subdomain, $product->add_to_cart_url() );

    I'm afraid that resolving this is beyond me so your help would be greatly appreciated!

  4. helgatheviking
    Member
    Posted 1 year ago #

    hmmm.... forgot to globally declare $product. I still don't know if it will work and can't really debug it. hope it helps.

    function kia_add_duplicate_button(){ 
    
     global $product;
    
    	<?php
    
    		/* edit these two variables*/
    		$domain = "example.com";
    		$subdomain = "sub.example.com";
    
    		$cart_url = str_replace( $domain, $subdomain, $product->add_to_cart_url() );
    	?>
    
    	<form action="<?php echo esc_url( $cart_url ); ?>" class="cart" method="post" enctype='multipart/form-data'>
    
    	 	<?php do_action('woocommerce_before_add_to_cart_button'); ?>
    
    	 	<?php
    	 		if ( ! $product->is_sold_individually() )
    	 			woocommerce_quantity_input( array( 'min_value' => 1, 'max_value' => $product->backorders_allowed() ? '' : $product->get_stock_quantity() ) );
    	 	?>
    
    	 	<button type="submit" class="single_add_to_cart_button button alt"><?php echo apply_filters('single_add_to_cart_text', __( 'Add to cart', 'woocommerce' ), $product->product_type); ?></button>
    
    	 	<?php do_action('woocommerce_after_add_to_cart_button'); ?>
    
    	</form>
    <?php }
    add_action('woocommerce_after_add_to_cart_form','kia_add_duplicate_button');
  5. jjbbrr
    Member
    Posted 1 year ago #

    Thanks, that got the code working and it displays the two buttons but they both go to the default cart. (I've been testing it here: http://webandtechstuff.com/shop/test-product/)

    The approach I initially tried was to insert an extra button using this:

    add_action('woocommerce_simple_add_to_cart', 'JRinsert_extra_addtocart_button', 31);
    
    function JRinsert_extra_addtocart_button() {
    	echo '<div id="extra-addtocart-button">
    		<form class="cart" method="link" action="http://subdomain.example.com">
    		<button class="single_add_to_cart_button button alt" type="submit" >Add to cart</button>
    		</div>';

    This sets up the button as a link to the subdomain. I was then hoping to be able to append the product url at the end of the domain using some kind of variation on either the Woocommerce shortcode:

    [add_to_cart_url id="41"]

    or by writing out the full link itself and somehow generating the product id of the current product:
    http://webandtechstuff.com/?add-to-cart=41

    This last one would work if there was a way to generate the product id of the current product somehow?

    That's as far as I got before I had torn all my hair out so if I can't find another solution I'm probably going to have to hard code the buttons in one by one as my top example above by pasting the html into the 'product short description'. It's a real hack but it does work...

    So any further input you can add here would be great!

  6. helgatheviking
    Member
    Posted 1 year ago #

    apparently the add_to_cart() method doesn't include the full URL? we could filter that to use absolute URLs. then what i have might work.

    function kia_filter_cart_url( $url ) {
         return add_query_arg( 'add-to-cart', $this->id, get_permalink( $this->id ) );
    }
    add_filter('woocommerce_add_to_cart_url','kia_filter_cart_url');

    gnite!

  7. jjbbrr
    Member
    Posted 1 year ago #

    Thanks, no you're right it doesn't. I tried adding the above code but when the page loads it only loads some parts and is missing the cart buttons, sidebar, reviews and footer, and looking at the source of the page it's really short with a lot missing so it's not quite there yet. Any suggestions you may have for what else needs to be done would be warmly received!

  8. helgatheviking
    Member
    Posted 1 year ago #

    that assuredly means there is a PHP parse error somewhere.

  9. jjbbrr
    Member
    Posted 1 year ago #

    I think this one is for the too hard basket! Going to try and find an alternative approach. Thanks for your help.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic