Add an elseif statement for wordpress page using woocommerce - php

I've tried several different things, but with no good results. I'm attempting to add an elseif which will allow me to echo "Enquire for Price" instead of a price if left blank on a wordpress.org site that uses Woocommerce. Instead, the price is being shown as 0 or EnquireforPrice£0. Our custom theme doesn't have a price.html or loop.html file, so I can't edit those. The price.html file in single-product (woocommerce folder) shows EnquireforPrice£(whatever the original price was) when I tried to edit it with the elseif I found on this wordpress woocommerce support thread
This is my current code that doesn't allow for enquire for price and for some reason I'm drawing a blank on how to get it to work.
<?php echo ($product->get_stock_quantity() < 1) ? '<span class="sold">Sold</span>' : null; ?><?php if ($product->get_stock_quantity() != 0) : ?><p class="price"><?php echo woocommerce_price(get_post_meta(get_the_ID(), '_price', true)); ?></p><?php endif; ?>
Basically, I need it to say Enquire for price if the price is left blank in the individual product price, but if there is a price to list the price. It should be straight forward, yet for some reason it simply isn't.
Thanks for any help that can be offered!
PS I've also tried the wordpress plugins that are supposed to do this, but they only work on the product page and leave £0 on the home and search pages.

I have downloaded woocommerce and have it working for me. Open
plugins/woocommerce/templates/single-product/price.php
Find this line
<p itemprop="price" class="price"><?php echo $product->get_price_html(); ?></p>
Replace it with the code below
<p itemprop="price" class="price">
<?php
$price = $product->get_price_html();
if(empty($price))
{
echo 'Anchor Text';
}
else
{
echo $price;
};
?>
</p>

Related

Change Out of Stock product overlay type on shop in Woocommerce

I'm trying to change the Out of Stock notice which overlays the product images in my shop page in Woocommerce. I want it to say 'New stock coming soon' as I have done on the product page itself.
I have now discovered where to change it - in Flatsome theme: content_product.php and changed it as below:
<div class="image-tools <?php echo flatsome_product_box_actions_class(); ?>">
<?php do_action( 'flatsome_product_box_actions' ); ?>
</div>
<?php if ( $out_of_stock ) { ?><div class="out-of-stock-label"><?php _e( 'New stock coming soon', 'woocommerce' ); ?></div><?php } ?>
</div>
and it works.
But this is in the parent theme. How do I change to an add_filter script so I can add to my child functions.php?
I solved it by translating the string and following this: https://businessbloomer.com/translate-single-string-woocommerce-wordpress/
I changed 'Sale!' to 'Out of Stock' (which is what originally appeared on my page) and changed 'On Offer' to 'New stock coming soon'.
Now the badge on my product images shows New stock coming soon instead of Out of Stock.

Move the variation description to the Woocommerce product variable description

