根据自定义输入更新差异产品价格

时间:2018-07-19 09:14:14

标签: woocommerce cart variation

  

variation Product

Update :我正在使用自定义plug-in来按照用户角色设置价格,并且我想从产品页面根据我的输入字段更新价格。

我想根据用户输入来更新产品价格,并选择两个预定义的变体,如图所示

bug

  

更新2:根据建议和代码链接,我能够在购物车页面上获取价格,但如果我启用了上述插件,则无法更新每种产品的总价。

 add_action( 'woocommerce_before_variations_form', 'addCustomDeimensionAction', 5 );

    function addCustomDeimensionAction() { ?>
    <input type="number" name="dimensionLength" class="dimensionLength" placeholder="L">
    <input type="number" name="dimensionWidth" class="dimensionWidth" placeholder="W">
    <input type="hidden" name="customTotalCharge" class="customTotalCharge" value="">
    <?php 
    }

    // Get custom field value, calculate new item price, save it as custom cart item data
    add_filter('woocommerce_add_cart_item_data', 'add_custom_field_data', 20, 2 );
    function add_custom_field_data( $cart_item_data, $product_id ){
        $customTotalCharge = (float) sanitize_text_field( $_POST['customTotalCharge'] );
        $product = wc_get_product($product_id); // The WC_Product Object
        if($product->product_type=='variable') {
        $new_price =  $customTotalCharge;

        // Set the custom amount in cart object
        $cart_item_data['custom_data']['extra_charge'] = (float) $customTotalCharge;
        $cart_item_data['custom_data']['new_price'] = (float) $new_price;
        $cart_item_data['custom_data']['unique_key'] = md5( microtime() . rand() ); // Make each item unique
    }
    return $cart_item_data;
    }

注意:下面的过滤器无法正常工作,因为上面的woocommerce_product_variation_get_price插件已使用

// Set the new calculated cart item price 
add_action( 'woocommerce_before_calculate_totals', 'extra_price_add_custom_price', 9999, 1 );
function extra_price_add_custom_price( $cart ) {
    if ( is_admin() && !defined('DOING_AJAX') )
        return;

    foreach ( $cart->get_cart() as $cart_item ) {
        if( isset($cart_item['custom_data']['new_price']) ){
            echo $cart_item['custom_data']['new_price'];
            $cart_item['data']->set_price( floatval($cart_item['custom_data']['new_price']) );
        }
    }
}

所以我正尝试使用相同的文件管理器(例如woocommerce_product_variation_get_price)更新价格,但无法在此过滤器中获取 custom_data

// Display cart item custom price details
add_filter('woocommerce_cart_item_price', 'display_cart_items_custom_price_details', 999, 3 );
function display_cart_items_custom_price_details( $product_price, $cart_item, $cart_item_key ){
    if( isset($cart_item['custom_data']['extra_charge']) ) {
        $product = $cart_item['data'];
        $product_price  = wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) );
        $product_price .= '<br>' . wc_price( $cart_item['custom_data']['extra_charge'] ).'&nbsp;';
        $product_price .= __("Extra Charge", "woocommerce" );
    }
    return $product_price;
}

add_action('wp_footer', 'get_variation_dump');
function get_variation_dump() {
?>
<script type="text/javascript">
    jQuery( document ).ready( function($) {
    jQuery( '.variations_form' ).each( function() {
        // when variation is found, do something
        jQuery(this).on( 'found_variation', function( event, variation ) {
            let dimensionLengthVal = jQuery('.dimensionLength').val();
            let dimensionWidthVal = jQuery('.dimensionWidth').val();
            let variationPrice = variation.display_price;
            const totalPrice = (dimensionWidthVal * dimensionLengthVal ) * variationPrice;
            jQuery('.customTotalCharge').val(totalPrice);
        });

    });

} );
</script>

0 个答案:

没有答案