WooCoommerce after page loads removes my modifications on mini-cart - wordpress

How do I modify something on the minicart without WooCommerce removing it? When page is still loading I can see my text on minicart, but after a second, when the page complete loads, it gets removed.
This is the code that I use to display the minicart in the header. Everything that comes with this function will reset after a while
<div class="widget_shopping_cart_content">
<?php woocommerce_mini_cart(); ?>
</div>
I am trying to append a few things for each item product but I would also need to do it for subtotal and maybe some other things. If I try to use this filter (which by itself shows single product price and quantity in the cart) then my appended HTML shows for a split of second and then disappears.
add_filter('woocommerce_widget_cart_item_quantity', 'ms_woocommerce_widget_cart_item_quantity', 999, 3);
function ms_woocommerce_widget_cart_item_quantity($html_price, $cart_item, $cart_item_key)
{
return $html_price . ' <div>Something</div>';
}
If I add some HTML code in the template cart/mini-cart.php, same thing happens. It will just reset itself to original state after page loads. Same thing happens if I modify directly that template directly in the WooCommerce plugin folder for testing it out, so I don't know where does it pull original HTML from?
I understand that WooCommerce updates fragments but shouldn't it update minicart with my modifications included? Either from mini-cart.php template or with above filter.

Related

hide shortcode when one is empty

I created a shortcode to be personalised directly in product page. I was wondering if there was a way to not show the shortcode if one of them is empty ? I saw it's not recommended to work directly and check if shortcode is empty but i don't find the solution anywhere.
What i need to check (if these shortcode is empty) : [tech_img1] [tech_img2] [tech_img3] [tech_text1][tech_text2] and [tech_img3]
For example : For each product, i can enter an url img saved in this shortcode [tech_img] and text saved in this shortcode [tech_text]. However if one of the shortcode is empty (no data in it), the function custom_tech shoudn't be display in front
I used this tutorial to add and save custom field in a product page : https://remicorson.com/mastering-woocommerce-products-custom-fields
I made a custom shortcode to fuse all the shortcode i need to display
function custom_tech() {
echo do_shortcode('<div class="flextech"><img class="picfit" src="[tech_img1]" alt="tech_image1"><p class="tech-block">[tech_text1]</p><img class="picfit" src="[tech_img2]" alt="tech_image1"><p class="tech-block">[tech_text2]</p><img class="picfit" src="[tech_img3]" alt="tech_image3"><p class="tech-block">[tech_text3]</p></div>'); } add_shortcode( 'my_custom_tech', 'custom_tech');

Woocommerce product archive displays nothing when called from custom Page

I have a woocommerce shop page which shows me all of the products perfectly. This page is also set as the "Shop" page in the settings. Now I want a second page called Home, which should use the same template as the shop page. (basically a second shop page without categories and some news)
home-template.php
<?php /* Template Name: Homepage */
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
wc_get_template( 'archive-product.php' );
?>
I'm using the standard archive-product template from woocommerce's github.
I can see the template being loaded, like the header, but certain woocommerce functions don't return the expected value. woocommerce_before_shop_loop doesn't return anything, even though it should display the orderby dropdown. woocommerce_product_loop() returns true, but wc_get_loop_prop('total') returns zero.
I've also tried renaming the file to front-page.php but that didn't help. The home page is set correctly in the reading settings.
Am i missing a call to a query, or is it something else? Thank you in advance for your help!
When WooCommerce creates the builtin shop page it saves the id of the created page in the wp_options database table with option_name 'woocommerce_shop_page_id'. When WordPress loads a page WooCommerce checks if the page id of the page being loaded is equal to the option_value of option_name 'woocommerce_shop_page_id' and then executes code to generate the HTML of the shop page. If you have looked at the shop page in the page editor you will notice that the content is empty. The magic is being done with hard coded routines that execute for this special id.
Since, your custom page has a different page id none of the custom code for generating the shop page will be execute. So, you need to execute this magic code for your custom page. It can be done but you need a good understanding of WooCommerce.
I suggest you reconsider your design and instead of a new page just customize the existing shop page using actions and filters.

how to display single product do_action in shop Page in Woocommerce

