<?php
/**
* Shop breadcrumb
*
* This template can be overridden by copying it to yourtheme/woocommerce/global/breadcrumb.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce/Templates
* @version 2.3.0
* @see woocommerce_breadcrumb()
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! empty( $breadcrumb ) ) {
echo $wrap_before;
foreach ( $breadcrumb as $key => $crumb ) {
echo $before;
if ( ! empty( $crumb[1] ) && sizeof( $breadcrumb ) !== $key + 1 ) {
echo '<a href="' . esc_url( $crumb[1] ) . '">' . esc_html( $crumb[0] ) . '</a>';
} else {
echo esc_html( $crumb[0] );
}
echo $after;
if ( sizeof( $breadcrumb ) !== $key + 1 ) {
echo $delimiter;
}
}
echo $wrap_after;
}
add_action( 'woocommerce_before_add_to_cart_button', 'sizes_before_add_to_cart_button', 0 );
function sizes_before_add_to_cart_button() {
global $product;
// Only on simple products
if( ! $product->is_type('simple') ) return;
// When product sizes are available
if ( $sizes = $product->get_attribute( 'size' ) ) :
$sizes = explode(", ",$sizes);
echo '<div class="lisatiedot__size__select" id="product-size-field">
<label for="product-size">' . __('Valitse koko <br>') . '</label>
<select class="select__size" name="product-size" id="product-size">
<option value="">' . __("Choose your size") . '</option>';
foreach( $sizes as $size ){
echo '<option value="' . $size . '">' . $size . '</option>';
}
echo '</select>
</div><br>';
endif;
}
add_filter( 'woocommerce_add_to_cart_validation', 'filter_add_to_cart_validation', 10, 4 );
function filter_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = 0 ) {
$product = wc_get_product($product_id);
// Only on simple products and When product sizes are available
if( ! $product->is_type('simple') && ! $product->get_attribute( 'size' ) )
return $passed;
if( isset( $_POST['product-size'] ) && empty( $_POST['product-size'] ) ) {
wc_add_notice( __("Please choose your size.", "woocommerce" ), 'error' );
$passed = false;
}
return $passed;
}
add_filter( 'woocommerce_add_cart_item_data', 'add_size_to_cart_item_data', 10, 3 );
function add_size_to_cart_item_data($cart_item_data, $product_id, $variation_id ){
if( isset( $_POST['product-size'] ) ) {
$cart_item_data['product-size'] = esc_attr( $_POST['product-size'] );
}
return $cart_item_data;
}
add_filter( 'woocommerce_get_item_data', 'display_size_on_cart_item', 10, 2 );
function display_size_on_cart_item( $cart_item_data, $cart_item ) {
if ( isset( $cart_item['product-size'] ) ){
$cart_item_data[] = array(
'name' => __('Size'),
'value' => $cart_item['product-size'],
);
}
return $cart_item_data;
}
<form class="cart"
action="<?php echo esc_url( apply_filters( 'woocommerce_add_to_cart_form_action', $product->get_permalink() ) ); ?>"
method="post"
enctype='multipart/form-data'>
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<?php
do_action( 'woocommerce_before_add_to_cart_quantity' );
woocommerce_quantity_input( array(
'min_value' => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( wp_unslash( $_POST['quantity'] ) ) : $product->get_min_purchase_quantity(), // WPCS: CSRF ok, input var ok.
) );
do_action( 'woocommerce_after_add_to_cart_quantity' );
?>
<button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
</form>
if ( $product->is_in_stock() ) {
echo '<div class="stock" >' . $product->get_stock_quantity() . ' в наличии</div>';
} else {
echo '<div class="out-of-stock" >Нет в наличии</div>';
}
$product->get_stock_quantity()