What I am trying: Show the Variation Description of the Child of a Variable Product on single product page in the product tab description area.
In WooCommerce and variable products the parent has the description which is stored in wp_posts->post_content and every child has an additional _variation_description field. On the frontent the description of the parent is always visible and the _variation_description of the Child is showing as a variation is selected. It gets updated with js and is displayed before the add to cart button together with the price of the variation.
The description is called in description.php with the code
the_content();
which outputs the parent products post_content db field.
The variation description is called in variation.php with the code
<script type="text/template" id="tmpl-variation-template">
<div class="woocommerce-variation-description">{{{ data.variation.variation_description }}}</div>
</script>
As I understand this works with wp.template and I also checked the info on Javascript Reference wp.template
The js is in add-to-cart-variation.js and the template is built with
template = wp.template( 'variation-template' );
Then the data can be updated which I do not really understand in detail. What I understand is that all the js calls are in the form,
form.$singleVariation.html( $template_html );
so then I realized why I couldn't just copy the code from variation.php and paste it to description.php because there it would be outside the form tag.
On the other hand the data for SKU is not called from within the form, it is called through .product_meta so if I put a div with class="product_meta" I can call the sku and have it updated with js from anywhere. For example I can put this code into description.php
<div class="product_meta">
<?php if ( wc_product_sku_enabled() && ( $product->get_sku() || $product->is_type( 'variable' ) ) ) : ?>
<?php if ( $sku = $product->get_sku() ) : ?>
<span class="sku_wrapper"><?php esc_html_e( 'SKU:', 'woocommerce' ); ?>
<span class="sku" itemprop="sku"><?php echo $sku; ?></span></span>
<?php endif; ?>
<?php endif; ?>
</div>
and it works fine.
So here I am stuck. I just want to have the variation description displayed dynamically in the description.php and I thought if I understand how the wp.template works in add-to-cart-variation.js I could figure it out. But I can't. I do not exactly understand how the content is being updated in the variation form and what the difference is to the data in SKU.
Maybe there is a totally simple and easy solution for what I want, and maybe my approach is too complicated. I would be very glad for any Ideas or hints or just pointing in the right direction.
This can be done with some jQuery code, where we will copy the variation description to the variable product description tab. But first you need to make some very small changes on some templates.
Here is the official docs related to: Template structure & Overriding templates via a theme
The templates changes and the necessary code:
On single-product/tabs/description.php template file, you will replace the line:
<?php the_content(); ?>
by the following line (We just add a surrounding html <div> tag with a selector class):
<div class="product-post-content"><?php the_content(); ?></div>
On single-product/add-to-cart/variation.php template file, you will replace the line:
<div class="woocommerce-variation-description">{{{ data.variation.variation_description }}}</div>
by the following line (we just hide the variation description):
<div class="woocommerce-variation-description" style="display:none;">{{{ data.variation.variation_description }}}</div>
Now the jQuery (commented) code (on single product pages for variable products only):
add_action( 'wp_footer', 'move_variation_description' );
function move_variation_description(){
global $product;
// Only on single product pages for variable products
if ( ! ( is_product() && $product->is_type('variable') ) ) return;
// jQuery code
?>
<script type="text/javascript">
jQuery(function($){
a = '.woocommerce-variation-description', b = a+' p', c = 'input.variation_id',
d = '#tab-description .product-post-content', de = $(d).html();
// On load, adding a mandatory very small delay
setTimeout(function(){
// variation ID selected by default
if( '' != $(c).val() && $(a).text() != '' )
$(d).html($(a).html());
}, 300);
// On live event (attribute select fields change)
$('table.variations select').on( 'blur', function(){
// variation ID is selected
if( '' != $(c).val() && $(a).text() != '' ){
$(d).html($(a).html()); // We copy the variation description
}
// No variation ID selected
else {
$(d).html($(a).html()); // We set back the variable product description
}
console.log($('input.variation_id').val()); // TEST: Selected variation ID … To be removed
});
});
</script>
<?php
}
This code goes on function.php file of your active child theme (or active theme).
Tested and works.
Other related threads:
Replace the Variable Price range by the chosen variation price in WooCommerce 3
Get the selected product variation ID in WooCommerce variable products single pages
Variable product selectors: Getting the live selected values

OpenCart show weight in product block

