Plugin Directory

Changeset 3805284


Ignore:
Timestamp:
11/16/2026 01:46:30 PM (19 hours ago)
Author:
cartwrightplugins
Message:

Update to version 1.1.0 from GitHub

Location:
cut-and-pack-for-woocommerce
Files:
18 edited
1 copied

Legend:

Unmodified
Added
Removed
  • cut-and-pack-for-woocommerce/tags/1.1.0/assets/js/admin.js

    r3800171 r3805284  
    1313
    1414                $( panel + ' .cutpack-show-if-packs' ).toggle( 'packs' === mode );
     15                $( '.cutpack-variation-field' ).toggle( 'packs' === mode && 'area' === measure );
    1516                $( panel + ' .cutpack-show-if-cut' ).toggle( 'cut' === mode );
    1617                $( panel + ' .cutpack-show-if-area' ).toggle( 'area' === measure );
    … …  
    2728        $( function () {
    2829                $( document ).on( 'change', '#_cutpack_mode, #_cutpack_measure', refresh );
     30                $( document ).on( 'woocommerce_variations_loaded woocommerce_variations_added', refresh );
    2931                refresh();
    3032        } );
  • cut-and-pack-for-woocommerce/tags/1.1.0/assets/js/calculator.js

    r3800171 r3805284  
    1919                return;
    2020        }
    21         var form = box.closest( 'form.cart' );
     21        var form = box.closest( 'form' );
    2222        if ( ! form ) {
    2323                return;
    … …  
    2525
    2626        var qty      = form.querySelector( 'input[name="quantity"]' );
    27         var button   = form.querySelector( '.single_add_to_cart_button' );
     27        var button   = form.querySelector( '[type="submit"], .single_add_to_cart_button' );
    2828        var inputs   = box.querySelectorAll( '.cutpack-input' );
    2929        var resultEl = box.querySelector( '.cutpack-result' );
    … …  
    6868        }
    6969
    70         function calculate() {
     70        /**
     71         * What applies right now: the product itself, or the chosen variation.
     72         * The variation id is read from the form every time, never cached.
     73         */
     74        function current() {
     75                if ( ! d.variable ) {
     76                        return { ok: true, coverage: d.coverage, unitPrice: d.unitPrice, maxQty: -1 };
     77                }
     78                var field = form.querySelector( 'input[name="variation_id"]' );
     79                var id    = field ? parseInt( field.value, 10 ) || 0 : 0;
     80                if ( ! id ) {
     81                        return { ok: false, id: 0, message: d.i18n.chooseOption };
     82                }
     83                var v = d.variations[ id ];
     84                if ( ! v || ! v.purchasable ) {
     85                        return { ok: false, id: id, message: d.i18n.unavailable };
     86                }
     87                return { ok: true, id: id, coverage: v.coverage, unitPrice: v.unitPrice, maxQty: v.maxQty };
     88        }
     89
     90        function calculate( cur ) {
    7191                var entered;
    7292
    … …  
    93113
    94114                if ( 'packs' === d.mode ) {
    95                         var packs   = Math.max( 1, Math.ceil( entered / d.coverage - EPSILON ) );
    96                         var charged = round( packs * d.coverage, 6 );
     115                        var packs   = Math.max( 1, Math.ceil( entered / cur.coverage - EPSILON ) );
     116                        var charged = round( packs * cur.coverage, 6 );
    97117                        // Price of one box is rounded to whole pennies first, as on the server.
    98                         var boxPrice = round( d.coverage * d.unitPrice, d.currency.decimals );
     118                        var boxPrice = round( cur.coverage * cur.unitPrice, d.currency.decimals );
     119                        if ( cur.maxQty >= 0 && packs > cur.maxQty ) {
     120                                var inStock = format( 1 === cur.maxQty ? d.i18n.oneBox : d.i18n.manyBoxes, cur.maxQty );
     121                                return { error: format( d.i18n.stock, inStock, formatAmount( cur.maxQty * cur.coverage ), d.unit ) };
     122                        }
    99123                        return { packs: packs, charged: charged, price: packs * boxPrice };
    100124                }
    101125
    102126                var amount = d.step > 0 ? round( Math.ceil( entered / d.step - EPSILON ) * d.step, 6 ) : entered;
    103                 return { packs: 1, charged: amount, price: round( amount * d.unitPrice, d.currency.decimals ) };
     127                return { packs: 1, charged: amount, price: round( amount * cur.unitPrice, d.currency.decimals ) };
    104128        }
    105129
    … …  
    121145        }
    122146
     147        /** Write only when the text changes, so screen readers don't repeat themselves. */
     148        function setText( el, text ) {
     149                if ( el.textContent !== text ) {
     150                        el.textContent = text;
     151                }
     152        }
     153
     154        /** The quantity box is hidden, so the browser must never block the form over it. */
     155        function relaxQuantity() {
     156                if ( qty ) {
     157                        qty.removeAttribute( 'min' );
     158                        qty.removeAttribute( 'max' );
     159                        qty.removeAttribute( 'step' );
     160                }
     161        }
     162
     163        /** The calculator replaces the normal quantity box. WooCommerce may show it again, so hide it on every update. */
     164        function hideQuantity() {
     165                var wrap = qty && button ? qty.closest( '.quantity' ) : null;
     166                if ( wrap ) {
     167                        wrap.style.setProperty( 'display', 'none', 'important' );
     168                }
     169        }
     170
    123171        function update() {
     172                hideQuantity();
     173                var cur = current();
     174                if ( ! cur.ok ) {
     175                        setText( resultEl, cur.message );
     176                        setText( errorEl, '' );
     177                        setButton( false );
     178                        return;
     179                }
     180
    124181                if ( isEmpty() ) {
    125                         resultEl.textContent = '';
    126                         errorEl.textContent  = '';
    127                         setButton( false );
    128                         return;
    129                 }
    130 
    131                 var r = calculate();
     182                        setText( resultEl, '' );
     183                        setText( errorEl, '' );
     184                        setButton( false );
     185                        return;
     186                }
     187
     188                var r = calculate( cur );
    132189
    133190                if ( r.error ) {
    134                         resultEl.textContent = '';
    135                         errorEl.textContent  = r.error;
    136                         setButton( false );
    137                         return;
    138                 }
    139 
    140                 errorEl.textContent = '';
     191                        setText( resultEl, '' );
     192                        setText( errorEl, r.error );
     193                        setButton( false );
     194                        return;
     195                }
     196
     197                setText( errorEl, '' );
    141198
    142199                if ( 'packs' === d.mode ) {
    143200                        var boxes = format( 1 === r.packs ? d.i18n.oneBox : d.i18n.manyBoxes, r.packs );
    144                         resultEl.textContent = format( d.i18n.resultPacks, boxes, formatAmount( r.charged ), d.unit, formatPrice( r.price ) );
     201                        setText( resultEl, format( d.i18n.resultPacks, boxes, formatAmount( r.charged ), d.unit, formatPrice( r.price ) ) );
    145202                        if ( qty ) {
    146203                                qty.value = r.packs;
    147204                        }
    148205                } else {
    149                         resultEl.textContent = format( d.i18n.resultCut, formatAmount( r.charged ), d.unit, formatPrice( r.price ) );
     206                        setText( resultEl, format( d.i18n.resultCut, formatAmount( r.charged ), d.unit, formatPrice( r.price ) ) );
    150207                        if ( qty ) {
    151208                                qty.value = 1;
    … …  
    153210                }
    154211
     212                relaxQuantity();
    155213                setButton( true );
    156214        }
    157215
    158         // The calculator replaces the normal quantity box.
    159         if ( qty ) {
    160                 var wrap = qty.closest( '.quantity' );
    161                 if ( wrap ) {
    162                         wrap.style.display = 'none';
    163                 }
     216        /** Re-run after WooCommerce (or the block) has finished its own changes. */
     217        function later() {
     218                window.setTimeout( function () {
     219                        update();
     220                        if ( window.requestAnimationFrame ) {
     221                                window.requestAnimationFrame( update );
     222                        }
     223                }, 0 );
    164224        }
    165225
    … …  
    168228        }
    169229
     230        if ( d.variable ) {
     231                // Any interaction with the form may change the chosen variation.
     232                [ 'click', 'change', 'input', 'keyup' ].forEach( function ( name ) {
     233                        form.addEventListener( name, later );
     234                } );
     235
     236                // WooCommerce's own events, when jQuery is on the page.
     237                if ( window.jQuery ) {
     238                        window.jQuery( form ).on( 'found_variation show_variation hide_variation reset_data check_variations', later );
     239                }
     240
     241                // Backstop for themes that change the chosen option without any event.
     242                var lastId = null;
     243                window.setInterval( function () {
     244                        var now = current();
     245                        var key = String( now.id || 0 ) + ( now.ok ? '1' : '0' );
     246                        if ( key !== lastId ) {
     247                                lastId = key;
     248                                update();
     249                        }
     250                }, 250 );
     251        }
     252
     253        // Drop the hidden quantity's limits just before the browser checks the form.
     254        if ( button ) {
     255                button.addEventListener( 'click', relaxQuantity, true );
     256        }
     257
    170258        // Stop the form being sent with an invalid size (e.g. by pressing Enter).
    171259        form.addEventListener( 'submit', function ( event ) {
    172                 var r = isEmpty() ? { error: d.i18n.invalid } : calculate();
     260                var cur = current();
     261                var r   = ! cur.ok ? { error: cur.message } : ( isEmpty() ? { error: d.i18n.invalid } : calculate( cur ) );
     262                relaxQuantity();
    173263                if ( r.error ) {
    174264                        event.preventDefault();
    175                         errorEl.textContent = r.error;
     265                        setText( errorEl, r.error );
    176266                        setButton( false );
    177267                }
  • cut-and-pack-for-woocommerce/tags/1.1.0/cut-and-pack-for-woocommerce.php

    r3800171 r3805284  
    33 * Plugin Name:          Cut & Pack for WooCommerce
    44 * Description:          Made-to-measure pricing for WooCommerce. Sell by m² or metre, round up to whole packs, and show the price live.
    5  * Version:              1.0.0
     5 * Version:              1.1.0
    66 * Requires at least:    6.9
    77 * Requires PHP:         7.4
    … …  
    2121defined( 'ABSPATH' ) || exit;
    2222
    23 define( 'CUTPACK_VERSION', '1.0.0' );
     23define( 'CUTPACK_VERSION', '1.1.0' );
    2424define( 'CUTPACK_FILE', __FILE__ );
    2525define( 'CUTPACK_PATH', plugin_dir_path( __FILE__ ) );
  • cut-and-pack-for-woocommerce/tags/1.1.0/includes/class-cutpack-admin.php

    r3800171 r3805284  
    2020                add_action( 'woocommerce_product_data_panels', array( __CLASS__, 'render_panel' ) );
    2121                add_action( 'woocommerce_admin_process_product_object', array( __CLASS__, 'save' ) );
     22                add_action( 'woocommerce_product_after_variable_attributes', array( __CLASS__, 'render_variation_field' ), 10, 3 );
     23                add_action( 'woocommerce_admin_process_variation_object', array( __CLASS__, 'save_variation' ), 10, 2 );
    2224                add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue' ) );
    2325                add_filter( 'woocommerce_get_settings_products', array( __CLASS__, 'store_settings' ), 10, 2 );
    … …  
    7880                        'label'    => __( 'Made to measure', 'cut-and-pack-for-woocommerce' ),
    7981                        'target'   => 'cutpack_product_data',
    80                         'class'    => array( 'show_if_simple', 'hide_if_variable', 'hide_if_grouped', 'hide_if_external' ),
     82                        'class'    => array( 'show_if_simple', 'show_if_variable', 'hide_if_grouped', 'hide_if_external' ),
    8183                        'priority' => 65,
    8284                );
    … …  
    147149
    148150                echo '<p class="form-field cutpack-price-note"><span class="description">';
    149                 echo esc_html__( 'The price comes from the normal Regular price field on the General tab, read as a price per m² or per metre.', 'cut-and-pack-for-woocommerce' );
     151                if ( $product_object instanceof WC_Product && $product_object->is_type( 'variable' ) ) {
     152                        echo esc_html__( 'The price comes from each variation\'s own Regular price, read as a price per m² or per metre.', 'cut-and-pack-for-woocommerce' );
     153                } else {
     154                        echo esc_html__( 'The price comes from the normal Regular price field on the General tab, read as a price per m² or per metre.', 'cut-and-pack-for-woocommerce' );
     155                }
    150156                echo '</span></p>';
    151157
    … …  
    227233
    228234        /**
     235         * Optional per-variation pack size, shown in each variation's panel.
     236         *
     237         * @param int     $loop           Position in the loop.
     238         * @param array   $variation_data Variation data.
     239         * @param WP_Post $variation      Variation post.
     240         */
     241        public static function render_variation_field( $loop, $variation_data, $variation ) {
     242                $value = get_post_meta( $variation->ID, Cutpack_Settings::meta_key( 'coverage' ), true );
     243                ?>
     244                <div class="cutpack-variation-field form-row form-row-full">
     245                        <label for="cutpack_variation_coverage_<?php echo esc_attr( $loop ); ?>">
     246                                <?php echo esc_html( sprintf( /* translators: %s: unit */ __( 'Pack size: coverage per pack (%s)', 'cut-and-pack-for-woocommerce' ), Cutpack_Settings::unit_label( Cutpack_Settings::MEASURE_AREA ) ) ); ?>
     247                        </label>
     248                        <input type="text" inputmode="decimal" class="short" id="cutpack_variation_coverage_<?php echo esc_attr( $loop ); ?>" name="cutpack_variation_coverage[<?php echo esc_attr( $loop ); ?>]" value="<?php echo esc_attr( $value ); ?>" placeholder="<?php echo esc_attr__( 'Same as the product', 'cut-and-pack-for-woocommerce' ); ?>" />
     249                        <span class="description"><?php esc_html_e( 'Only for packs. Leave empty to use the number from the "Made to measure" tab.', 'cut-and-pack-for-woocommerce' ); ?></span>
     250                </div>
     251                <?php
     252        }
     253
     254        /**
     255         * Save the per-variation pack size. WooCommerce has already checked the nonce.
     256         *
     257         * @param WC_Product_Variation $variation Variation being saved.
     258         * @param int                  $i         Position in the posted list.
     259         */
     260        public static function save_variation( $variation, $i ) {
     261                // phpcs:disable WordPress.Security.NonceVerification.Missing -- Nonce checked by WooCommerce before this hook runs.
     262                if ( ! isset( $_POST['cutpack_variation_coverage'][ $i ] ) || is_array( $_POST['cutpack_variation_coverage'][ $i ] ) ) {
     263                        return;
     264                }
     265                $raw = sanitize_text_field( wp_unslash( $_POST['cutpack_variation_coverage'][ $i ] ) );
     266                // phpcs:enable
     267
     268                if ( ! current_user_can( 'edit_post', $variation->get_parent_id() ) ) {
     269                        return;
     270                }
     271                $variation->update_meta_data( Cutpack_Settings::meta_key( 'coverage' ), Cutpack_Settings::to_positive_decimal( $raw ) );
     272        }
     273
     274        /**
    229275         * Save the tab's fields. WooCommerce has already checked the nonce and permissions.
    230276         *
    … …  
    278324                }
    279325
    280                 if ( '' === $product->get_regular_price() ) {
     326                // A variable product keeps its prices on the variations.
     327                if ( ! $product->is_type( 'variable' ) && '' === $product->get_regular_price() ) {
    281328                        $problems[] = __( 'Enter a Regular price on the General tab (price per m² or per metre).', 'cut-and-pack-for-woocommerce' );
    282329                }
  • cut-and-pack-for-woocommerce/tags/1.1.0/includes/class-cutpack-cart.php

    r3800171 r3805284  
    3535
    3636        /**
     37         * Variation chosen for each product during this add-to-cart request, as
     38         * seen by the validation filter (which WooCommerce gives the variation,
     39         * even when the form only posted attributes).
     40         *
     41         * @var int[]
     42         */
     43        private static $chosen = array();
     44
     45        /**
    3746         * Register hooks.
    3847         */
    3948        public static function init() {
    40                 add_filter( 'woocommerce_add_to_cart_validation', array( __CLASS__, 'validate' ), 10, 2 );
    41                 add_filter( 'woocommerce_add_to_cart_quantity', array( __CLASS__, 'quantity' ), 10, 2 );
    42                 add_filter( 'woocommerce_add_cart_item_data', array( __CLASS__, 'add_item_data' ), 10, 2 );
    43 
     49                add_filter( 'woocommerce_add_to_cart_validation', array( __CLASS__, 'validate' ), 10, 4 );
     50                add_filter( 'woocommerce_add_to_cart_quantity', array( __CLASS__, 'quantity' ), 10, 3 );
     51                add_filter( 'woocommerce_add_cart_item_data', array( __CLASS__, 'add_item_data' ), 10, 3 );
     52
     53                add_filter( 'woocommerce_add_cart_item', array( __CLASS__, 'lock_quantity' ), 15 );
    4454                add_filter( 'woocommerce_add_cart_item', array( __CLASS__, 'apply_price' ), 20 );
    4555                add_filter( 'woocommerce_get_cart_item_from_session', array( __CLASS__, 'apply_price' ), 20 );
    … …  
    7585
    7686        /**
     87         * Work out the parent product and variation being added.
     88         *
     89         * WooCommerce passes the variation ID to some filters but not all, and
     90         * `?add-to-cart=<variation id>` passes the variation as the product.
     91         * Every server path resolves it here so they cannot disagree.
     92         *
     93         * @param int $product_id   Product ID as received by the filter.
     94         * @param int $variation_id Variation ID as received by the filter, or 0.
     95         * @return int[] array( product ID, variation ID ).
     96         */
     97        private static function resolve_ids( $product_id, $variation_id = 0 ) {
     98                $product_id   = absint( $product_id );
     99                $variation_id = absint( $variation_id );
     100
     101                $product = wc_get_product( $product_id );
     102                if ( $product instanceof WC_Product && $product->is_type( 'variation' ) ) {
     103                        return array( (int) $product->get_parent_id(), $product_id );
     104                }
     105
     106                if ( ! $variation_id && isset( self::$chosen[ $product_id ] ) ) {
     107                        return array( $product_id, self::$chosen[ $product_id ] );
     108                }
     109
     110                if ( ! $variation_id && isset( $_REQUEST['variation_id'] ) && is_scalar( $_REQUEST['variation_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- WooCommerce add-to-cart forms carry no nonce; the ID is only used after checking it belongs to the product.
     111                        $variation_id = absint( wp_unslash( $_REQUEST['variation_id'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
     112                }
     113
     114                return array( $product_id, $variation_id );
     115        }
     116
     117        /**
    77118         * Calculate the posted input for a product.
    78119         *
    79          * @param int $product_id Product ID.
     120         * @param int $product_id   Product ID.
     121         * @param int $variation_id Variation ID, or 0.
    80122         * @return array|WP_Error|null Null if the product doesn't use the calculator.
    81123         */
    82         private static function posted_result( $product_id ) {
     124        private static function posted_result( $product_id, $variation_id = 0 ) {
     125                list( $product_id, $variation_id ) = self::resolve_ids( $product_id, $variation_id );
     126
    83127                $product = wc_get_product( $product_id );
    84128                if ( ! Cutpack_Settings::is_enabled( $product ) ) {
    85129                        return null;
    86130                }
    87                 return Cutpack_Calculator::calculate( Cutpack_Settings::get( $product ), self::posted_input() );
     131
     132                if ( $product->is_type( 'variable' ) ) {
     133                        $variation = Cutpack_Settings::owned_variation( $product, $variation_id );
     134                        if ( ! $variation ) {
     135                                return new WP_Error( 'cutpack_variation', __( 'Choose an option first.', 'cut-and-pack-for-woocommerce' ) );
     136                        }
     137                }
     138
     139                return Cutpack_Calculator::calculate( Cutpack_Settings::get_for( $product_id, $variation_id ), self::posted_input() );
    88140        }
    89141
    … …  
    91143         * Block adding a measured product without a valid size.
    92144         *
    93          * @param bool $passed     Whether earlier checks passed.
    94          * @param int  $product_id Product ID.
     145         * @param bool $passed       Whether earlier checks passed.
     146         * @param int  $product_id   Product ID.
     147         * @param int  $quantity     Quantity (unused).
     148         * @param int  $variation_id Variation ID.
    95149         * @return bool
    96150         */
    97         public static function validate( $passed, $product_id ) {
    98                 $result = self::posted_result( $product_id );
     151        public static function validate( $passed, $product_id, $quantity = 1, $variation_id = 0 ) {
     152                if ( absint( $variation_id ) ) {
     153                        self::$chosen[ absint( $product_id ) ] = absint( $variation_id );
     154                }
     155                $result = self::posted_result( $product_id, $variation_id );
    99156                if ( null === $result || ! $passed ) {
    100157                        return $passed;
    … …  
    111168         * whatever quantity the browser sent.
    112169         *
    113          * @param int $quantity   Quantity sent.
    114          * @param int $product_id Product ID.
     170         * @param int $quantity     Quantity sent.
     171         * @param int $product_id   Product ID.
     172         * @param int $variation_id Variation ID.
    115173         * @return int
    116174         */
    117         public static function quantity( $quantity, $product_id ) {
    118                 $result = self::posted_result( $product_id );
     175        public static function quantity( $quantity, $product_id, $variation_id = 0 ) {
     176                $result = self::posted_result( $product_id, $variation_id );
    119177                if ( null === $result || is_wp_error( $result ) ) {
    120178                        return $quantity;
    … …  
    126184         * Store the measurements on the cart item.
    127185         *
    128          * @param array $data       Cart item data.
    129          * @param int   $product_id Product ID.
     186         * @param array $data         Cart item data.
     187         * @param int   $product_id   Product ID.
     188         * @param int   $variation_id Variation ID.
    130189         * @return array
    131190         */
    132         public static function add_item_data( $data, $product_id ) {
    133                 $result = self::posted_result( $product_id );
     191        public static function add_item_data( $data, $product_id, $variation_id = 0 ) {
     192                $result = self::posted_result( $product_id, $variation_id );
    134193                if ( null === $result || is_wp_error( $result ) ) {
    135194                        return $data;
    … …  
    154213
    155214        /**
     215         * The stored measurement is authoritative: make the line quantity match it,
     216         * so quantity and measurements can never disagree.
     217         *
     218         * @param array $item Cart item.
     219         * @return array
     220         */
     221        public static function lock_quantity( $item ) {
     222                if ( empty( $item[ self::KEY ] ) || ! isset( $item[ self::KEY ]['packs'] ) ) {
     223                        return $item;
     224                }
     225                $settings         = Cutpack_Settings::get_for( $item['product_id'], isset( $item['variation_id'] ) ? $item['variation_id'] : 0 );
     226                $item['quantity'] = Cutpack_Settings::MODE_PACKS === $settings['mode'] ? (int) $item[ self::KEY ]['packs'] : 1;
     227                return $item;
     228        }
     229
     230        /**
    156231         * Set the price of one cart unit (one pack, or one cut piece).
    157232         *
    … …  
    163238                        return $item;
    164239                }
    165                 $settings = Cutpack_Settings::get( $item['product_id'] );
    166                 $price    = Cutpack_Calculator::line_unit_price( $settings, $item[ self::KEY ], self::base_price( $item['product_id'] ) );
     240                $variation_id = isset( $item['variation_id'] ) ? (int) $item['variation_id'] : 0;
     241                $settings     = Cutpack_Settings::get_for( $item['product_id'], $variation_id );
     242                $price        = Cutpack_Calculator::line_unit_price( $settings, $item[ self::KEY ], self::base_price( $variation_id ? $variation_id : $item['product_id'] ) );
    167243
    168244                /**
    … …  
    285361                        return $item_data;
    286362                }
    287                 $settings = Cutpack_Settings::get( $item['product_id'] );
     363                $settings = Cutpack_Settings::get_for( $item['product_id'], isset( $item['variation_id'] ) ? $item['variation_id'] : 0 );
    288364                foreach ( self::describe( $item[ self::KEY ], $settings, $item['quantity'] ) as $line ) {
    289365                        $item_data[] = array(
    … …  
    307383                        return;
    308384                }
    309                 $settings = Cutpack_Settings::get( $values['product_id'] );
     385                $settings = Cutpack_Settings::get_for( $values['product_id'], isset( $values['variation_id'] ) ? $values['variation_id'] : 0 );
    310386                foreach ( self::describe( $values[ self::KEY ], $settings, $values['quantity'] ) as $line ) {
    311387                        $order_item->add_meta_data( $line[0], $line[1], true );
  • cut-and-pack-for-woocommerce/tags/1.1.0/includes/class-cutpack-frontend.php

    r3800171 r3805284  
    3333                        return $html;
    3434                }
    35                 $s = Cutpack_Settings::get( $product );
     35                $s = $product->is_type( 'variation' )
     36                        ? Cutpack_Settings::get_for( $product->get_parent_id(), $product->get_id() )
     37                        : Cutpack_Settings::get( $product );
    3638                return $html . ' <span class="cutpack-per-unit">/ ' . esc_html( Cutpack_Settings::unit_label( $s['measure'] ) ) . '</span>';
    3739        }
    … …  
    9294
    9395        /**
     96         * Per-variation data for the browser preview, keyed by variation ID.
     97         *
     98         * Built on the server so the preview never depends on theme markup or on
     99         * WooCommerce's own variation scripts.
     100         *
     101         * @param WC_Product $product Variable product.
     102         * @return array
     103         */
     104        private static function variation_map( $product ) {
     105                $map = array();
     106                if ( ! $product->is_type( 'variable' ) ) {
     107                        return $map;
     108                }
     109
     110                /**
     111                 * Maximum number of variations sent to the browser preview.
     112                 *
     113                 * @param int $limit Limit.
     114                 */
     115                $limit = (int) apply_filters( 'cutpack_max_preview_variations', 500 );
     116
     117                $children = array_slice( $product->get_children(), 0, $limit );
     118                if ( $children ) {
     119                        _prime_post_caches( $children, false, true );
     120                }
     121
     122                foreach ( $children as $variation_id ) {
     123                        $variation = wc_get_product( $variation_id );
     124                        if ( ! $variation instanceof WC_Product_Variation || ! $variation->variation_is_visible() ) {
     125                                continue;
     126                        }
     127                        $vs    = Cutpack_Settings::get_for( $product->get_id(), $variation->get_id() );
     128                        $price = $variation->get_price();
     129
     130                        $map[ $variation->get_id() ] = array(
     131                                'coverage'    => (float) $vs['coverage'],
     132                                'unitPrice'   => '' === $price ? 0 : (float) wc_get_price_to_display( $variation ),
     133                                'purchasable' => '' !== $price && $variation->is_purchasable() && $variation->is_in_stock(),
     134                                'maxQty'      => (int) $variation->get_max_purchase_quantity(),
     135                        );
     136                }
     137                return $map;
     138        }
     139
     140        /**
    94141         * Load the calculator script and its settings on enabled product pages.
    95142         */
    … …  
    112159                        'cutpackData',
    113160                        array(
    114                                 'id'        => 'cutpack-' . $product->get_id(),
    115                                 'mode'      => $s['mode'],
    116                                 'measure'   => $s['measure'],
    117                                 'input'     => $s['input'],
    118                                 'coverage'  => (float) $s['coverage'],
    119                                 'min'       => '' === $s['min'] ? 0 : (float) $s['min'],
    120                                 'max'       => '' === $s['max'] ? 0 : (float) $s['max'],
    121                                 'step'      => '' === $s['step'] ? 0 : (float) $s['step'],
    122                                 'unitPrice' => (float) wc_get_price_to_display( $product ),
    123                                 'unit'      => Cutpack_Settings::unit_label( $s['measure'] ),
    124                                 'currency'  => array(
     161                                'id'         => 'cutpack-' . $product->get_id(),
     162                                'mode'       => $s['mode'],
     163                                'measure'    => $s['measure'],
     164                                'input'      => $s['input'],
     165                                'coverage'   => (float) $s['coverage'],
     166                                'min'        => '' === $s['min'] ? 0 : (float) $s['min'],
     167                                'max'        => '' === $s['max'] ? 0 : (float) $s['max'],
     168                                'step'       => '' === $s['step'] ? 0 : (float) $s['step'],
     169                                'unitPrice'  => $product->is_type( 'variable' ) ? 0 : (float) wc_get_price_to_display( $product ),
     170                                'variable'   => $product->is_type( 'variable' ),
     171                                'variations' => self::variation_map( $product ),
     172                                'unit'       => Cutpack_Settings::unit_label( $s['measure'] ),
     173                                'currency'   => array(
    125174                                        'symbol'    => html_entity_decode( get_woocommerce_currency_symbol(), ENT_QUOTES, 'UTF-8' ),
    126175                                        'format'    => html_entity_decode( get_woocommerce_price_format(), ENT_QUOTES, 'UTF-8' ),
    … …  
    129178                                        'thousands' => wc_get_price_thousand_separator(),
    130179                                ),
    131                                 'i18n'      => array(
     180                                'i18n'       => array(
    132181                                        /* translators: 1: number of boxes, 2: amount, 3: unit, 4: price. Example: You need 17 boxes (24.48 m²) – £976.75 */
    133                                         'resultPacks' => __( 'You need %1$s (%2$s %3$s) – %4$s', 'cut-and-pack-for-woocommerce' ),
     182                                        'resultPacks'  => __( 'You need %1$s (%2$s %3$s) – %4$s', 'cut-and-pack-for-woocommerce' ),
    134183                                        /* translators: 1: amount, 2: unit, 3: price. Example: 2.4 m – £43.20 */
    135                                         'resultCut'   => __( '%1$s %2$s – %3$s', 'cut-and-pack-for-woocommerce' ),
     184                                        'resultCut'    => __( '%1$s %2$s – %3$s', 'cut-and-pack-for-woocommerce' ),
    136185                                        /* translators: %s: a number */
    137                                         'oneBox'      => __( '%s box', 'cut-and-pack-for-woocommerce' ),
     186                                        'oneBox'       => __( '%s box', 'cut-and-pack-for-woocommerce' ),
    138187                                        /* translators: %s: a number */
    139                                         'manyBoxes'   => __( '%s boxes', 'cut-and-pack-for-woocommerce' ),
    140                                         'invalid'     => __( 'Please enter a number greater than zero.', 'cut-and-pack-for-woocommerce' ),
    141                                         'invalidSize' => __( 'Please enter a length and a width greater than zero.', 'cut-and-pack-for-woocommerce' ),
     188                                        'manyBoxes'    => __( '%s boxes', 'cut-and-pack-for-woocommerce' ),
     189                                        'chooseOption' => __( 'Choose an option first.', 'cut-and-pack-for-woocommerce' ),
     190                                        'unavailable'  => __( 'This option is unavailable.', 'cut-and-pack-for-woocommerce' ),
     191                                        /* translators: 1: number of boxes in stock, 2: amount covered, 3: unit such as m² */
     192                                        'stock'        => __( 'Only %1$s in stock (%2$s %3$s).', 'cut-and-pack-for-woocommerce' ),
     193                                        'invalid'      => __( 'Please enter a number greater than zero.', 'cut-and-pack-for-woocommerce' ),
     194                                        'invalidSize'  => __( 'Please enter a length and a width greater than zero.', 'cut-and-pack-for-woocommerce' ),
    142195                                        /* translators: 1: minimum amount, 2: unit such as m² */
    143                                         'min'         => __( 'The minimum is %1$s %2$s.', 'cut-and-pack-for-woocommerce' ),
     196                                        'min'          => __( 'The minimum is %1$s %2$s.', 'cut-and-pack-for-woocommerce' ),
    144197                                        /* translators: 1: maximum amount, 2: unit such as m² */
    145                                         'max'         => __( 'The maximum is %1$s %2$s.', 'cut-and-pack-for-woocommerce' ),
     198                                        'max'          => __( 'The maximum is %1$s %2$s.', 'cut-and-pack-for-woocommerce' ),
    146199                                ),
    147200                        )
  • cut-and-pack-for-woocommerce/tags/1.1.0/includes/class-cutpack-settings.php

    r3800171 r3805284  
    6666         */
    6767        public static function get( $product ) {
    68                 $product  = is_numeric( $product ) ? wc_get_product( $product ) : $product;
     68                $product = is_numeric( $product ) ? wc_get_product( $product ) : $product;
     69
     70                /**
     71                 * Filter a product's Cut & Pack settings.
     72                 *
     73                 * @param array                     $settings  Settings.
     74                 * @param WC_Product|null           $product   Product (the parent for a variation).
     75                 * @param WC_Product_Variation|null $variation Variation, when settings are read for one.
     76                 */
     77                return apply_filters( 'cutpack_product_settings', self::read( $product ), $product, null );
     78        }
     79
     80        /**
     81         * Settings stored on a product, cleaned but not filtered.
     82         *
     83         * @param WC_Product|null $product Product.
     84         * @return array
     85         */
     86        private static function read( $product ) {
    6987                $settings = self::defaults();
    7088
    … …  
    7896                }
    7997
    80                 /**
    81                  * Filter a product's Cut & Pack settings.
    82                  *
    83                  * @param array           $settings Settings.
    84                  * @param WC_Product|null $product  Product.
    85                  */
    86                 return apply_filters( 'cutpack_product_settings', self::normalise( $settings ), $product );
     98                return self::normalise( $settings );
     99        }
     100
     101        /**
     102         * Settings for a product, or for one of its variations.
     103         *
     104         * A variation may override the pack coverage; everything else comes from
     105         * the parent product. A variation that does not belong to the product is ignored.
     106         *
     107         * @param int $product_id   Parent (or simple) product ID.
     108         * @param int $variation_id Variation ID, or 0.
     109         * @return array
     110         */
     111        public static function get_for( $product_id, $variation_id = 0 ) {
     112                $product = wc_get_product( absint( $product_id ) );
     113                if ( ! $product instanceof WC_Product ) {
     114                        return self::get( null );
     115                }
     116
     117                $variation = self::owned_variation( $product, $variation_id );
     118                if ( ! $variation ) {
     119                        return self::get( $product );
     120                }
     121
     122                $settings = self::read( $product );
     123                $override = self::to_positive_decimal( $variation->get_meta( self::meta_key( 'coverage' ), true ) );
     124                if ( '' !== $override && self::MODE_PACKS === $settings['mode'] && self::MEASURE_AREA === $settings['measure'] ) {
     125                        $settings['coverage'] = $override;
     126                }
     127
     128                /** This filter is documented in get(). */
     129                return apply_filters( 'cutpack_product_settings', self::normalise( $settings ), $product, $variation );
     130        }
     131
     132        /**
     133         * The variation with this ID, but only if it really belongs to the product.
     134         *
     135         * @param WC_Product $product      Parent product.
     136         * @param int        $variation_id Variation ID.
     137         * @return WC_Product_Variation|null
     138         */
     139        public static function owned_variation( $product, $variation_id ) {
     140                $variation_id = absint( $variation_id );
     141                if ( ! $variation_id || ! $product instanceof WC_Product || ! $product->is_type( 'variable' ) ) {
     142                        return null;
     143                }
     144                $variation = wc_get_product( $variation_id );
     145                if ( $variation instanceof WC_Product && $variation->is_type( 'variation' ) && (int) $variation->get_parent_id() === (int) $product->get_id() ) {
     146                        return $variation;
     147                }
     148                return null;
    87149        }
    88150
    … …  
    90152         * Whether the calculator is switched on for a product.
    91153         *
    92          * Version 1 supports simple products only.
     154         * Works for simple and variable products. A variation counts as enabled
     155         * when its parent is.
    93156         *
    94157         * @param WC_Product|int $product Product or product ID.
    … …  
    97160        public static function is_enabled( $product ) {
    98161                $product = is_numeric( $product ) ? wc_get_product( $product ) : $product;
    99                 if ( ! $product instanceof WC_Product || ! $product->is_type( 'simple' ) ) {
     162                if ( $product instanceof WC_Product && $product->is_type( 'variation' ) ) {
     163                        $product = wc_get_product( $product->get_parent_id() );
     164                }
     165                if ( ! $product instanceof WC_Product || ! ( $product->is_type( 'simple' ) || $product->is_type( 'variable' ) ) ) {
    100166                        return false;
    101167                }
  • cut-and-pack-for-woocommerce/tags/1.1.0/languages/cut-and-pack-for-woocommerce.pot

    r3800171 r3805284  
    1 # Copyright (C) 2026 Cut & Pack
     1# Copyright (C) 2026 Cartwright
    22# This file is distributed under the GPL-2.0-or-later.
    33msgid ""
    44msgstr ""
    5 "Project-Id-Version: Cut & Pack for WooCommerce 0.1.0\n"
     5"Project-Id-Version: Cut & Pack for WooCommerce 1.0.0\n"
    66"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/cut-and-pack-for-woocommerce\n"
    77"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
    … …  
    1010"Content-Type: text/plain; charset=UTF-8\n"
    1111"Content-Transfer-Encoding: 8bit\n"
    12 "POT-Creation-Date: 2026-10-29T17:11:31+00:00\n"
     12"POT-Creation-Date: 2026-11-13T09:29:09+00:00\n"
    1313"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
    1414"X-Generator: WP-CLI 2.12.0\n"
    … …  
    2727#. Author of the plugin
    2828#: cut-and-pack-for-woocommerce.php
    29 #: includes/class-cutpack-admin.php:39
     29msgid "Cartwright"
     30msgstr ""
     31
     32#. Author URI of the plugin
     33#: cut-and-pack-for-woocommerce.php
     34msgid "https://cartwrightplugins.com/"
     35msgstr ""
     36
     37#: includes/class-cutpack-admin.php:41
    3038msgid "Cut & Pack"
    3139msgstr ""
    3240
    33 #: includes/class-cutpack-admin.php:41
     41#: includes/class-cutpack-admin.php:43
    3442msgid "Switch the calculator on for each product in its \"Made to measure\" tab."
    3543msgstr ""
    3644
    37 #: includes/class-cutpack-admin.php:45
     45#: includes/class-cutpack-admin.php:47
    3846msgid "Remove data on uninstall"
    3947msgstr ""
    4048
    41 #: includes/class-cutpack-admin.php:46
     49#: includes/class-cutpack-admin.php:48
    4250msgid "Delete all Cut & Pack product settings when the plugin is deleted. Measurements on past orders are always kept."
    4351msgstr ""
    4452
    45 #: includes/class-cutpack-admin.php:66
     53#: includes/class-cutpack-admin.php:68
    4654msgid "Settings"
    4755msgstr ""
    4856
    49 #: includes/class-cutpack-admin.php:78
     57#: includes/class-cutpack-admin.php:80
    5058msgid "Made to measure"
    5159msgstr ""
    5260
    53 #: includes/class-cutpack-admin.php:102
     61#: includes/class-cutpack-admin.php:104
    5462msgid "Enable calculator"
    5563msgstr ""
    5664
    57 #: includes/class-cutpack-admin.php:103
     65#: includes/class-cutpack-admin.php:105
    5866msgid "Shoppers enter a size instead of a quantity, and the price is calculated for them."
    5967msgstr ""
    6068
    61 #: includes/class-cutpack-admin.php:112
     69#: includes/class-cutpack-admin.php:114
    6270msgid "Mode"
    6371msgstr ""
    6472
    65 #: includes/class-cutpack-admin.php:114
     73#: includes/class-cutpack-admin.php:116
    6674msgid "Sold in packs (e.g. tiles, flooring)"
    6775msgstr ""
    6876
    69 #: includes/class-cutpack-admin.php:115
     77#: includes/class-cutpack-admin.php:117
    7078msgid "Cut to size (e.g. fabric, blinds)"
    7179msgstr ""
    7280
    73 #: includes/class-cutpack-admin.php:119
     81#: includes/class-cutpack-admin.php:121
    7482msgid "Packs: the order is rounded up to whole boxes. Cut to size: the shopper pays for exactly the amount entered."
    7583msgstr ""
    7684
    77 #: includes/class-cutpack-admin.php:126
     85#: includes/class-cutpack-admin.php:128
    7886msgid "Sold by"
    7987msgstr ""
    8088
    81 #: includes/class-cutpack-admin.php:128
     89#: includes/class-cutpack-admin.php:130
    8290msgid "Area (m²)"
    8391msgstr ""
    8492
    85 #: includes/class-cutpack-admin.php:129
     93#: includes/class-cutpack-admin.php:131
    8694msgid "Length (m)"
    8795msgstr ""
    8896
    89 #: includes/class-cutpack-admin.php:138
     97#: includes/class-cutpack-admin.php:140
    9098msgid "Shopper enters"
    9199msgstr ""
    92100
    93 #: includes/class-cutpack-admin.php:140
     101#: includes/class-cutpack-admin.php:142
    94102msgid "Total area"
    95103msgstr ""
    96104
    97 #: includes/class-cutpack-admin.php:141
     105#: includes/class-cutpack-admin.php:143
    98106msgid "Length × width"
    99107msgstr ""
    100108
    101 #: includes/class-cutpack-admin.php:149
     109#: includes/class-cutpack-admin.php:152
     110msgid "The price comes from each variation's own Regular price, read as a price per m² or per metre."
     111msgstr ""
     112
     113#: includes/class-cutpack-admin.php:154
    102114msgid "The price comes from the normal Regular price field on the General tab, read as a price per m² or per metre."
    103115msgstr ""
    104116
    105117#. translators: %s: unit, e.g. m²
    106 #: includes/class-cutpack-admin.php:158
     118#: includes/class-cutpack-admin.php:164
    107119#, php-format
    108120msgid "Coverage per pack (%s)"
    109121msgstr ""
    110122
    111 #: includes/class-cutpack-admin.php:163
     123#: includes/class-cutpack-admin.php:169
    112124msgid "How much one box covers. Stock is counted in boxes."
    113125msgstr ""
    114126
    115127#. translators: %s: unit, e.g. m²
    116 #: includes/class-cutpack-admin.php:172
     128#: includes/class-cutpack-admin.php:178
    117129#, php-format
    118130msgid "Minimum (%s)"
    119131msgstr ""
    120132
    121 #: includes/class-cutpack-admin.php:176
     133#: includes/class-cutpack-admin.php:182
    122134msgid "Smallest amount a shopper can order. Leave empty for no minimum."
    123135msgstr ""
    124136
    125137#. translators: %s: unit, e.g. m²
    126 #: includes/class-cutpack-admin.php:184
     138#: includes/class-cutpack-admin.php:190
    127139#, php-format
    128140msgid "Maximum (%s)"
    129141msgstr ""
    130142
    131 #: includes/class-cutpack-admin.php:188
     143#: includes/class-cutpack-admin.php:194
    132144msgid "Largest amount a shopper can order. Leave empty for no maximum."
    133145msgstr ""
    134146
    135147#. translators: %s: unit, e.g. m
    136 #: includes/class-cutpack-admin.php:196
     148#: includes/class-cutpack-admin.php:202
    137149#, php-format
    138150msgid "Step (%s)"
    139151msgstr ""
    140152
    141 #: includes/class-cutpack-admin.php:201
     153#: includes/class-cutpack-admin.php:207
    142154msgid "Amounts are rounded up to this step, e.g. 0.1 turns 2.34 into 2.4."
    143155msgstr ""
    144156
    145 #: includes/class-cutpack-admin.php:209
     157#: includes/class-cutpack-admin.php:215
    146158msgid "Label text"
    147159msgstr ""
    148160
    149 #: includes/class-cutpack-admin.php:211
    150 #: includes/class-cutpack-frontend.php:59
     161#: includes/class-cutpack-admin.php:217
     162#: includes/class-cutpack-frontend.php:61
    151163msgid "How many m² do you need?"
    152164msgstr ""
    153165
    154 #: includes/class-cutpack-admin.php:213
     166#: includes/class-cutpack-admin.php:219
    155167msgid "Optional wording shown above the calculator."
    156168msgstr ""
    157169
    158 #: includes/class-cutpack-admin.php:273
     170#. translators: %s: unit
     171#: includes/class-cutpack-admin.php:246
     172#, php-format
     173msgid "Pack size: coverage per pack (%s)"
     174msgstr ""
     175
     176#: includes/class-cutpack-admin.php:248
     177msgid "Same as the product"
     178msgstr ""
     179
     180#: includes/class-cutpack-admin.php:249
     181msgid "Only for packs. Leave empty to use the number from the \"Made to measure\" tab."
     182msgstr ""
     183
     184#: includes/class-cutpack-admin.php:319
    159185msgid "Enter how much one pack covers."
    160186msgstr ""
    161187
    162 #: includes/class-cutpack-admin.php:277
     188#: includes/class-cutpack-admin.php:323
    163189msgid "The minimum is larger than the maximum."
    164190msgstr ""
    165191
    166 #: includes/class-cutpack-admin.php:281
     192#: includes/class-cutpack-admin.php:328
    167193msgid "Enter a Regular price on the General tab (price per m² or per metre)."
    168194msgstr ""
    169195
    170 #: includes/class-cutpack-admin.php:287
     196#: includes/class-cutpack-admin.php:334
    171197msgid "Cut & Pack: the calculator was switched off for this product."
    172198msgstr ""
    173199
    174200#: includes/class-cutpack-calculator.php:45
    175 #: includes/class-cutpack-frontend.php:141
     201#: includes/class-cutpack-frontend.php:194
    176202msgid "Please enter a length and a width greater than zero."
    177203msgstr ""
    … …  
    183209#. translators: 1: minimum amount, 2: unit such as m²
    184210#: includes/class-cutpack-calculator.php:61
    185 #: includes/class-cutpack-frontend.php:143
     211#: includes/class-cutpack-frontend.php:196
    186212#, php-format
    187213msgid "The minimum is %1$s %2$s."
    … …  
    190216#. translators: 1: maximum amount, 2: unit such as m²
    191217#: includes/class-cutpack-calculator.php:69
    192 #: includes/class-cutpack-frontend.php:145
     218#: includes/class-cutpack-frontend.php:198
    193219#, php-format
    194220msgid "The maximum is %1$s %2$s."
    … …  
    199225msgstr ""
    200226
     227#: includes/class-cutpack-cart.php:135
     228#: includes/class-cutpack-frontend.php:189
     229msgid "Choose an option first."
     230msgstr ""
     231
    201232#. translators: %s: product name
    202 #: includes/class-cutpack-cart.php:206
     233#: includes/class-cutpack-cart.php:282
    203234#, php-format
    204235msgid "%s was removed from your cart. Please enter the size you need on the product page and add it again."
    205236msgstr ""
    206237
    207 #: includes/class-cutpack-cart.php:227
     238#: includes/class-cutpack-cart.php:303
    208239msgid "Coverage"
    209240msgstr ""
    210241
    211242#. translators: 1: amount, 2: unit, 3: number of boxes. Example: 24.48 m² (17 boxes)
    212 #: includes/class-cutpack-cart.php:230
     243#: includes/class-cutpack-cart.php:306
    213244#, php-format
    214245msgid "%1$s %2$s (%3$s box)"
    … …  
    217248msgstr[1] ""
    218249
    219 #: includes/class-cutpack-cart.php:238
    220 #: includes/class-cutpack-cart.php:246
     250#: includes/class-cutpack-cart.php:314
     251#: includes/class-cutpack-cart.php:322
    221252msgid "You entered"
    222253msgstr ""
    223254
    224 #: includes/class-cutpack-cart.php:242
     255#: includes/class-cutpack-cart.php:318
    225256msgid "Length"
    226257msgstr ""
    227258
    228 #: includes/class-cutpack-cart.php:242
     259#: includes/class-cutpack-cart.php:318
    229260msgid "Area"
    230261msgstr ""
    231262
    232263#. translators: abbreviation for metres
    233 #: includes/class-cutpack-cart.php:267
    234 #: includes/class-cutpack-settings.php:171
     264#: includes/class-cutpack-cart.php:343
     265#: includes/class-cutpack-settings.php:237
    235266msgid "m"
    236267msgstr ""
    237268
    238 #: includes/class-cutpack-cart.php:346
     269#: includes/class-cutpack-cart.php:422
    239270msgid "Enter size"
    240271msgstr ""
    241272
    242 #: includes/class-cutpack-frontend.php:55
     273#: includes/class-cutpack-frontend.php:57
    243274msgid "Enter the size of your area"
    244275msgstr ""
    245276
    246 #: includes/class-cutpack-frontend.php:57
     277#: includes/class-cutpack-frontend.php:59
    247278msgid "How many metres do you need?"
    248279msgstr ""
    249280
    250281#. translators: %s: unit
    251 #: includes/class-cutpack-frontend.php:71
     282#: includes/class-cutpack-frontend.php:73
    252283#, php-format
    253284msgid "Length (%s)"
    … …  
    255286
    256287#. translators: %s: unit
    257 #: includes/class-cutpack-frontend.php:76
     288#: includes/class-cutpack-frontend.php:78
    258289#, php-format
    259290msgid "Width (%s)"
    … …  
    261292
    262293#. translators: 1: number of boxes, 2: amount, 3: unit, 4: price. Example: You need 17 boxes (24.48 m²) – £976.75
    263 #: includes/class-cutpack-frontend.php:133
     294#: includes/class-cutpack-frontend.php:182
    264295#, php-format
    265296msgid "You need %1$s (%2$s %3$s) – %4$s"
    … …  
    267298
    268299#. translators: 1: amount, 2: unit, 3: price. Example: 2.4 m – £43.20
    269 #: includes/class-cutpack-frontend.php:135
     300#: includes/class-cutpack-frontend.php:184
    270301#, php-format
    271302msgid "%1$s %2$s – %3$s"
    … …  
    273304
    274305#. translators: %s: a number
    275 #: includes/class-cutpack-frontend.php:137
     306#: includes/class-cutpack-frontend.php:186
    276307#, php-format
    277308msgid "%s box"
    … …  
    279310
    280311#. translators: %s: a number
    281 #: includes/class-cutpack-frontend.php:139
     312#: includes/class-cutpack-frontend.php:188
    282313#, php-format
    283314msgid "%s boxes"
    284315msgstr ""
    285316
    286 #: includes/class-cutpack-frontend.php:140
     317#: includes/class-cutpack-frontend.php:190
     318msgid "This option is unavailable."
     319msgstr ""
     320
     321#. translators: 1: number of boxes in stock, 2: amount covered, 3: unit such as m²
     322#: includes/class-cutpack-frontend.php:192
     323#, php-format
     324msgid "Only %1$s in stock (%2$s %3$s)."
     325msgstr ""
     326
     327#: includes/class-cutpack-frontend.php:193
    287328msgid "Please enter a number greater than zero."
    288329msgstr ""
    289330
    290331#. translators: abbreviation for square metres
    291 #: includes/class-cutpack-settings.php:173
     332#: includes/class-cutpack-settings.php:239
    292333msgid "m²"
    293334msgstr ""
  • cut-and-pack-for-woocommerce/tags/1.1.0/readme.txt

    r3800171 r3805284  
    55Tested up to: 7.1
    66Requires PHP: 7.4
    7 Stable tag: 1.0.0
     7Stable tag: 1.1.0
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
    … …  
    47471. Install and activate WooCommerce.
    48482. Go to Plugins → Add New, search for "Cut & Pack", then install and activate it.
    49 3. Edit a simple product. On the General tab, enter the price per m² (or per metre) as the Regular price.
     493. Edit a simple or variable product and enter the price per m² (or per metre) as the Regular price (on a variable product, on each variation).
    50504. Open the new "Made to measure" tab, tick "Enable calculator" and choose the mode.
    51515. For packs, enter how much one box covers (for example 1.44 m²).
    … …  
    5656= Which product types are supported? =
    5757
    58 Simple products. Support for variable products (for example one floor in several finishes) is planned.
     58Simple and variable products. On a variable product (for example one floor in several finishes) each variation has its own price per m², its own stock and, if you like, its own pack size. Grouped and external products are not supported.
     59
     60= Can each variation have a different pack size? =
     61
     62Yes. Switch the calculator on in the product's "Made to measure" tab and enter the usual pack size. In each variation's panel you can enter a different "Pack size"; leave it empty to use the product's. Stock is counted in boxes, so if a finish has 5 boxes left the shopper is told so instead of being able to order more.
     63
     64= What does "Order again" do with measured items? =
     65
     66Measured items are not added again automatically, because the size has to be entered again. The shopper opens the product page and enters the size.
    5967
    6068= What price do I enter? =
    … …  
    94102== Changelog ==
    95103
     104= 1.1.0 =
     105* New: variable products. Each variation can have its own price per m² (or per metre), its own stock and, optionally, its own pack size.
     106* New: a finish that has only a few boxes left tells the shopper how many boxes can be ordered.
     107* Developers: the `cutpack_product_settings` filter now receives the parent product as its second argument and, for a variation, the variation as a third argument.
     108
    96109= 1.0.0 =
    97110* First release.
    … …  
    99112== Upgrade Notice ==
    100113
     114= 1.1.0 =
     115Adds support for variable products (different price, stock and pack size per variation).
     116
    101117= 1.0.0 =
    102118First release.
  • cut-and-pack-for-woocommerce/trunk/assets/js/admin.js

    r3800171 r3805284  
    1313
    1414                $( panel + ' .cutpack-show-if-packs' ).toggle( 'packs' === mode );
     15                $( '.cutpack-variation-field' ).toggle( 'packs' === mode && 'area' === measure );
    1516                $( panel + ' .cutpack-show-if-cut' ).toggle( 'cut' === mode );
    1617                $( panel + ' .cutpack-show-if-area' ).toggle( 'area' === measure );
    … …  
    2728        $( function () {
    2829                $( document ).on( 'change', '#_cutpack_mode, #_cutpack_measure', refresh );
     30                $( document ).on( 'woocommerce_variations_loaded woocommerce_variations_added', refresh );
    2931                refresh();
    3032        } );
  • cut-and-pack-for-woocommerce/trunk/assets/js/calculator.js

    r3800171 r3805284  
    1919                return;
    2020        }
    21         var form = box.closest( 'form.cart' );
     21        var form = box.closest( 'form' );
    2222        if ( ! form ) {
    2323                return;
    … …  
    2525
    2626        var qty      = form.querySelector( 'input[name="quantity"]' );
    27         var button   = form.querySelector( '.single_add_to_cart_button' );
     27        var button   = form.querySelector( '[type="submit"], .single_add_to_cart_button' );
    2828        var inputs   = box.querySelectorAll( '.cutpack-input' );
    2929        var resultEl = box.querySelector( '.cutpack-result' );
    … …  
    6868        }
    6969
    70         function calculate() {
     70        /**
     71         * What applies right now: the product itself, or the chosen variation.
     72         * The variation id is read from the form every time, never cached.
     73         */
     74        function current() {
     75                if ( ! d.variable ) {
     76                        return { ok: true, coverage: d.coverage, unitPrice: d.unitPrice, maxQty: -1 };
     77                }
     78                var field = form.querySelector( 'input[name="variation_id"]' );
     79                var id    = field ? parseInt( field.value, 10 ) || 0 : 0;
     80                if ( ! id ) {
     81                        return { ok: false, id: 0, message: d.i18n.chooseOption };
     82                }
     83                var v = d.variations[ id ];
     84                if ( ! v || ! v.purchasable ) {
     85                        return { ok: false, id: id, message: d.i18n.unavailable };
     86                }
     87                return { ok: true, id: id, coverage: v.coverage, unitPrice: v.unitPrice, maxQty: v.maxQty };
     88        }
     89
     90        function calculate( cur ) {
    7191                var entered;
    7292
    … …  
    93113
    94114                if ( 'packs' === d.mode ) {
    95                         var packs   = Math.max( 1, Math.ceil( entered / d.coverage - EPSILON ) );
    96                         var charged = round( packs * d.coverage, 6 );
     115                        var packs   = Math.max( 1, Math.ceil( entered / cur.coverage - EPSILON ) );
     116                        var charged = round( packs * cur.coverage, 6 );
    97117                        // Price of one box is rounded to whole pennies first, as on the server.
    98                         var boxPrice = round( d.coverage * d.unitPrice, d.currency.decimals );
     118                        var boxPrice = round( cur.coverage * cur.unitPrice, d.currency.decimals );
     119                        if ( cur.maxQty >= 0 && packs > cur.maxQty ) {
     120                                var inStock = format( 1 === cur.maxQty ? d.i18n.oneBox : d.i18n.manyBoxes, cur.maxQty );
     121                                return { error: format( d.i18n.stock, inStock, formatAmount( cur.maxQty * cur.coverage ), d.unit ) };
     122                        }
    99123                        return { packs: packs, charged: charged, price: packs * boxPrice };
    100124                }
    101125
    102126                var amount = d.step > 0 ? round( Math.ceil( entered / d.step - EPSILON ) * d.step, 6 ) : entered;
    103                 return { packs: 1, charged: amount, price: round( amount * d.unitPrice, d.currency.decimals ) };
     127                return { packs: 1, charged: amount, price: round( amount * cur.unitPrice, d.currency.decimals ) };
    104128        }
    105129
    … …  
    121145        }
    122146
     147        /** Write only when the text changes, so screen readers don't repeat themselves. */
     148        function setText( el, text ) {
     149                if ( el.textContent !== text ) {
     150                        el.textContent = text;
     151                }
     152        }
     153
     154        /** The quantity box is hidden, so the browser must never block the form over it. */
     155        function relaxQuantity() {
     156                if ( qty ) {
     157                        qty.removeAttribute( 'min' );
     158                        qty.removeAttribute( 'max' );
     159                        qty.removeAttribute( 'step' );
     160                }
     161        }
     162
     163        /** The calculator replaces the normal quantity box. WooCommerce may show it again, so hide it on every update. */
     164        function hideQuantity() {
     165                var wrap = qty && button ? qty.closest( '.quantity' ) : null;
     166                if ( wrap ) {
     167                        wrap.style.setProperty( 'display', 'none', 'important' );
     168                }
     169        }
     170
    123171        function update() {
     172                hideQuantity();
     173                var cur = current();
     174                if ( ! cur.ok ) {
     175                        setText( resultEl, cur.message );
     176                        setText( errorEl, '' );
     177                        setButton( false );
     178                        return;
     179                }
     180
    124181                if ( isEmpty() ) {
    125                         resultEl.textContent = '';
    126                         errorEl.textContent  = '';
    127                         setButton( false );
    128                         return;
    129                 }
    130 
    131                 var r = calculate();
     182                        setText( resultEl, '' );
     183                        setText( errorEl, '' );
     184                        setButton( false );
     185                        return;
     186                }
     187
     188                var r = calculate( cur );
    132189
    133190                if ( r.error ) {
    134                         resultEl.textContent = '';
    135                         errorEl.textContent  = r.error;
    136                         setButton( false );
    137                         return;
    138                 }
    139 
    140                 errorEl.textContent = '';
     191                        setText( resultEl, '' );
     192                        setText( errorEl, r.error );
     193                        setButton( false );
     194                        return;
     195                }
     196
     197                setText( errorEl, '' );
    141198
    142199                if ( 'packs' === d.mode ) {
    143200                        var boxes = format( 1 === r.packs ? d.i18n.oneBox : d.i18n.manyBoxes, r.packs );
    144                         resultEl.textContent = format( d.i18n.resultPacks, boxes, formatAmount( r.charged ), d.unit, formatPrice( r.price ) );
     201                        setText( resultEl, format( d.i18n.resultPacks, boxes, formatAmount( r.charged ), d.unit, formatPrice( r.price ) ) );
    145202                        if ( qty ) {
    146203                                qty.value = r.packs;
    147204                        }
    148205                } else {
    149                         resultEl.textContent = format( d.i18n.resultCut, formatAmount( r.charged ), d.unit, formatPrice( r.price ) );
     206                        setText( resultEl, format( d.i18n.resultCut, formatAmount( r.charged ), d.unit, formatPrice( r.price ) ) );
    150207                        if ( qty ) {
    151208                                qty.value = 1;
    … …  
    153210                }
    154211
     212                relaxQuantity();
    155213                setButton( true );
    156214        }
    157215
    158         // The calculator replaces the normal quantity box.
    159         if ( qty ) {
    160                 var wrap = qty.closest( '.quantity' );
    161                 if ( wrap ) {
    162                         wrap.style.display = 'none';
    163                 }
     216        /** Re-run after WooCommerce (or the block) has finished its own changes. */
     217        function later() {
     218                window.setTimeout( function () {
     219                        update();
     220                        if ( window.requestAnimationFrame ) {
     221                                window.requestAnimationFrame( update );
     222                        }
     223                }, 0 );
    164224        }
    165225
    … …  
    168228        }
    169229
     230        if ( d.variable ) {
     231                // Any interaction with the form may change the chosen variation.
     232                [ 'click', 'change', 'input', 'keyup' ].forEach( function ( name ) {
     233                        form.addEventListener( name, later );
     234                } );
     235
     236                // WooCommerce's own events, when jQuery is on the page.
     237                if ( window.jQuery ) {
     238                        window.jQuery( form ).on( 'found_variation show_variation hide_variation reset_data check_variations', later );
     239                }
     240
     241                // Backstop for themes that change the chosen option without any event.
     242                var lastId = null;
     243                window.setInterval( function () {
     244                        var now = current();
     245                        var key = String( now.id || 0 ) + ( now.ok ? '1' : '0' );
     246                        if ( key !== lastId ) {
     247                                lastId = key;
     248                                update();
     249                        }
     250                }, 250 );
     251        }
     252
     253        // Drop the hidden quantity's limits just before the browser checks the form.
     254        if ( button ) {
     255                button.addEventListener( 'click', relaxQuantity, true );
     256        }
     257
    170258        // Stop the form being sent with an invalid size (e.g. by pressing Enter).
    171259        form.addEventListener( 'submit', function ( event ) {
    172                 var r = isEmpty() ? { error: d.i18n.invalid } : calculate();
     260                var cur = current();
     261                var r   = ! cur.ok ? { error: cur.message } : ( isEmpty() ? { error: d.i18n.invalid } : calculate( cur ) );
     262                relaxQuantity();
    173263                if ( r.error ) {
    174264                        event.preventDefault();
    175                         errorEl.textContent = r.error;
     265                        setText( errorEl, r.error );
    176266                        setButton( false );
    177267                }
  • cut-and-pack-for-woocommerce/trunk/cut-and-pack-for-woocommerce.php

    r3800171 r3805284  
    33 * Plugin Name:          Cut & Pack for WooCommerce
    44 * Description:          Made-to-measure pricing for WooCommerce. Sell by m² or metre, round up to whole packs, and show the price live.
    5  * Version:              1.0.0
     5 * Version:              1.1.0
    66 * Requires at least:    6.9
    77 * Requires PHP:         7.4
    … …  
    2121defined( 'ABSPATH' ) || exit;
    2222
    23 define( 'CUTPACK_VERSION', '1.0.0' );
     23define( 'CUTPACK_VERSION', '1.1.0' );
    2424define( 'CUTPACK_FILE', __FILE__ );
    2525define( 'CUTPACK_PATH', plugin_dir_path( __FILE__ ) );
  • cut-and-pack-for-woocommerce/trunk/includes/class-cutpack-admin.php

    r3800171 r3805284  
    2020                add_action( 'woocommerce_product_data_panels', array( __CLASS__, 'render_panel' ) );
    2121                add_action( 'woocommerce_admin_process_product_object', array( __CLASS__, 'save' ) );
     22                add_action( 'woocommerce_product_after_variable_attributes', array( __CLASS__, 'render_variation_field' ), 10, 3 );
     23                add_action( 'woocommerce_admin_process_variation_object', array( __CLASS__, 'save_variation' ), 10, 2 );
    2224                add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue' ) );
    2325                add_filter( 'woocommerce_get_settings_products', array( __CLASS__, 'store_settings' ), 10, 2 );
    … …  
    7880                        'label'    => __( 'Made to measure', 'cut-and-pack-for-woocommerce' ),
    7981                        'target'   => 'cutpack_product_data',
    80                         'class'    => array( 'show_if_simple', 'hide_if_variable', 'hide_if_grouped', 'hide_if_external' ),
     82                        'class'    => array( 'show_if_simple', 'show_if_variable', 'hide_if_grouped', 'hide_if_external' ),
    8183                        'priority' => 65,
    8284                );
    … …  
    147149
    148150                echo '<p class="form-field cutpack-price-note"><span class="description">';
    149                 echo esc_html__( 'The price comes from the normal Regular price field on the General tab, read as a price per m² or per metre.', 'cut-and-pack-for-woocommerce' );
     151                if ( $product_object instanceof WC_Product && $product_object->is_type( 'variable' ) ) {
     152                        echo esc_html__( 'The price comes from each variation\'s own Regular price, read as a price per m² or per metre.', 'cut-and-pack-for-woocommerce' );
     153                } else {
     154                        echo esc_html__( 'The price comes from the normal Regular price field on the General tab, read as a price per m² or per metre.', 'cut-and-pack-for-woocommerce' );
     155                }
    150156                echo '</span></p>';
    151157
    … …  
    227233
    228234        /**
     235         * Optional per-variation pack size, shown in each variation's panel.
     236         *
     237         * @param int     $loop           Position in the loop.
     238         * @param array   $variation_data Variation data.
     239         * @param WP_Post $variation      Variation post.
     240         */
     241        public static function render_variation_field( $loop, $variation_data, $variation ) {
     242                $value = get_post_meta( $variation->ID, Cutpack_Settings::meta_key( 'coverage' ), true );
     243                ?>
     244                <div class="cutpack-variation-field form-row form-row-full">
     245                        <label for="cutpack_variation_coverage_<?php echo esc_attr( $loop ); ?>">
     246                                <?php echo esc_html( sprintf( /* translators: %s: unit */ __( 'Pack size: coverage per pack (%s)', 'cut-and-pack-for-woocommerce' ), Cutpack_Settings::unit_label( Cutpack_Settings::MEASURE_AREA ) ) ); ?>
     247                        </label>
     248                        <input type="text" inputmode="decimal" class="short" id="cutpack_variation_coverage_<?php echo esc_attr( $loop ); ?>" name="cutpack_variation_coverage[<?php echo esc_attr( $loop ); ?>]" value="<?php echo esc_attr( $value ); ?>" placeholder="<?php echo esc_attr__( 'Same as the product', 'cut-and-pack-for-woocommerce' ); ?>" />
     249                        <span class="description"><?php esc_html_e( 'Only for packs. Leave empty to use the number from the "Made to measure" tab.', 'cut-and-pack-for-woocommerce' ); ?></span>
     250                </div>
     251                <?php
     252        }
     253
     254        /**
     255         * Save the per-variation pack size. WooCommerce has already checked the nonce.
     256         *
     257         * @param WC_Product_Variation $variation Variation being saved.
     258         * @param int                  $i         Position in the posted list.
     259         */
     260        public static function save_variation( $variation, $i ) {
     261                // phpcs:disable WordPress.Security.NonceVerification.Missing -- Nonce checked by WooCommerce before this hook runs.
     262                if ( ! isset( $_POST['cutpack_variation_coverage'][ $i ] ) || is_array( $_POST['cutpack_variation_coverage'][ $i ] ) ) {
     263                        return;
     264                }
     265                $raw = sanitize_text_field( wp_unslash( $_POST['cutpack_variation_coverage'][ $i ] ) );
     266                // phpcs:enable
     267
     268                if ( ! current_user_can( 'edit_post', $variation->get_parent_id() ) ) {
     269                        return;
     270                }
     271                $variation->update_meta_data( Cutpack_Settings::meta_key( 'coverage' ), Cutpack_Settings::to_positive_decimal( $raw ) );
     272        }
     273
     274        /**
    229275         * Save the tab's fields. WooCommerce has already checked the nonce and permissions.
    230276         *
    … …  
    278324                }
    279325
    280                 if ( '' === $product->get_regular_price() ) {
     326                // A variable product keeps its prices on the variations.
     327                if ( ! $product->is_type( 'variable' ) && '' === $product->get_regular_price() ) {
    281328                        $problems[] = __( 'Enter a Regular price on the General tab (price per m² or per metre).', 'cut-and-pack-for-woocommerce' );
    282329                }
  • cut-and-pack-for-woocommerce/trunk/includes/class-cutpack-cart.php

    r3800171 r3805284  
    3535
    3636        /**
     37         * Variation chosen for each product during this add-to-cart request, as
     38         * seen by the validation filter (which WooCommerce gives the variation,
     39         * even when the form only posted attributes).
     40         *
     41         * @var int[]
     42         */
     43        private static $chosen = array();
     44
     45        /**
    3746         * Register hooks.
    3847         */
    3948        public static function init() {
    40                 add_filter( 'woocommerce_add_to_cart_validation', array( __CLASS__, 'validate' ), 10, 2 );
    41                 add_filter( 'woocommerce_add_to_cart_quantity', array( __CLASS__, 'quantity' ), 10, 2 );
    42                 add_filter( 'woocommerce_add_cart_item_data', array( __CLASS__, 'add_item_data' ), 10, 2 );
    43 
     49                add_filter( 'woocommerce_add_to_cart_validation', array( __CLASS__, 'validate' ), 10, 4 );
     50                add_filter( 'woocommerce_add_to_cart_quantity', array( __CLASS__, 'quantity' ), 10, 3 );
     51                add_filter( 'woocommerce_add_cart_item_data', array( __CLASS__, 'add_item_data' ), 10, 3 );
     52
     53                add_filter( 'woocommerce_add_cart_item', array( __CLASS__, 'lock_quantity' ), 15 );
    4454                add_filter( 'woocommerce_add_cart_item', array( __CLASS__, 'apply_price' ), 20 );
    4555                add_filter( 'woocommerce_get_cart_item_from_session', array( __CLASS__, 'apply_price' ), 20 );
    … …  
    7585
    7686        /**
     87         * Work out the parent product and variation being added.
     88         *
     89         * WooCommerce passes the variation ID to some filters but not all, and
     90         * `?add-to-cart=<variation id>` passes the variation as the product.
     91         * Every server path resolves it here so they cannot disagree.
     92         *
     93         * @param int $product_id   Product ID as received by the filter.
     94         * @param int $variation_id Variation ID as received by the filter, or 0.
     95         * @return int[] array( product ID, variation ID ).
     96         */
     97        private static function resolve_ids( $product_id, $variation_id = 0 ) {
     98                $product_id   = absint( $product_id );
     99                $variation_id = absint( $variation_id );
     100
     101                $product = wc_get_product( $product_id );
     102                if ( $product instanceof WC_Product && $product->is_type( 'variation' ) ) {
     103                        return array( (int) $product->get_parent_id(), $product_id );
     104                }
     105
     106                if ( ! $variation_id && isset( self::$chosen[ $product_id ] ) ) {
     107                        return array( $product_id, self::$chosen[ $product_id ] );
     108                }
     109
     110                if ( ! $variation_id && isset( $_REQUEST['variation_id'] ) && is_scalar( $_REQUEST['variation_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- WooCommerce add-to-cart forms carry no nonce; the ID is only used after checking it belongs to the product.
     111                        $variation_id = absint( wp_unslash( $_REQUEST['variation_id'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
     112                }
     113
     114                return array( $product_id, $variation_id );
     115        }
     116
     117        /**
    77118         * Calculate the posted input for a product.
    78119         *
    79          * @param int $product_id Product ID.
     120         * @param int $product_id   Product ID.
     121         * @param int $variation_id Variation ID, or 0.
    80122         * @return array|WP_Error|null Null if the product doesn't use the calculator.
    81123         */
    82         private static function posted_result( $product_id ) {
     124        private static function posted_result( $product_id, $variation_id = 0 ) {
     125                list( $product_id, $variation_id ) = self::resolve_ids( $product_id, $variation_id );
     126
    83127                $product = wc_get_product( $product_id );
    84128                if ( ! Cutpack_Settings::is_enabled( $product ) ) {
    85129                        return null;
    86130                }
    87                 return Cutpack_Calculator::calculate( Cutpack_Settings::get( $product ), self::posted_input() );
     131
     132                if ( $product->is_type( 'variable' ) ) {
     133                        $variation = Cutpack_Settings::owned_variation( $product, $variation_id );
     134                        if ( ! $variation ) {
     135                                return new WP_Error( 'cutpack_variation', __( 'Choose an option first.', 'cut-and-pack-for-woocommerce' ) );
     136                        }
     137                }
     138
     139                return Cutpack_Calculator::calculate( Cutpack_Settings::get_for( $product_id, $variation_id ), self::posted_input() );
    88140        }
    89141
    … …  
    91143         * Block adding a measured product without a valid size.
    92144         *
    93          * @param bool $passed     Whether earlier checks passed.
    94          * @param int  $product_id Product ID.
     145         * @param bool $passed       Whether earlier checks passed.
     146         * @param int  $product_id   Product ID.
     147         * @param int  $quantity     Quantity (unused).
     148         * @param int  $variation_id Variation ID.
    95149         * @return bool
    96150         */
    97         public static function validate( $passed, $product_id ) {
    98                 $result = self::posted_result( $product_id );
     151        public static function validate( $passed, $product_id, $quantity = 1, $variation_id = 0 ) {
     152                if ( absint( $variation_id ) ) {
     153                        self::$chosen[ absint( $product_id ) ] = absint( $variation_id );
     154                }
     155                $result = self::posted_result( $product_id, $variation_id );
    99156                if ( null === $result || ! $passed ) {
    100157                        return $passed;
    … …  
    111168         * whatever quantity the browser sent.
    112169         *
    113          * @param int $quantity   Quantity sent.
    114          * @param int $product_id Product ID.
     170         * @param int $quantity     Quantity sent.
     171         * @param int $product_id   Product ID.
     172         * @param int $variation_id Variation ID.
    115173         * @return int
    116174         */
    117         public static function quantity( $quantity, $product_id ) {
    118                 $result = self::posted_result( $product_id );
     175        public static function quantity( $quantity, $product_id, $variation_id = 0 ) {
     176                $result = self::posted_result( $product_id, $variation_id );
    119177                if ( null === $result || is_wp_error( $result ) ) {
    120178                        return $quantity;
    … …  
    126184         * Store the measurements on the cart item.
    127185         *
    128          * @param array $data       Cart item data.
    129          * @param int   $product_id Product ID.
     186         * @param array $data         Cart item data.
     187         * @param int   $product_id   Product ID.
     188         * @param int   $variation_id Variation ID.
    130189         * @return array
    131190         */
    132         public static function add_item_data( $data, $product_id ) {
    133                 $result = self::posted_result( $product_id );
     191        public static function add_item_data( $data, $product_id, $variation_id = 0 ) {
     192                $result = self::posted_result( $product_id, $variation_id );
    134193                if ( null === $result || is_wp_error( $result ) ) {
    135194                        return $data;
    … …  
    154213
    155214        /**
     215         * The stored measurement is authoritative: make the line quantity match it,
     216         * so quantity and measurements can never disagree.
     217         *
     218         * @param array $item Cart item.
     219         * @return array
     220         */
     221        public static function lock_quantity( $item ) {
     222                if ( empty( $item[ self::KEY ] ) || ! isset( $item[ self::KEY ]['packs'] ) ) {
     223                        return $item;
     224                }
     225                $settings         = Cutpack_Settings::get_for( $item['product_id'], isset( $item['variation_id'] ) ? $item['variation_id'] : 0 );
     226                $item['quantity'] = Cutpack_Settings::MODE_PACKS === $settings['mode'] ? (int) $item[ self::KEY ]['packs'] : 1;
     227                return $item;
     228        }
     229
     230        /**
    156231         * Set the price of one cart unit (one pack, or one cut piece).
    157232         *
    … …  
    163238                        return $item;
    164239                }
    165                 $settings = Cutpack_Settings::get( $item['product_id'] );
    166                 $price    = Cutpack_Calculator::line_unit_price( $settings, $item[ self::KEY ], self::base_price( $item['product_id'] ) );
     240                $variation_id = isset( $item['variation_id'] ) ? (int) $item['variation_id'] : 0;
     241                $settings     = Cutpack_Settings::get_for( $item['product_id'], $variation_id );
     242                $price        = Cutpack_Calculator::line_unit_price( $settings, $item[ self::KEY ], self::base_price( $variation_id ? $variation_id : $item['product_id'] ) );
    167243
    168244                /**
    … …  
    285361                        return $item_data;
    286362                }
    287                 $settings = Cutpack_Settings::get( $item['product_id'] );
     363                $settings = Cutpack_Settings::get_for( $item['product_id'], isset( $item['variation_id'] ) ? $item['variation_id'] : 0 );
    288364                foreach ( self::describe( $item[ self::KEY ], $settings, $item['quantity'] ) as $line ) {
    289365                        $item_data[] = array(
    … …  
    307383                        return;
    308384                }
    309                 $settings = Cutpack_Settings::get( $values['product_id'] );
     385                $settings = Cutpack_Settings::get_for( $values['product_id'], isset( $values['variation_id'] ) ? $values['variation_id'] : 0 );
    310386                foreach ( self::describe( $values[ self::KEY ], $settings, $values['quantity'] ) as $line ) {
    311387                        $order_item->add_meta_data( $line[0], $line[1], true );
  • cut-and-pack-for-woocommerce/trunk/includes/class-cutpack-frontend.php

    r3800171 r3805284  
    3333                        return $html;
    3434                }
    35                 $s = Cutpack_Settings::get( $product );
     35                $s = $product->is_type( 'variation' )
     36                        ? Cutpack_Settings::get_for( $product->get_parent_id(), $product->get_id() )
     37                        : Cutpack_Settings::get( $product );
    3638                return $html . ' <span class="cutpack-per-unit">/ ' . esc_html( Cutpack_Settings::unit_label( $s['measure'] ) ) . '</span>';
    3739        }
    … …  
    9294
    9395        /**
     96         * Per-variation data for the browser preview, keyed by variation ID.
     97         *
     98         * Built on the server so the preview never depends on theme markup or on
     99         * WooCommerce's own variation scripts.
     100         *
     101         * @param WC_Product $product Variable product.
     102         * @return array
     103         */
     104        private static function variation_map( $product ) {
     105                $map = array();
     106                if ( ! $product->is_type( 'variable' ) ) {
     107                        return $map;
     108                }
     109
     110                /**
     111                 * Maximum number of variations sent to the browser preview.
     112                 *
     113                 * @param int $limit Limit.
     114                 */
     115                $limit = (int) apply_filters( 'cutpack_max_preview_variations', 500 );
     116
     117                $children = array_slice( $product->get_children(), 0, $limit );
     118                if ( $children ) {
     119                        _prime_post_caches( $children, false, true );
     120                }
     121
     122                foreach ( $children as $variation_id ) {
     123                        $variation = wc_get_product( $variation_id );
     124                        if ( ! $variation instanceof WC_Product_Variation || ! $variation->variation_is_visible() ) {
     125                                continue;
     126                        }
     127                        $vs    = Cutpack_Settings::get_for( $product->get_id(), $variation->get_id() );
     128                        $price = $variation->get_price();
     129
     130                        $map[ $variation->get_id() ] = array(
     131                                'coverage'    => (float) $vs['coverage'],
     132                                'unitPrice'   => '' === $price ? 0 : (float) wc_get_price_to_display( $variation ),
     133                                'purchasable' => '' !== $price && $variation->is_purchasable() && $variation->is_in_stock(),
     134                                'maxQty'      => (int) $variation->get_max_purchase_quantity(),
     135                        );
     136                }
     137                return $map;
     138        }
     139
     140        /**
    94141         * Load the calculator script and its settings on enabled product pages.
    95142         */
    … …  
    112159                        'cutpackData',
    113160                        array(
    114                                 'id'        => 'cutpack-' . $product->get_id(),
    115                                 'mode'      => $s['mode'],
    116                                 'measure'   => $s['measure'],
    117                                 'input'     => $s['input'],
    118                                 'coverage'  => (float) $s['coverage'],
    119                                 'min'       => '' === $s['min'] ? 0 : (float) $s['min'],
    120                                 'max'       => '' === $s['max'] ? 0 : (float) $s['max'],
    121                                 'step'      => '' === $s['step'] ? 0 : (float) $s['step'],
    122                                 'unitPrice' => (float) wc_get_price_to_display( $product ),
    123                                 'unit'      => Cutpack_Settings::unit_label( $s['measure'] ),
    124                                 'currency'  => array(
     161                                'id'         => 'cutpack-' . $product->get_id(),
     162                                'mode'       => $s['mode'],
     163                                'measure'    => $s['measure'],
     164                                'input'      => $s['input'],
     165                                'coverage'   => (float) $s['coverage'],
     166                                'min'        => '' === $s['min'] ? 0 : (float) $s['min'],
     167                                'max'        => '' === $s['max'] ? 0 : (float) $s['max'],
     168                                'step'       => '' === $s['step'] ? 0 : (float) $s['step'],
     169                                'unitPrice'  => $product->is_type( 'variable' ) ? 0 : (float) wc_get_price_to_display( $product ),
     170                                'variable'   => $product->is_type( 'variable' ),
     171                                'variations' => self::variation_map( $product ),
     172                                'unit'       => Cutpack_Settings::unit_label( $s['measure'] ),
     173                                'currency'   => array(
    125174                                        'symbol'    => html_entity_decode( get_woocommerce_currency_symbol(), ENT_QUOTES, 'UTF-8' ),
    126175                                        'format'    => html_entity_decode( get_woocommerce_price_format(), ENT_QUOTES, 'UTF-8' ),
    … …  
    129178                                        'thousands' => wc_get_price_thousand_separator(),
    130179                                ),
    131                                 'i18n'      => array(
     180                                'i18n'       => array(
    132181                                        /* translators: 1: number of boxes, 2: amount, 3: unit, 4: price. Example: You need 17 boxes (24.48 m²) – £976.75 */
    133                                         'resultPacks' => __( 'You need %1$s (%2$s %3$s) – %4$s', 'cut-and-pack-for-woocommerce' ),
     182                                        'resultPacks'  => __( 'You need %1$s (%2$s %3$s) – %4$s', 'cut-and-pack-for-woocommerce' ),
    134183                                        /* translators: 1: amount, 2: unit, 3: price. Example: 2.4 m – £43.20 */
    135                                         'resultCut'   => __( '%1$s %2$s – %3$s', 'cut-and-pack-for-woocommerce' ),
     184                                        'resultCut'    => __( '%1$s %2$s – %3$s', 'cut-and-pack-for-woocommerce' ),
    136185                                        /* translators: %s: a number */
    137                                         'oneBox'      => __( '%s box', 'cut-and-pack-for-woocommerce' ),
     186                                        'oneBox'       => __( '%s box', 'cut-and-pack-for-woocommerce' ),
    138187                                        /* translators: %s: a number */
    139                                         'manyBoxes'   => __( '%s boxes', 'cut-and-pack-for-woocommerce' ),
    140                                         'invalid'     => __( 'Please enter a number greater than zero.', 'cut-and-pack-for-woocommerce' ),
    141                                         'invalidSize' => __( 'Please enter a length and a width greater than zero.', 'cut-and-pack-for-woocommerce' ),
     188                                        'manyBoxes'    => __( '%s boxes', 'cut-and-pack-for-woocommerce' ),
     189                                        'chooseOption' => __( 'Choose an option first.', 'cut-and-pack-for-woocommerce' ),
     190                                        'unavailable'  => __( 'This option is unavailable.', 'cut-and-pack-for-woocommerce' ),
     191                                        /* translators: 1: number of boxes in stock, 2: amount covered, 3: unit such as m² */
     192                                        'stock'        => __( 'Only %1$s in stock (%2$s %3$s).', 'cut-and-pack-for-woocommerce' ),
     193                                        'invalid'      => __( 'Please enter a number greater than zero.', 'cut-and-pack-for-woocommerce' ),
     194                                        'invalidSize'  => __( 'Please enter a length and a width greater than zero.', 'cut-and-pack-for-woocommerce' ),
    142195                                        /* translators: 1: minimum amount, 2: unit such as m² */
    143                                         'min'         => __( 'The minimum is %1$s %2$s.', 'cut-and-pack-for-woocommerce' ),
     196                                        'min'          => __( 'The minimum is %1$s %2$s.', 'cut-and-pack-for-woocommerce' ),
    144197                                        /* translators: 1: maximum amount, 2: unit such as m² */
    145                                         'max'         => __( 'The maximum is %1$s %2$s.', 'cut-and-pack-for-woocommerce' ),
     198                                        'max'          => __( 'The maximum is %1$s %2$s.', 'cut-and-pack-for-woocommerce' ),
    146199                                ),
    147200                        )
  • cut-and-pack-for-woocommerce/trunk/includes/class-cutpack-settings.php

    r3800171 r3805284  
    6666         */
    6767        public static function get( $product ) {
    68                 $product  = is_numeric( $product ) ? wc_get_product( $product ) : $product;
     68                $product = is_numeric( $product ) ? wc_get_product( $product ) : $product;
     69
     70                /**
     71                 * Filter a product's Cut & Pack settings.
     72                 *
     73                 * @param array                     $settings  Settings.
     74                 * @param WC_Product|null           $product   Product (the parent for a variation).
     75                 * @param WC_Product_Variation|null $variation Variation, when settings are read for one.
     76                 */
     77                return apply_filters( 'cutpack_product_settings', self::read( $product ), $product, null );
     78        }
     79
     80        /**
     81         * Settings stored on a product, cleaned but not filtered.
     82         *
     83         * @param WC_Product|null $product Product.
     84         * @return array
     85         */
     86        private static function read( $product ) {
    6987                $settings = self::defaults();
    7088
    … …  
    7896                }
    7997
    80                 /**
    81                  * Filter a product's Cut & Pack settings.
    82                  *
    83                  * @param array           $settings Settings.
    84                  * @param WC_Product|null $product  Product.
    85                  */
    86                 return apply_filters( 'cutpack_product_settings', self::normalise( $settings ), $product );
     98                return self::normalise( $settings );
     99        }
     100
     101        /**
     102         * Settings for a product, or for one of its variations.
     103         *
     104         * A variation may override the pack coverage; everything else comes from
     105         * the parent product. A variation that does not belong to the product is ignored.
     106         *
     107         * @param int $product_id   Parent (or simple) product ID.
     108         * @param int $variation_id Variation ID, or 0.
     109         * @return array
     110         */
     111        public static function get_for( $product_id, $variation_id = 0 ) {
     112                $product = wc_get_product( absint( $product_id ) );
     113                if ( ! $product instanceof WC_Product ) {
     114                        return self::get( null );
     115                }
     116
     117                $variation = self::owned_variation( $product, $variation_id );
     118                if ( ! $variation ) {
     119                        return self::get( $product );
     120                }
     121
     122                $settings = self::read( $product );
     123                $override = self::to_positive_decimal( $variation->get_meta( self::meta_key( 'coverage' ), true ) );
     124                if ( '' !== $override && self::MODE_PACKS === $settings['mode'] && self::MEASURE_AREA === $settings['measure'] ) {
     125                        $settings['coverage'] = $override;
     126                }
     127
     128                /** This filter is documented in get(). */
     129                return apply_filters( 'cutpack_product_settings', self::normalise( $settings ), $product, $variation );
     130        }
     131
     132        /**
     133         * The variation with this ID, but only if it really belongs to the product.
     134         *
     135         * @param WC_Product $product      Parent product.
     136         * @param int        $variation_id Variation ID.
     137         * @return WC_Product_Variation|null
     138         */
     139        public static function owned_variation( $product, $variation_id ) {
     140                $variation_id = absint( $variation_id );
     141                if ( ! $variation_id || ! $product instanceof WC_Product || ! $product->is_type( 'variable' ) ) {
     142                        return null;
     143                }
     144                $variation = wc_get_product( $variation_id );
     145                if ( $variation instanceof WC_Product && $variation->is_type( 'variation' ) && (int) $variation->get_parent_id() === (int) $product->get_id() ) {
     146                        return $variation;
     147                }
     148                return null;
    87149        }
    88150
    … …  
    90152         * Whether the calculator is switched on for a product.
    91153         *
    92          * Version 1 supports simple products only.
     154         * Works for simple and variable products. A variation counts as enabled
     155         * when its parent is.
    93156         *
    94157         * @param WC_Product|int $product Product or product ID.
    … …  
    97160        public static function is_enabled( $product ) {
    98161                $product = is_numeric( $product ) ? wc_get_product( $product ) : $product;
    99                 if ( ! $product instanceof WC_Product || ! $product->is_type( 'simple' ) ) {
     162                if ( $product instanceof WC_Product && $product->is_type( 'variation' ) ) {
     163                        $product = wc_get_product( $product->get_parent_id() );
     164                }
     165                if ( ! $product instanceof WC_Product || ! ( $product->is_type( 'simple' ) || $product->is_type( 'variable' ) ) ) {
    100166                        return false;
    101167                }
  • cut-and-pack-for-woocommerce/trunk/languages/cut-and-pack-for-woocommerce.pot

    r3800171 r3805284  
    1 # Copyright (C) 2026 Cut & Pack
     1# Copyright (C) 2026 Cartwright
    22# This file is distributed under the GPL-2.0-or-later.
    33msgid ""
    44msgstr ""
    5 "Project-Id-Version: Cut & Pack for WooCommerce 0.1.0\n"
     5"Project-Id-Version: Cut & Pack for WooCommerce 1.0.0\n"
    66"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/cut-and-pack-for-woocommerce\n"
    77"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
    … …  
    1010"Content-Type: text/plain; charset=UTF-8\n"
    1111"Content-Transfer-Encoding: 8bit\n"
    12 "POT-Creation-Date: 2026-10-29T17:11:31+00:00\n"
     12"POT-Creation-Date: 2026-11-13T09:29:09+00:00\n"
    1313"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
    1414"X-Generator: WP-CLI 2.12.0\n"
    … …  
    2727#. Author of the plugin
    2828#: cut-and-pack-for-woocommerce.php
    29 #: includes/class-cutpack-admin.php:39
     29msgid "Cartwright"
     30msgstr ""
     31
     32#. Author URI of the plugin
     33#: cut-and-pack-for-woocommerce.php
     34msgid "https://cartwrightplugins.com/"
     35msgstr ""
     36
     37#: includes/class-cutpack-admin.php:41
    3038msgid "Cut & Pack"
    3139msgstr ""
    3240
    33 #: includes/class-cutpack-admin.php:41
     41#: includes/class-cutpack-admin.php:43
    3442msgid "Switch the calculator on for each product in its \"Made to measure\" tab."
    3543msgstr ""
    3644
    37 #: includes/class-cutpack-admin.php:45
     45#: includes/class-cutpack-admin.php:47
    3846msgid "Remove data on uninstall"
    3947msgstr ""
    4048
    41 #: includes/class-cutpack-admin.php:46
     49#: includes/class-cutpack-admin.php:48
    4250msgid "Delete all Cut & Pack product settings when the plugin is deleted. Measurements on past orders are always kept."
    4351msgstr ""
    4452
    45 #: includes/class-cutpack-admin.php:66
     53#: includes/class-cutpack-admin.php:68
    4654msgid "Settings"
    4755msgstr ""
    4856
    49 #: includes/class-cutpack-admin.php:78
     57#: includes/class-cutpack-admin.php:80
    5058msgid "Made to measure"
    5159msgstr ""
    5260
    53 #: includes/class-cutpack-admin.php:102
     61#: includes/class-cutpack-admin.php:104
    5462msgid "Enable calculator"
    5563msgstr ""
    5664
    57 #: includes/class-cutpack-admin.php:103
     65#: includes/class-cutpack-admin.php:105
    5866msgid "Shoppers enter a size instead of a quantity, and the price is calculated for them."
    5967msgstr ""
    6068
    61 #: includes/class-cutpack-admin.php:112
     69#: includes/class-cutpack-admin.php:114
    6270msgid "Mode"
    6371msgstr ""
    6472
    65 #: includes/class-cutpack-admin.php:114
     73#: includes/class-cutpack-admin.php:116
    6674msgid "Sold in packs (e.g. tiles, flooring)"
    6775msgstr ""
    6876
    69 #: includes/class-cutpack-admin.php:115
     77#: includes/class-cutpack-admin.php:117
    7078msgid "Cut to size (e.g. fabric, blinds)"
    7179msgstr ""
    7280
    73 #: includes/class-cutpack-admin.php:119
     81#: includes/class-cutpack-admin.php:121
    7482msgid "Packs: the order is rounded up to whole boxes. Cut to size: the shopper pays for exactly the amount entered."
    7583msgstr ""
    7684
    77 #: includes/class-cutpack-admin.php:126
     85#: includes/class-cutpack-admin.php:128
    7886msgid "Sold by"
    7987msgstr ""
    8088
    81 #: includes/class-cutpack-admin.php:128
     89#: includes/class-cutpack-admin.php:130
    8290msgid "Area (m²)"
    8391msgstr ""
    8492
    85 #: includes/class-cutpack-admin.php:129
     93#: includes/class-cutpack-admin.php:131
    8694msgid "Length (m)"
    8795msgstr ""
    8896
    89 #: includes/class-cutpack-admin.php:138
     97#: includes/class-cutpack-admin.php:140
    9098msgid "Shopper enters"
    9199msgstr ""
    92100
    93 #: includes/class-cutpack-admin.php:140
     101#: includes/class-cutpack-admin.php:142
    94102msgid "Total area"
    95103msgstr ""
    96104
    97 #: includes/class-cutpack-admin.php:141
     105#: includes/class-cutpack-admin.php:143
    98106msgid "Length × width"
    99107msgstr ""
    100108
    101 #: includes/class-cutpack-admin.php:149
     109#: includes/class-cutpack-admin.php:152
     110msgid "The price comes from each variation's own Regular price, read as a price per m² or per metre."
     111msgstr ""
     112
     113#: includes/class-cutpack-admin.php:154
    102114msgid "The price comes from the normal Regular price field on the General tab, read as a price per m² or per metre."
    103115msgstr ""
    104116
    105117#. translators: %s: unit, e.g. m²
    106 #: includes/class-cutpack-admin.php:158
     118#: includes/class-cutpack-admin.php:164
    107119#, php-format
    108120msgid "Coverage per pack (%s)"
    109121msgstr ""
    110122
    111 #: includes/class-cutpack-admin.php:163
     123#: includes/class-cutpack-admin.php:169
    112124msgid "How much one box covers. Stock is counted in boxes."
    113125msgstr ""
    114126
    115127#. translators: %s: unit, e.g. m²
    116 #: includes/class-cutpack-admin.php:172
     128#: includes/class-cutpack-admin.php:178
    117129#, php-format
    118130msgid "Minimum (%s)"
    119131msgstr ""
    120132
    121 #: includes/class-cutpack-admin.php:176
     133#: includes/class-cutpack-admin.php:182
    122134msgid "Smallest amount a shopper can order. Leave empty for no minimum."
    123135msgstr ""
    124136
    125137#. translators: %s: unit, e.g. m²
    126 #: includes/class-cutpack-admin.php:184
     138#: includes/class-cutpack-admin.php:190
    127139#, php-format
    128140msgid "Maximum (%s)"
    129141msgstr ""
    130142
    131 #: includes/class-cutpack-admin.php:188
     143#: includes/class-cutpack-admin.php:194
    132144msgid "Largest amount a shopper can order. Leave empty for no maximum."
    133145msgstr ""
    134146
    135147#. translators: %s: unit, e.g. m
    136 #: includes/class-cutpack-admin.php:196
     148#: includes/class-cutpack-admin.php:202
    137149#, php-format
    138150msgid "Step (%s)"
    139151msgstr ""
    140152
    141 #: includes/class-cutpack-admin.php:201
     153#: includes/class-cutpack-admin.php:207
    142154msgid "Amounts are rounded up to this step, e.g. 0.1 turns 2.34 into 2.4."
    143155msgstr ""
    144156
    145 #: includes/class-cutpack-admin.php:209
     157#: includes/class-cutpack-admin.php:215
    146158msgid "Label text"
    147159msgstr ""
    148160
    149 #: includes/class-cutpack-admin.php:211
    150 #: includes/class-cutpack-frontend.php:59
     161#: includes/class-cutpack-admin.php:217
     162#: includes/class-cutpack-frontend.php:61
    151163msgid "How many m² do you need?"
    152164msgstr ""
    153165
    154 #: includes/class-cutpack-admin.php:213
     166#: includes/class-cutpack-admin.php:219
    155167msgid "Optional wording shown above the calculator."
    156168msgstr ""
    157169
    158 #: includes/class-cutpack-admin.php:273
     170#. translators: %s: unit
     171#: includes/class-cutpack-admin.php:246
     172#, php-format
     173msgid "Pack size: coverage per pack (%s)"
     174msgstr ""
     175
     176#: includes/class-cutpack-admin.php:248
     177msgid "Same as the product"
     178msgstr ""
     179
     180#: includes/class-cutpack-admin.php:249
     181msgid "Only for packs. Leave empty to use the number from the \"Made to measure\" tab."
     182msgstr ""
     183
     184#: includes/class-cutpack-admin.php:319
    159185msgid "Enter how much one pack covers."
    160186msgstr ""
    161187
    162 #: includes/class-cutpack-admin.php:277
     188#: includes/class-cutpack-admin.php:323
    163189msgid "The minimum is larger than the maximum."
    164190msgstr ""
    165191
    166 #: includes/class-cutpack-admin.php:281
     192#: includes/class-cutpack-admin.php:328
    167193msgid "Enter a Regular price on the General tab (price per m² or per metre)."
    168194msgstr ""
    169195
    170 #: includes/class-cutpack-admin.php:287
     196#: includes/class-cutpack-admin.php:334
    171197msgid "Cut & Pack: the calculator was switched off for this product."
    172198msgstr ""
    173199
    174200#: includes/class-cutpack-calculator.php:45
    175 #: includes/class-cutpack-frontend.php:141
     201#: includes/class-cutpack-frontend.php:194
    176202msgid "Please enter a length and a width greater than zero."
    177203msgstr ""
    … …  
    183209#. translators: 1: minimum amount, 2: unit such as m²
    184210#: includes/class-cutpack-calculator.php:61
    185 #: includes/class-cutpack-frontend.php:143
     211#: includes/class-cutpack-frontend.php:196
    186212#, php-format
    187213msgid "The minimum is %1$s %2$s."
    … …  
    190216#. translators: 1: maximum amount, 2: unit such as m²
    191217#: includes/class-cutpack-calculator.php:69
    192 #: includes/class-cutpack-frontend.php:145
     218#: includes/class-cutpack-frontend.php:198
    193219#, php-format
    194220msgid "The maximum is %1$s %2$s."
    … …  
    199225msgstr ""
    200226
     227#: includes/class-cutpack-cart.php:135
     228#: includes/class-cutpack-frontend.php:189
     229msgid "Choose an option first."
     230msgstr ""
     231
    201232#. translators: %s: product name
    202 #: includes/class-cutpack-cart.php:206
     233#: includes/class-cutpack-cart.php:282
    203234#, php-format
    204235msgid "%s was removed from your cart. Please enter the size you need on the product page and add it again."
    205236msgstr ""
    206237
    207 #: includes/class-cutpack-cart.php:227
     238#: includes/class-cutpack-cart.php:303
    208239msgid "Coverage"
    209240msgstr ""
    210241
    211242#. translators: 1: amount, 2: unit, 3: number of boxes. Example: 24.48 m² (17 boxes)
    212 #: includes/class-cutpack-cart.php:230
     243#: includes/class-cutpack-cart.php:306
    213244#, php-format
    214245msgid "%1$s %2$s (%3$s box)"
    … …  
    217248msgstr[1] ""
    218249
    219 #: includes/class-cutpack-cart.php:238
    220 #: includes/class-cutpack-cart.php:246
     250#: includes/class-cutpack-cart.php:314
     251#: includes/class-cutpack-cart.php:322
    221252msgid "You entered"
    222253msgstr ""
    223254
    224 #: includes/class-cutpack-cart.php:242
     255#: includes/class-cutpack-cart.php:318
    225256msgid "Length"
    226257msgstr ""
    227258
    228 #: includes/class-cutpack-cart.php:242
     259#: includes/class-cutpack-cart.php:318
    229260msgid "Area"
    230261msgstr ""
    231262
    232263#. translators: abbreviation for metres
    233 #: includes/class-cutpack-cart.php:267
    234 #: includes/class-cutpack-settings.php:171
     264#: includes/class-cutpack-cart.php:343
     265#: includes/class-cutpack-settings.php:237
    235266msgid "m"
    236267msgstr ""
    237268
    238 #: includes/class-cutpack-cart.php:346
     269#: includes/class-cutpack-cart.php:422
    239270msgid "Enter size"
    240271msgstr ""
    241272
    242 #: includes/class-cutpack-frontend.php:55
     273#: includes/class-cutpack-frontend.php:57
    243274msgid "Enter the size of your area"
    244275msgstr ""
    245276
    246 #: includes/class-cutpack-frontend.php:57
     277#: includes/class-cutpack-frontend.php:59
    247278msgid "How many metres do you need?"
    248279msgstr ""
    249280
    250281#. translators: %s: unit
    251 #: includes/class-cutpack-frontend.php:71
     282#: includes/class-cutpack-frontend.php:73
    252283#, php-format
    253284msgid "Length (%s)"
    … …  
    255286
    256287#. translators: %s: unit
    257 #: includes/class-cutpack-frontend.php:76
     288#: includes/class-cutpack-frontend.php:78
    258289#, php-format
    259290msgid "Width (%s)"
    … …  
    261292
    262293#. translators: 1: number of boxes, 2: amount, 3: unit, 4: price. Example: You need 17 boxes (24.48 m²) – £976.75
    263 #: includes/class-cutpack-frontend.php:133
     294#: includes/class-cutpack-frontend.php:182
    264295#, php-format
    265296msgid "You need %1$s (%2$s %3$s) – %4$s"
    … …  
    267298
    268299#. translators: 1: amount, 2: unit, 3: price. Example: 2.4 m – £43.20
    269 #: includes/class-cutpack-frontend.php:135
     300#: includes/class-cutpack-frontend.php:184
    270301#, php-format
    271302msgid "%1$s %2$s – %3$s"
    … …  
    273304
    274305#. translators: %s: a number
    275 #: includes/class-cutpack-frontend.php:137
     306#: includes/class-cutpack-frontend.php:186
    276307#, php-format
    277308msgid "%s box"
    … …  
    279310
    280311#. translators: %s: a number
    281 #: includes/class-cutpack-frontend.php:139
     312#: includes/class-cutpack-frontend.php:188
    282313#, php-format
    283314msgid "%s boxes"
    284315msgstr ""
    285316
    286 #: includes/class-cutpack-frontend.php:140
     317#: includes/class-cutpack-frontend.php:190
     318msgid "This option is unavailable."
     319msgstr ""
     320
     321#. translators: 1: number of boxes in stock, 2: amount covered, 3: unit such as m²
     322#: includes/class-cutpack-frontend.php:192
     323#, php-format
     324msgid "Only %1$s in stock (%2$s %3$s)."
     325msgstr ""
     326
     327#: includes/class-cutpack-frontend.php:193
    287328msgid "Please enter a number greater than zero."
    288329msgstr ""
    289330
    290331#. translators: abbreviation for square metres
    291 #: includes/class-cutpack-settings.php:173
     332#: includes/class-cutpack-settings.php:239
    292333msgid "m²"
    293334msgstr ""
  • cut-and-pack-for-woocommerce/trunk/readme.txt

    r3800171 r3805284  
    55Tested up to: 7.1
    66Requires PHP: 7.4
    7 Stable tag: 1.0.0
     7Stable tag: 1.1.0
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
    … …  
    47471. Install and activate WooCommerce.
    48482. Go to Plugins → Add New, search for "Cut & Pack", then install and activate it.
    49 3. Edit a simple product. On the General tab, enter the price per m² (or per metre) as the Regular price.
     493. Edit a simple or variable product and enter the price per m² (or per metre) as the Regular price (on a variable product, on each variation).
    50504. Open the new "Made to measure" tab, tick "Enable calculator" and choose the mode.
    51515. For packs, enter how much one box covers (for example 1.44 m²).
    … …  
    5656= Which product types are supported? =
    5757
    58 Simple products. Support for variable products (for example one floor in several finishes) is planned.
     58Simple and variable products. On a variable product (for example one floor in several finishes) each variation has its own price per m², its own stock and, if you like, its own pack size. Grouped and external products are not supported.
     59
     60= Can each variation have a different pack size? =
     61
     62Yes. Switch the calculator on in the product's "Made to measure" tab and enter the usual pack size. In each variation's panel you can enter a different "Pack size"; leave it empty to use the product's. Stock is counted in boxes, so if a finish has 5 boxes left the shopper is told so instead of being able to order more.
     63
     64= What does "Order again" do with measured items? =
     65
     66Measured items are not added again automatically, because the size has to be entered again. The shopper opens the product page and enters the size.
    5967
    6068= What price do I enter? =
    … …  
    94102== Changelog ==
    95103
     104= 1.1.0 =
     105* New: variable products. Each variation can have its own price per m² (or per metre), its own stock and, optionally, its own pack size.
     106* New: a finish that has only a few boxes left tells the shopper how many boxes can be ordered.
     107* Developers: the `cutpack_product_settings` filter now receives the parent product as its second argument and, for a variation, the variation as a third argument.
     108
    96109= 1.0.0 =
    97110* First release.
    … …  
    99112== Upgrade Notice ==
    100113
     114= 1.1.0 =
     115Adds support for variable products (different price, stock and pack size per variation).
     116
    101117= 1.0.0 =
    102118First release.
Note: See TracChangeset for help on using the changeset viewer.