Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 基于自定义字段的WooCommerce产品变化价格_Php_Wordpress_Woocommerce_Custom Fields_Price - Fatal编程技术网

Php 基于自定义字段的WooCommerce产品变化价格

Php 基于自定义字段的WooCommerce产品变化价格,php,wordpress,woocommerce,custom-fields,price,Php,Wordpress,Woocommerce,Custom Fields,Price,我有一些可变产品,其中添加了如下自定义字段: function ab_preorder_variation_fields( $loop, $variation_data, $variation ) { echo '<div class="options_group form-row form-row-full">'; // Is Preordable woocommerce_wp_checkbox( array(

我有一些可变产品,其中添加了如下自定义字段:

function ab_preorder_variation_fields( $loop, $variation_data, $variation ) {

    echo '<div class="options_group form-row form-row-full">';
    // Is Preordable
    woocommerce_wp_checkbox(
        array(
            'id'            => '_ab_preorder_checkbox[' . $variation->ID . ']',
            'wrapper_class' => 'show_if_simple',
            'label'         => __(' Disponible à la précommande', 'woocommerce' ),
            'description'   => __( 'Disponible à la précommande', 'woocommerce' ),
            'desc_tip'    => true,
            'value' => get_post_meta( $variation->ID, '_ab_preorder_checkbox', true )
        )
    );
    
    // Custom Preorder Price
    woocommerce_wp_text_input(
        array(
            'id'                => '_ab_preorder_custom_price[' . $variation->ID . ']',
            'label'             => __( 'Prix à la précommande', 'woocommerce' ),
            'placeholder'       => '',
            'desc_tip'          => true,
            'description'       => __( "Prix à la précommande", 'woocommerce' ),
            'type'              => 'number',
            'custom_attributes' => array(
                    'step'  => 'any',
                    'min'   => '0'
                ),
            'value' => get_post_meta( $variation->ID, '_ab_preorder_custom_price', true )
        )
    );
    // Date de livraison estimée
    woocommerce_wp_text_input(
        array(
            'id'          => '_ab_preorder_estimated_date[' . $variation->ID . ']',
            'label'       => __( 'Date de livraison estimé', 'woocommerce' ),
            'placeholder' => '24/09/2021',
            'desc_tip'    => true,
            'description' => __( "Date de livraison estimé", "woocommerce" ),
            'value' => get_post_meta( $variation->ID, '_ab_preorder_estimated_date', true )
        )
    );


    echo '</div>';

}
add_action( 'woocommerce_product_after_variable_attributes', 'ab_preorder_variation_fields', 10, 3 ); // After all Variation fields
function ab_preorder_variation_fields_saving( $post_id ){

    // Is Preordable
    $woocommerce_text_field = $_POST['_ab_preorder_checkbox'][ $post_id ];
    update_post_meta( $post_id, '_ab_preorder_checkbox', esc_attr( $woocommerce_text_field ) );
    
    // Custom Preorder Price
    $woocommerce_text_field = $_POST['_ab_preorder_custom_price'][ $post_id ];
    update_post_meta( $post_id, '_ab_preorder_custom_price', esc_attr( $woocommerce_text_field ) );
    
    // Date de livraison estimée
    $woocommerce_text_field = $_POST['_ab_preorder_estimated_date'][ $post_id ];
    update_post_meta( $post_id, '_ab_preorder_estimated_date', esc_attr( $woocommerce_text_field ) );

}
谁能帮我一下吗


关于,

由于woocommerce不允许购买零库存的产品,您需要为相关产品设置“允许缺货”选项

我重新访问了您的代码(特别是您的第二个函数)

在您的情况下,计算总计之前的钩子
woocommerce\u
不方便。最好在产品上设置缺货定制价格

启用该复选框后,以下内容将在变体上设置延期订单价格,并将在相关变体上显示该价格以及预计交货日期:

// Admin Variation custom fields
add_action( 'woocommerce_product_after_variable_attributes', 'ab_preorder_variation_fields', 10, 3 );
function ab_preorder_variation_fields( $loop, $variation_data, $variation ) {

    echo '<div class="options_group form-row form-row-full">';
    
    // Is Preordable
    woocommerce_wp_checkbox(
        array(
            'id'            => '_ab_preorder_checkbox['.$loop.']',
            'wrapper_class' => 'show_if_simple',
            'label'         => __(' Disponible à la précommande', 'woocommerce' ),
            'description'   => __( 'Disponible à la précommande', 'woocommerce' ),
            'desc_tip'    => true,
            'value' => get_post_meta( $variation->ID, '_ab_preorder_checkbox', true )
        )
    );

    // Custom Preorder Price
    woocommerce_wp_text_input(
        array(
            'id'                => '_ab_preorder_custom_price['.$loop.']',
            'label'             => __( 'Prix à la précommande', 'woocommerce' ),
            'placeholder'       => '',
            'desc_tip'          => true,
            'description'       => __( "Prix à la précommande", 'woocommerce' ),
            'type'              => 'number',
            'custom_attributes' => array(
                    'step'  => 'any',
                    'min'   => '0'
                ),
            'value' => get_post_meta( $variation->ID, '_ab_preorder_custom_price', true )
        )
    );

    // Date de livraison estimée
    woocommerce_wp_text_input(
        array(
            'id'          => '_ab_preorder_estimated_date['.$loop.']',
            'label'       => __( 'Date de livraison estimé', 'woocommerce' ),
            'desc_tip'    => true,
            'description' => __( "Date de livraison estimé", "woocommerce" ),
            'type'        => 'date',
            'value' => get_post_meta( $variation->ID, '_ab_preorder_estimated_date', true )
        )
    );

    echo '</div>';
}

// Save admin Variations custom fields values
add_action( 'woocommerce_admin_process_variation_object', 'ab_preorder_variation_fields_saving', 10, 2 );
function ab_preorder_variation_fields_saving( $variation, $loop ) {
    if( isset($_POST['_ab_preorder_checkbox'][$loop]) ) {
        $variation->update_meta_data( '_ab_preorder_checkbox', esc_attr($_POST['_ab_preorder_checkbox'][$loop]) );
    }

    if( isset($_POST['_ab_preorder_custom_price'][$loop]) ) {
        $variation->update_meta_data( '_ab_preorder_custom_price', esc_attr($_POST['_ab_preorder_custom_price'][$loop]) );
    }

    if( isset($_POST['_ab_preorder_estimated_date'][$loop]) ) {
        $variation->update_meta_data( '_ab_preorder_estimated_date', esc_attr($_POST['_ab_preorder_estimated_date'][$loop]) );
    }
}

// Set the variation backorder price
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2 );
function custom_price( $price, $product ) {
    if ( $product->get_stock_quantity() == 0 && $product->get_meta('_ab_preorder_checkbox') === 'yes' ) {
        $backorder_price = $product->get_meta('_ab_preorder_custom_price');
        
        if( $backorder_price > 0 ) {
            $price = (float) $backorder_price;
        }
    }
    return $price;
}