Is there any method available to display single product woocommerce_before_single_product_summary action woocommerce shop page
Yes. There are a couple ways to do this, depending on what you're actually trying to achieve:
You can override the WooCommerce shop page template (archive-product.php) in your theme (or hopefully child theme) to do the woocommerce_before_single_product_summary action
You can add the functionality you're looking for that's currently hooked into the woocommerce_before_single_product_summary action to an action that fires on the Shop page
You can create a function that will do the woocommerce_before_single_product_summary action, and hook that into an action on the Shop page (similar to option 1, but doesn't override the Shop template - in theory, preventing more work in future if/when WooCommerce updates their templates)
How to do it
Create a folder in your theme/child theme called woocommerce and copy plugins/woocommerce/templates/content-product.php to it and figure out where you want to put the woocommerce_before_single_product_summary action. Making sure you're within the <?php ?> tags, pop this is where you want: do_action( 'woocommerce_before_single_product_summary' );
Find the specific function that does what you're looking for, and add it to an action that's already firing on the Shop page. In the functions.php file of your theme/child theme, (please use a child theme), add this within the <?php ?> tags:
add_action( 'woocommerce_before_shop_loop_item', 'specific_function_you_want' );
where 'woocommerce_before_shop_loop_item' is the first action that fires on the Shop page (you can swap out for another action in content-product.php depending on where you want to put the functionality, and 'specific_function_you_want' is the name of the function currently hooked into the woocommerce_before_single_product_summary action that you're looking to replicate.
Again, within the <?php ?> tags in your functions.php file add the following:
add_action( 'woocommerce_before_shop_loop_item', 'do_before_product_summary');
function do_before_product_summary() {
do_action('woocommerce_before_single_product_summary');
}
Pros/Cons
Easiest and fastest way to do what the question asks; however: note that by default, woocommerce_before_single_product_summary only does two things: display the product image & say if the item is on sale. Both of these are already being shown on the Shop page. Since this method just does the woocommerce_before_single_product_summary action, you'll most likely have duplicated content on the Shop page. Probably not the best way to go about this.
Gives you the most granularity. I'm making the assumption that you're probably looking to get some specific functionality that is currently hooked to the woocommerce_before_single_product_summary action, and do that on the Shop page. If that is the case, this method will allow you do that, without having to worry about any other functionality that's attached to the woocommerce_before_single_product_summary action (like duplicated product image as mentioned in method 1). However, you will need to find out where that specific functionality is coming from, which will require a bit of digging.
Doesn't require the digging required in method 2, but as with method 1, will do everything that's hooked into the woocommerce_before_single_product_summary action. The big benefit to using this over method 1 is that when/if WooCommerce updates their templates in the future (as they do from time to time), you won't need to worry about keeping your template override up to date with theirs. This isn't always a big deal, but it is something to keep in mind.

Load a page through ajax with Wordpress using different template

After the user clicks a link, I want to load the corresponding page within the currentpage. This ajaxpage should be themed differently than it's non-ajax page.
The pages would be added by the client in different languages. So there is no fixed id's and the client shouldnt add the same article twice (1 for normal and 1 for ajax). This rules manually overwriting templates out.
The first part, ajax loading of content is easy to do, but I don't know how to display content in different ways.
So
domain.com/product/car direct visit shows a product (car) using single-product.php as template which includes a header and footer.
ajax loading domain.com/product/car would show a product (car) but without a header and footer and perhaps some small layout changes.
My initial thought would be setting some template dynamicly (template-product-ajax.php) but Im not sure if this is possible or how.
In short, how can I create a different template/layout/view when loading a page with ajax in wordpress
You would have to pass in a series of variables, including the page/post ID you want to load, what kind of object you're trying to load (either 'page' or 'post') and a boolean to ensure that the standard get_header() function isn't loaded (as you only want the content of the page, not the headers).
Somewhere within the template, you can use that boolean to determine the template's behavior. For example:
<?php
/*Template Name: AJAX Content*/
if(!isset($_POST['bool']))
get_header();
?>
<div id="body">
<div class="page_content">
<?php
if(isset($_POST['bool']))
{
$object = $_POST['ob_type'] == 'page' ? get_page($_POST['PID']) : get_post($_POST['PID']);
echo $object->post_content;
/*echo '<pre>';var_dump($object);echo '</pre>';//Uncomment this section if you want to know what you can access with variable $object*/
{
else
{
//STANDARD LOOP
}
?>
</div>
</div>
This is untested, but it should help get you started if you're already familiar with AJAX calls and sending POST data to a separate page.

Can WordPress post content be external data (not from the database)

I am trying to display a page based on some data returned from an external API (Amazon). This data is formatted then, has to be displayed on a page, created on the fly, based on URL querys. I can already do this with shortcodes but this has to be from the query.
I see all kinds of info in the codex on returning custom query_posts into the loop from the database. However, I cannot find info getting external data to appear on a page.
Is this possible in WordPress? (anything is possible, right?) Just point me to some functions, filters or tutorials please.
If I understand you correctly, you want to retrieve data dynamically and display it in a WordPress page?
There are many ways to do this, but here's one option:
Create a Page Template
Create a WordPress page and use the Page Template created in step 1.
Edit the Page Template to call the external API and display the data
I'm guessing you've been trying to find a way to do this from the "content" of a page or post, but the easiest way is to put the code in a custom Page Template.
UPDATE: If you want to programmatically create a page, this might work for you: http://wordpress.org/support/topic/how-to-create-pages-programmatically?replies=5#post-1230619
http://www.prelovac.com/vladimir/wordpress-shortcode-snippet-to-display-external-files
This is a snippet to display external data in a post.
Would this help? If the dynamic page was a HTML page and THEM displayed in WP.
Yep, Thats possible.
I Did by using bridges.
You can do it by add "add_meta_boxes" action.
Inside the meta box function you can add call and get the external page contents, or can give your own forms .etc
My Code :
/*
* Add Meta Product Type Field to POST
*/
add_action('add_meta_boxes', 'meta_box_product_type_add');
My Meta Box
/*
* Product Type Meta Box Init
*/
function meta_box_product_type_add()
{
add_meta_box('ptype_testing', 'Product Type', 'add_ptype', 'testing', 'normal', 'high');
}
/*
* Product Type Field
*/
function add_ptype()
{ ?>
<label>Type of Product : </label>
<select name="ptype" id="ptypes">
<option>----Select----</option>
<option>Physical</option>
<option>Virtual</option>
</select>
<label>Unit :</label>
<select name="punit" id="units">
<option>----Select----</option>
<option>KG</option>
<option>Mtr</option>
<option>Ltr</option>
</select>
<?php
}
Try It....

Resources