woocommerce variations

后端 未结 5 862
再見小時候
再見小時候 2020-12-14 08:14

I am creating a woocommerce theme and I have product variations i.e. size which is displayed on product details page but problem is that I want to get all variations in my c

5条回答
  •  孤城傲影
    2020-12-14 08:37

    A common problem often starts from changing "In stock" or "Out of stock" labels for a single product. There are few solutions out there, where you need to change functions.php file and add a new filter.

    The problems get more complicated when you need to modify this for product variations.

    This may be your solution: http://bucketpress.com/changing-stock-availability-text-for-product-variations

    In order to display available variations of the product you need to modify variable.php file, which can be found in /wp-content/plugins/woocommerce/templates/single-product/add-to-cart/

    Find this:

    and before tag paste this code:

    foreach( $available_variations as $i => $variation ) {
    //check if variation has stock or not 
    if ( $variation['is_in_stock'] ) {
        // Get max qty that user can purchase
        $max_qty = $variation['max_qty'];
    
        // Prepare availability html for stock available instance
        $availability_html = '

    ' . $max_qty . ' units available for your purchase.' . '

    '; } else { // Prepare availability html for out of stock instance $availability_html = '

    Oops, we have no stock left.

    '; } $available_variations[$i]['availability_html'] = $availability_html; }

    Don't forget to move php end tag "?>" from this line

    do_action( 'woocommerce_before_add_to_cart_form' ); ?>
    

    after your new code.

    So the complete variable.php file should like something like this (WooCommerce 3.2.4):

     $variation ) {
        // check if variation has stock or not 
        if ( $variation['is_in_stock'] ) {
            // Get max qty that user can purchase
            $max_qty = $variation['max_qty'];
    
            // Prepare availability html for stock available instance
            $availability_html = '

    Available: ' . $max_qty . '

    '; } else { // Prepare availability html for out of stock instance $availability_html = '

    Out of stock!

    '; } $available_variations[$i]['availability_html'] = $availability_html; } ?>

    $options ) : ?>
    get_variation_default_attribute( $attribute_name ); wc_dropdown_variation_attribute_options( array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected ) ); echo end( $attribute_keys ) === $attribute_name ? apply_filters( 'woocommerce_reset_variations_link', '' . esc_html__( 'Clear', 'woocommerce' ) . '' ) : ''; ?>

    All credits to "Kevin" from: http://bucketpress.com/author/base-admin

提交回复
热议问题