首页 文章

Woocommerce Variable Product页面

提问于
浏览
1

我们聘请了一位开发人员来创建一个自定义代码,进入Woocommerce>单个产品>添加到购物车> Variable.php

您可以在此处查看页面的修改方式:Example of Single Variable Product Page

<?php
/**
 * Variable product add to cart
 *
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     2.1.0
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

function omniwp_output_variation_row( $ref, $dimensions, $qty, $price1, $price2, $price3, $price4, $id ) {
?>
        <tr id="ctl00_ContentPlaceHolder1_rptPrices_ctl00_tr" class="">
          <td class="colRef"><?php echo $ref ?></td>
          <td class="colDimensions"><?php echo $dimensions ?></td>
          <td class="colPackQty"><?php echo $qty ?></td>
          <td class="colPPI1"><?php echo $price1 ?></td>
          <td class="colPPI2-4"><?php echo $price2 ?></td>
          <td class="colPPI5-9"><?php echo $price3 ?></td>
          <td class="colPPI10"><?php echo $price4 ?></td>
          <td class="colOrderQty"><input name="qty_variation_id[<?php echo $id ?>]" class="textbox txtQty" type="text"></td>
        </tr>
<?php
}

global $woocommerce, $product, $post;

$discounts              = '';
$_enable_discount       = get_post_meta( $product->post->ID, '_enablediscount', true);
$dpta_overall_discounts = get_option( 'dpta_overall_discounts' );

if ( $_enable_discount == 'yes' ) { 

    $_attached_discount = get_post_meta( $product->post->ID, '_attacheddiscount', true);
    $discounts          = array_shift( dpta_get_applicable_discount( $_attached_discount ) );

} elseif ( isset( $dpta_overall_discounts['common'] ) 
    && $dpta_overall_discounts['common'] == 'yes' 
    && isset( $dpta_overall_discounts['commondiscount'] ) )  {

    $_attached_discount = $dpta_overall_discounts['commondiscount']; 
    $discounts          = array_shift( dpta_get_applicable_discount( $_attached_discount ) );

}

$currency_symbol     = get_woocommerce_currency_symbol();
$currency_symbol_len = strlen( $currency_symbol );

$dimensions         = false;
$cartons            = false;
$product_attributes = $product->get_attributes();

if ( $product_attributes ) {

$dimensions_values  = explode( '|' , $product_attributes['dimensions']['value'] );
$dimensions_keys    = array_map( 'sanitize_title', $dimensions_values );
$dimensions         = array_combine( $dimensions_keys, $dimensions_values );

$cartons_values  = explode( '|' , $product_attributes['cartons']['value'] );
$cartons_keys    = array_map( 'sanitize_title', $cartons_values );
$cartons         = array_combine( $cartons_keys, $cartons_values );

}

?>

<?php do_action( 'woocommerce_before_add_to_cart_form' ); ?>

<form class="variations_form cart" method="post" enctype="multipart/form-data" data-product_id="<?php echo $post->ID; ?>" data-product_variations="<?php echo esc_attr( json_encode( $available_variations ) ) ?>">
    <?php if ( ! empty( $available_variations ) ) : ?>
    <table>
      <thead>
        <tr>
          <th class="colRef" rowspan="2" scope="col">Ref.</th>
          <th class="colDimensions" rowspan="2" scope="col">Dimensions</th>
          <th class="colPackQty" rowspan="2" scope="col">Pack Qty.</th>
          <th class="colPPI" colspan="4" scope="colgroup">Price per Pack</th>
          <th class="colOrderQty" rowspan="2" scope="col">Order Qty.</th>
        </tr>
        <tr>
          <th class="colPPI1" scope="col">1</th>
          <th class="colPPI2-4" scope="col">2-4</th>
          <th class="colPPI5-9" scope="col">5-9</th>
          <th class="colPPI10" scope="col">10+</th>
        </tr>
      </thead>
      <tbody>
<?php 


    foreach ( $available_variations as $variation ) {

        $name = false;
        $qty  = '';

        if ( $dimensions && array_key_exists( 'attribute_dimensions', $variation['attributes'] ) )
            $name = $dimensions[ $variation['attributes']['attribute_dimensions'] ];
        elseif ( $cartons && array_key_exists( 'attribute_cartons', $variation['attributes'] ) )
            $name = $cartons[ $variation['attributes']['attribute_cartons'] ];
        elseif ( array_key_exists( 'attribute_pa_dimensions', $variation['attributes'] ) ) {
            $term = get_term_by( 'slug', $variation['attributes']['attribute_pa_dimensions'], 'pa_dimensions');
            if ( $term ) 
                $name = $term->name;
        }

        if ( ! $name ) { 
            $option = 'Not found variation attribute for  `' . print_r( $variation['attributes'], true ) . '`';
            $qty    = '-';
        } else {
            $option = substr( $name, 0, strpos( $name, '(' ) - 1  ); 
            if ( strpos( $name, '(Qty.' ) )
                $qty = substr( $name, strpos( $name, '(Qty.' ) + 5, -1 ); 
            elseif ( strpos( $name, '(Qty' ) )
                $qty = substr( $name, strpos( $name, '(Qty' ) + 4, -1 );

            $qty = str_replace( ')', '', $qty ); 

        }

        $price1 = $variation['price_html'];

        if ( empty( $price1 ) ) 
            $price1 = '<span class="price">' . wc_price( $product->get_variation_price( 'min', $display = true ) ) . '</span>';
    //  $price1 = '<span class="price">' . wc_price( $product->get_price() ) . '</span>';

        if ( empty( $discounts ) ) {
            $price2 = $price3 = $price4 = $price1 . ' no discounts';
        } else {

            $price = substr( $price1, strpos( $price1, $currency_symbol ) + $currency_symbol_len  );
            $price = substr( $price, 0, strpos( $price, '<' )  );
            $price = str_replace( array( '.', ',' ), '', $price ); // remove any number formating
            $price = $price / 100;                                 // make price a float with 2 digits 


            $price2 = '<span class="price">' . wc_price( $price - ( $price / 100 ) * $discounts['rules'][1]['amount'] ) . '</span>';
            $price3 = '<span class="price">' . wc_price( $price - ( $price / 100 ) * $discounts['rules'][2]['amount'] ) . '</span>';
            $price4 = '<span class="price">' . wc_price( $price - ( $price / 100 ) * $discounts['rules'][3]['amount'] ) . '</span>';
        }

        omniwp_output_variation_row( 
            $ref = $variation['sku'], 
            $variation_dimensions = esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ), 
            $qty, 
            $price1, 
            $price2, 
            $price3, 
            $price4, 
            $field_name = $variation['variation_id'] 
            );
    }
?>
      </tbody>
      <tfoot>
        <tr>
          <th class="colRef" rowspan="2" scope="col">Ref.</th>
          <th class="colDimensions" rowspan="2" scope="col">Dimensions</th>
          <th class="colPackQty" rowspan="2" scope="col">Pack Qty.</th>
          <th class="colPPI" colspan="4" scope="colgroup">Price per Pack</th>
          <th class="colOrderQty" rowspan="2" scope="col">Order Qty.</th>
        </tr>
        <tr>
          <th class="colPPI1" scope="col">1</th>
          <th class="colPPI2-4" scope="col">2-4</th>
          <th class="colPPI5-9" scope="col">5-9</th>
          <th class="colPPI10" scope="col">10+</th>
        </tr>
      </tfoot>
    </table>

        <?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>

        <div class="multiple_variation_wrap">
            <div class="variations_button">
                <button type="submit" class="single_add_to_cart_button button alt"><?php echo $product->single_add_to_cart_text(); ?></button>
                <input type="hidden" name="multiple-add-to-cart" value="<?php echo $product->id; ?>" />
                <input type="hidden" name="product_id" value="<?php echo esc_attr( $post->ID ); ?>" />
                <input type="hidden" name="xvariation_id" value="multiple-add-to-cart" />
            </div>
        </div>

        <?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>

    <?php else : ?>

        <p class="stock out-of-stock"><?php _e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?></p>

        <?php endif; ?>

        </form>

        <?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>

但是,当我们尝试将woocommerce更新到最新版本时,它会破坏此代码(以及单变量产品页面)并且不再像它应该的那样 . 任何人都可以发现任何明显需要改变的东西 . 我想也许有一些不再使用/需要改变的已弃用功能 .

我非常感谢知道PHP和Woocommerce的人的任何帮助:)

保罗

1 回答

  • 0

    是的,有一些变化,比如,需要更新

    $product->id to $product->get_id()
    

    $product->product_type to $product->get_type()
    

    自Woocommerce 3.0.0更新 .

相关问题