I am currently working on an ecommerce website using opencart, trying to show the weight of each product, in product details template it works, however, when I do the same thing to the product block, it shows undefined variable. This is the site KCityMart
So what I did to show the weight in product details page is this
in file catalog/controller/product/product.php I added this
$data['weight'] = $this->weight->format($product_info['weight'],$product_info['weight_class_id']);
and in /catalog/view/theme/theme/template/product/product.tpl I added this
<ul class="volume">
<?php if ($weight > 0) { ?>
<li><?php echo $weight; ?></li>
<?php } ?>
</ul>
and it works. But, when I tried to add this to /catalog/view/theme/theme/template/common/product/default.tpl it shows "undefined variable" error.
What could be the problem? Which part did I miss?
Any input from you all will be very much appreciated. Thanks!
What is default.tpl file ? If it's new file then you need to create controller file for it and then do the same thing which you are performed in product controller.
Assigned weight varible in catalog/controller/product/default.php (Note: If you did not created default.php controller then you need to create) then add following code
> $data['weight'] = $this->weight->format($product_info['weight'],$product_info['weight_class_id']);
and in /catalog/view/theme/*/template/product/default.tpl
add following code in above file.
<ul class="volume">
<?php if ($weight > 0) { ?>
<li><?php echo $weight; ?></li>
<?php } ?>
</ul>

In OpenCart, How can I customize the info visible on the product page

Okay, first I want to say that I am not a geek programmer. I am intermediate to programming. I was recently learning Php through teamtreehouse, which I have not finished yet. But instead I started making an online store from OpenCart. I watched some youtube videos and added some products, categories etc. Now I am strucked and lost my sleep, because there is no proper explanation on the web. The Problem is- On the product page I have a category of Reward Points for many products. But only thing visible on the category page is image, price, short description, Add to Cart option. I am sorry, If I am not being able to explain it properly...Below is the url of the screenshot-
http://postimg.org/image/7s413wjyd/
And also see what I want to make-
http://postimg.org/image/rggxj7hq5/
Is there a way of doing it through the admin page of opencart?? If not then please guide me how it will work by code. I am new to php too, but it will make me understand. Please help me out.
Is there a way of doing it through the admin page of opencart??
No
If not then please guide me how it will work by code
It's a very simple task but you will not be able to do it if you are not aware of web development basics
First of all, you should read about the MVC design pattern, there are pretty cool MVC examples for PHP on the internet, GIYF !
Second, you should read this great article which explains basics of OC, Here
The real work:
Make sure that each product in the array of products to be displayed in the category page has the value of reward points (or what ever datum you want), you can check that by opening ControllerProductCategory class # catalog/controller/product/category.php and dumping the variable $data['products'], if it's there then step 1 in the real work is done, if not, you will need to change the model function getProducts() in ModelCatalogProduct class # catalog/model/catalog/product.php
Now in the category template file catalog/view/theme/your theme folder/template/product/category.tpl, access the value of reward points and display it the product html div, you will find it enclosed in this loopforeach ($products as $product) {
That may seem too much for you to learn (MVC and all that stuff...), but you have to climb the ladder, Good Luck !
Try something like in default OpenCart code.
Step 1
Open file: catalog\language\english\product\category.php
Find:
$_['text_price'] = 'Price:';
Add after:
$_['text_reward'] = 'Reward Points:';
Step 2
Open file: catalog\controller\product\category.php
Find:
'price' => $price,
Add after:
'points' => $result['points'],
Step 3
In the same file: catalog\controller\product\category.php
Find:
$data['text_price'] = $this->language->get('text_price');
Add after:
$data['text_reward'] = $this->language->get('text_reward');
Step 4
Open File: catalog\view\theme\default\template\product\category.tpl
Find:
<?php if ($product['price']) { ?>
<p class="price">
<?php if (!$product['special']) { ?>
<?php echo $product['price']; ?>
<?php } else { ?>
<span class="price-new"><?php echo $product['special']; ?></span> <span class="price-old"><?php echo $product['price']; ?></span>
<?php } ?>
<?php if ($product['tax']) { ?>
<span class="price-tax"><?php echo $text_tax; ?> <?php echo $product['tax']; ?></span>
<?php } ?>
</p>
<?php } ?>
Add after:
<p><?php echo $text_reward; ?> <?php echo $product['points']; ?></p>
and then check it.

Opencart targeting specific product_id

I'm trying to display the price of particular product (with the ID 51) on the home page. It is not a featured product, but something else entirely so I can't use the featured module.
I know the product ID is 51, so have tried to use the following:
<?php if (!$product[$product_id[51]]['special']) { ?>
<?php echo $product[$product_id[51]]['price']; ?>
<?php } else { ?>
<span class="price-old"><?php echo $product_id[51]['price']; ?></span> <span class="price-new"><?php echo $product_id[51]['special']; ?></span>
<?php } ?>
but this only returns "undefined variable product_id". How do I display the price of this particular product?
MTIA.
You can get the product in full using
$product = $this->model_catalog_product->getProduct(51);
Don't forget when using currency values you should format them appropriately using
$this->currency->format()
To get an idea of what code you need to be using, take a look inside the catalog/controller/product/product.php for the special and price coding

Categories