// Display prefixed backorder price and estimated delivery on single product pages
add_filter( 'woocommerce_available_variation', 'ab_available_variation_custom_field', 10, 3 );
function ab_available_variation_custom_field( $variation_data, $product, $variation ) {
    if ( $variation->get_stock_quantity() == 0 && $variation->get_meta('_ab_preorder_checkbox') === 'yes' ) {
        if ( $estimated_delivery_date = $variation->get_meta('_ab_preorder_estimated_date') ) {
            // Display estimated delivery date
            $variation_data['availability_html'] .= sprintf( '<p class="stock date-precommande">%s : %s</p>',
            __("Date de livraison estimée (précommande)", "woocommerce"), $estimated_delivery_date );
        }
        
        // Displayed prefixed formatted price
        $variation_data['price_html'] = '<span class="price-prefix">'.__("Prix à la précommande", "") .'<span> : ' . wc_price( $variation_data['display_price'] );
    }
    return $variation_data;
}

// Display on estimated delivery date on cart and checkout
add_filter( 'woocommerce_get_item_data', 'display_acf_on_cart_and_checkout', 10, 2 );
function display_acf_on_cart_and_checkout( $cart_data, $cart_item ) {
    if ( $cart_item['variation_id'] > 0 && $cart_item['data']->get_stock_quantity() == 0
    && $cart_item['data']->get_meta('_ab_preorder_checkbox') === 'yes' ) {
        if ( $estimated_delivery_date = $cart_item['data']->get_meta('_ab_preorder_estimated_date') ) {
            $custom_items[] = array( "name" => __("Date de livraison estimée", "woocommerce"),  "value" => $estimated_delivery_date );
        }
    }
    return $custom_items;
}
//管理变量自定义字段
添加操作('WOOMerce\u product\u在变量属性之后,'ab\u预订单\u变体字段',10,3);
函数ab_预排序_变量_字段($loop、$VARIANCE_data、$VARIANCE){
回声';
//这是预定的
woocommerce\u wp\u复选框(
排列(
“id'=>”\u ab\u preorder\u复选框['.$loop.]',
'wrapper\u class'=>'show\u if\u simple',
“label”=>(“disposibleála précommande”,“woocommerce”),
“description”=>(“可争议的”命令)、“woocommerce”),
“描述提示”=>正确,
'value'=>get_post_meta($variation->ID,'ab_preorder_checkbox',true)
)
);
//定购价格
woocommerce_wp_text_输入(
排列(
“id'=>”\u ab\u预订单\u自定义\u价格['.$loop.]',
“label”=>(Prixála précommande)、“woocommerce”),
'占位符'=>'',
“描述提示”=>正确,
“description”=>(Prixála précommande),“woocommerce”),
'类型'=>'编号',
“自定义_属性”=>数组(
'步骤'=>'任何',
“min”=>“0”
),
'value'=>get_post_meta($variation->ID,'u ab_preorder_custom_price',true)
)
);
//里夫莱森酒店
woocommerce_wp_text_输入(
排列(
“id'=>”\u ab\u预订单\u估计的\u日期['.$loop.]',
“label”=>(日期“Date de livraison estimé”,“woocommerce”),
“描述提示”=>正确,
“说明”=>““生活日”、“商业日”),
'类型'=>'日期',
'value'=>get_post_meta($variation->ID,'u ab_preorder_estimated_date',true)
)
);
回声';
}
//保存管理员变量自定义字段值
添加操作('WOOMerce\u admin\u process\u variation\u object'、'ab\u preorder\u variation\u fields\u saving',10,2);
函数ab\预订单\变量\字段\保存($variation,$loop){
如果(isset($\u POST[''u ab\u preorder\u checkbox'][$loop])){
$variation->update_meta_数据('u ab_preorder_checkbox',esc_attr($_POST['u ab_preorder_checkbox'][$loop]);
}
如果(isset($邮政[''''u ab'.'预订单].[自定义价格][$循环]){
$variation->update_meta_data('u ab_preorder_custom_price',esc_attr($_POST['u ab_preorder_custom_price'][$loop]);
}
如果(isset($后[''前订单['估计日期][$循环]){
$variation->update_meta_data('u ab_preorder_estimated_date',esc_attr($_POST['u ab_preorder_estimated_date'][$loop]);
}
}
//设置变更延期交货价格
添加过滤器('woocommerce\u product\u variation\u get\u regular\u price'、'custom\u price',99,2);
添加过滤器('woocommerce\u product\u variation\u get\u price','custom\u price',99,2);
功能自定义价格($price,$product){
如果($product->get_stock\u quantity()==0&&$product->get_meta(“'ab\u preorder\u复选框”)====yes'){
$backorder\u price=$product->get\u meta(“'u ab\u preorder\u custom\u price”);
如果($backorder_price>0){
$price=(浮动)$backorder\u价格;
}
}
返回$price;
}
//在单个产品页面上显示带前缀的缺货价格和预计交货期
添加过滤器('woocommerce\u available\u variation'、'ab\u available\u variation\u custom\u field',10,3);
函数ab_可用_变量_自定义_字段($variation_data、$product、$variation){
如果($variation->get\u stock\u quantity()==0&&&$variation->get\u meta(“'ab\u preorder\u checkbox')===='yes'){
如果($estimated\u delivery\u date=$variation->get\u meta(''ab\u preorder\u estimated\u date')){
//显示预计交货日期
$variation\u data['availability\u html'].=sprintf('

%s:%s

', __(“预计交货日期”(précommande)”,“woocommerce”),$预计交货日期); } //显示带前缀的格式化价格 $variation_data['price_html']='.''.:'.wc_price($variation_data['display_price']); } 返回$U数据; } //在购物车上显示预计交货日期并结帐 添加过滤器('woocommerce\u get\u item\u data','display\u acf\u on\u cart\u and \u checkout',10,2); 功能显示购物车和结帐上的acf($cart\u数据,$cart\u项目){ 如果($cart\u item['variation\u id']>0&&$cart\u item['data']->get\u stock\u quantity()==0 &&$cart\u item['data']->get\u meta(“'ab\u preorder\u checkbox')='yes'){ 如果($estimated_delivery_date=$cart_item['data']->get_meta(“'ab_preorder_estimated_date')){ $custom_items[]=array(“name”=>“Date de livraison estimée”,“woocommerce”),“value”=>$estimated_delivery_Date); } } 返回$custom_项目; }
代码进入活动子主题(或活动主题)的functions.php文件。测试和工作

// Admin Variation custom fields
add_action( 'woocommerce_product_after_variable_attributes', 'ab_preorder_variation_fields', 10, 3 );
function ab_preorder_variation_fields( $loop, $variation_data, $variation ) {

    echo '<div class="options_group form-row form-row-full">';
    
    // Is Preordable
    woocommerce_wp_checkbox(
        array(
            'id'            => '_ab_preorder_checkbox['.$loop.']',
            'wrapper_class' => 'show_if_simple',
            'label'         => __(' Disponible à la précommande', 'woocommerce' ),
            'description'   => __( 'Disponible à la précommande', 'woocommerce' ),
            'desc_tip'    => true,
            'value' => get_post_meta( $variation->ID, '_ab_preorder_checkbox', true )
        )
    );

    // Custom Preorder Price
    woocommerce_wp_text_input(
        array(
            'id'                => '_ab_preorder_custom_price['.$loop.']',
            'label'             => __( 'Prix à la précommande', 'woocommerce' ),
            'placeholder'       => '',
            'desc_tip'          => true,
            'description'       => __( "Prix à la précommande", 'woocommerce' ),
            'type'              => 'number',
            'custom_attributes' => array(
                    'step'  => 'any',
                    'min'   => '0'
                ),
            'value' => get_post_meta( $variation->ID, '_ab_preorder_custom_price', true )
        )
    );

    // Date de livraison estimée
    woocommerce_wp_text_input(
        array(
            'id'          => '_ab_preorder_estimated_date['.$loop.']',
            'label'       => __( 'Date de livraison estimé', 'woocommerce' ),
            'desc_tip'    => true,
            'description' => __( "Date de livraison estimé", "woocommerce" ),
            'type'        => 'date',
            'value' => get_post_meta( $variation->ID, '_ab_preorder_estimated_date', true )
        )
    );

    echo '</div>';
}

// Save admin Variations custom fields values
add_action( 'woocommerce_admin_process_variation_object', 'ab_preorder_variation_fields_saving', 10, 2 );
function ab_preorder_variation_fields_saving( $variation, $loop ) {
    if( isset($_POST['_ab_preorder_checkbox'][$loop]) ) {
        $variation->update_meta_data( '_ab_preorder_checkbox', esc_attr($_POST['_ab_preorder_checkbox'][$loop]) );
    }

    if( isset($_POST['_ab_preorder_custom_price'][$loop]) ) {
        $variation->update_meta_data( '_ab_preorder_custom_price', esc_attr($_POST['_ab_preorder_custom_price'][$loop]) );
    }

    if( isset($_POST['_ab_preorder_estimated_date'][$loop]) ) {
        $variation->update_meta_data( '_ab_preorder_estimated_date', esc_attr($_POST['_ab_preorder_estimated_date'][$loop]) );
    }
}

// Set the variation backorder price
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2 );
function custom_price( $price, $product ) {
    if ( $product->get_stock_quantity() == 0 && $product->get_meta('_ab_preorder_checkbox') === 'yes' ) {
        $backorder_price = $product->get_meta('_ab_preorder_custom_price');
        
        if( $backorder_price > 0 ) {
            $price = (float) $backorder_price;
        }
    }
    return $price;
}

// Display prefixed backorder price and estimated delivery on single product pages
add_filter( 'woocommerce_available_variation', 'ab_available_variation_custom_field', 10, 3 );
function ab_available_variation_custom_field( $variation_data, $product, $variation ) {
    if ( $variation->get_stock_quantity() == 0 && $variation->get_meta('_ab_preorder_checkbox') === 'yes' ) {
        if ( $estimated_delivery_date = $variation->get_meta('_ab_preorder_estimated_date') ) {
            // Display estimated delivery date
            $variation_data['availability_html'] .= sprintf( '<p class="stock date-precommande">%s : %s</p>',
            __("Date de livraison estimée (précommande)", "woocommerce"), $estimated_delivery_date );
        }
        
        // Displayed prefixed formatted price
        $variation_data['price_html'] = '<span class="price-prefix">'.__("Prix à la précommande", "") .'<span> : ' . wc_price( $variation_data['display_price'] );
    }
    return $variation_data;
}

// Display on estimated delivery date on cart and checkout
add_filter( 'woocommerce_get_item_data', 'display_acf_on_cart_and_checkout', 10, 2 );
function display_acf_on_cart_and_checkout( $cart_data, $cart_item ) {
    if ( $cart_item['variation_id'] > 0 && $cart_item['data']->get_stock_quantity() == 0
    && $cart_item['data']->get_meta('_ab_preorder_checkbox') === 'yes' ) {
        if ( $estimated_delivery_date = $cart_item['data']->get_meta('_ab_preorder_estimated_date') ) {
            $custom_items[] = array( "name" => __("Date de livraison estimée", "woocommerce"),  "value" => $estimated_delivery_date );
        }
    }
    return $custom_items;
}