Home › Forums › WoodMart support forum › The Product labels not showing › Reply To: The Product labels not showing
justinavct
Hi,
Thanks for that, but after activating the parent theme, some of the functions.PHP code is not working for the Parent theme.
It will show this information after I copy and paste the code from the Child theme to the parent theme:
Fatal error: Cannot redeclare woodmart_child_enqueue_styles() (previously declared in /home/customer/www/rantingj11.sg-host.com/public_html/wp-content/themes/woodmart-child/functions.php:6) in /home/customer/www/rantingj11.sg-host.com/public_html/wp-content/themes/woodmart/functions.php on line 76
There has been a critical error on this website.”
Can you help to add a conditional statement of the PHP code? Here is the code that I had added to the child theme before:
<?php
/**
* Enqueue script and styles for child theme
*/
function woodmart_child_enqueue_styles() {
wp_enqueue_style( ‘child-style’, get_stylesheet_directory_uri() . ‘/style.css’, array( ‘woodmart-style’ ), woodmart_get_theme_info( ‘Version’ ) );
}
add_action( ‘wp_enqueue_scripts’, ‘woodmart_child_enqueue_styles’, 10010 );
// function get_variation_id_by_slug($product_id, $variation_slug) {
// $product = wc_get_product($product_id);
// if ($product) {
// $variations = $product->get_available_variations();
// // print_r($variations);
// foreach ($variations as $variation) {
// if ($variation[‘attributes’][‘attribute_pa_cabinet-options’] === $variation_slug) {
// return $variation[‘variation_id’];
// }
// }
// }
// return 0; // Return 0 if variation not found
// }
// function get_stock_status() {
// $variation_slug = $_POST[‘variation_id’];
// $product_id = $_POST[‘product_id’];
// $variation_id = get_variation_id_by_slug($product_id, $variation_slug);
// $variation = wc_get_product($variation_id);
// $stock_quantity = $variation->get_stock_quantity();
// if ($stock_quantity === 0) {
// echo ‘outofstock’;
// } elseif ($stock_quantity <= 10) {
// echo ‘lowstock’;
// } else {
// echo ‘instock’;
// }
// die();
// }
// add_action(‘wp_ajax_get_stock_status’, ‘get_stock_status’);
// add_action(‘wp_ajax_nopriv_get_stock_status’, ‘get_stock_status’);
// * archive page stock label
function woocommerce_stock_status_archive_loop()
{
global $product;
if ($product->is_type(‘simple’)) {
if (!$product->is_in_stock()) {
echo ‘<div class=”product-labels labels-rounded new”><span class=”out-of-stock product-label”>Out of Stock</span></div>’;
} elseif ($product->is_in_stock() && $product->get_stock_quantity() < 10) {
echo ‘<div class=”product-labels labels-rounded new”><span class=”out-of-stock product-label”>Low Stock</span></div>’;
} else if ($product->is_in_stock() && $product->get_stock_quantity() >= 10) {
echo ‘<div class=”product-labels labels-rounded new”><span class=”out-of-stock product-label”>In Stock</span></div>’;
}
}
}
// Add stock status to archive pages
// add_action( ‘woocommerce_before_shop_loop_item’, ‘woocommerce_stock_status_archive_loop’, 10 );
// * single product page
function woocommerce_stock_status_single()
{
global $product;
if ($product->is_type(‘simple’)) {
if (!$product->is_in_stock()) {
echo ‘<div class=”stock red”>Out of Stock</div>’;
} elseif ($product->is_in_stock() && $product->get_stock_quantity() < 10) {
echo ‘<div class=”stock orange”>Low Stock</div>’;
} else if ($product->is_in_stock() && $product->get_stock_quantity() >= 10) {
echo ‘<div class=”stock orange”>In Stock</div>’;
}
}
}
// Add stock status to single pages
add_action( ‘woocommerce_product_meta_start’, ‘woocommerce_stock_status_single’, 10 );
// * stock label
add_action( ‘woocommerce_before_single_variation’, ‘variation_info’ );
function variation_info() {
global $product;
if ( ! $product->is_type( ‘variable’ ) ) return;
echo ‘<div class=”var_info stock”></div>’;
wc_enqueue_js( ”
$(document).on(‘found_variation’, ‘form.cart’, function( event, variation ) {
$(‘.var_info’).addClass(‘show’);
$(‘.var_info’).removeClass(‘hide’);
if(variation.max_qty >= 10){
$(‘.var_info’).html(‘In Stock’);
$(‘.var_info’).addClass(‘in-stock’);
$(‘.var_info’).removeClass(‘low-stock’);
$(‘.var_info’).removeClass(‘out-of-stock’);
}
else if(variation.max_qty < 10 && variation.max_qty > 1 ){
$(‘.var_info’).html(‘Low Stock’);
$(‘.var_info’).removeClass(‘in-stock’);
$(‘.var_info’).addClass(‘low-stock’);
$(‘.var_info’).removeClass(‘out-of-stock’);
}else if(variation.max_qty < 1 ){
$(‘.var_info’).html(‘Out Of Stock’);
$(‘.var_info’).removeClass(‘in-stock’);
$(‘.var_info’).removeClass(‘low-stock’);
$(‘.var_info’).addClass(‘out-of-stock’);
}
});
” );
}
function wooc_extra_register_fields() {?>
<p class=”form-row form-row-wide”>
<label for=”reg_billing_phone”><?php _e( ‘Phone’, ‘woocommerce’ ); ?></label>
<input type=”text” class=”input-text” name=”billing_phone” id=”reg_billing_phone” value=”<?php esc_attr_e( $_POST[‘billing_phone’] ); ?>” />
</p>
<p class=”form-row form-row-wide”>
<label for=”reg_billing_business_name”><?php _e( ‘Business name’, ‘woocommerce’ ); ?><span class=”required”>*</span></label>
<input type=”text” class=”input-text” name=”billing_business_name” id=”reg_billing_business_name” value=”<?php if ( ! empty( $_POST[‘billing_business_name’] ) ) esc_attr_e( $_POST[‘billing_business_name’] ); ?>” />
</p>
<div class=”clear”></div>
<?php
}
add_action( ‘woocommerce_register_form_start’, ‘wooc_extra_register_fields’ );