php 产品附加组件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 产品附加组件相关的知识,希望对你有一定的参考价值。

// 1. Show custom input field above Add to Cart
 
add_action( 'woocommerce_before_add_to_cart_button', 'bbloomer_product_add_on', 9 );
 
function bbloomer_product_add_on() {
	if ( is_product() && has_term( array (
		'cordless-drills',
		'domino-joiners',
		'dust-extractors',
		'edge-banders',
		'jigsaws',
		'kapex-miter-saws',
		'mft-tables',
		'planers',
		'routers',
		'sanders',
		'track-saws'),
								  	'product_cat' ) )
		
	 {
    $value = isset( $_POST['_tshirt_add_on'] ) ? sanitize_text_field( $_POST['_tshirt_add_on'] ) : '';
    echo '<shirt><label>FREE T-Shirt with Power Tool<Br /> (please specify size M, L, XL, XXL) </label><p><input name="_tshirt_add_on" value="' . $value . '"></p></shirt>';
	}
}
 
// -----------------------------------------
// 2. Throw error if custom input field empty
 
add_filter( 'woocommerce_add_to_cart_validation', 'bbloomer_product_add_on_validation', 10, 3 );
 
function bbloomer_product_add_on_validation( $passed, $product_id, $qty ){
   if( isset( $_POST['_tshirt_add_on'] ) && sanitize_text_field( $_POST['_tshirt_add_on'] ) == '' ) {
      wc_add_notice( 'T-shirt is a required field', 'error' );
      $passed = false;
   }
    return $passed;
}
 
// -----------------------------------------
// 3. Save custom input field value into cart item data
 
add_filter( 'woocommerce_add_cart_item_data', 'bbloomer_product_add_on_cart_item_data', 10, 2 );
 
function bbloomer_product_add_on_cart_item_data( $cart_item, $product_id ){
    if( isset( $_POST['_tshirt_add_on'] ) ) {
        $cart_item['tshirt_add_on'] = sanitize_text_field( $_POST['_tshirt_add_on'] );
    }
    return $cart_item;
}
 
// -----------------------------------------
// 4. Display custom input field value @ Cart
 
add_filter( 'woocommerce_get_item_data', 'bbloomer_product_add_on_display_cart', 10, 2 );
 
function bbloomer_product_add_on_display_cart( $_data, $cart_item ) {
    if ( isset( $cart_item['tshirt_add_on'] ) ){
        $data[] = array(
            'name' => 'ShirtSize',
            'value' => 'T-Shirt:' . sanitize_text_field( $cart_item['tshirt_add_on'] )
        );
    }
    return $data;
}
 
// -----------------------------------------
// 5. Save custom input field value into order item meta
 
add_action( 'woocommerce_add_order_item_meta', 'bbloomer_product_add_on_order_item_meta', 10, 2 );
 
function bbloomer_product_add_on_order_item_meta( $item_id, $values ) {
    if ( ! empty( $values['tshirt_add_on'] ) ) {
        wc_add_order_item_meta( $item_id, 'T-Shirt Size', $values['tshirt_add_on'], true );
    }
}
 
// -----------------------------------------
// 6. Display custom input field value into order table
 
add_filter( 'woocommerce_order_item_product', 'bbloomer_product_add_on_display_order', 10, 2 );
 
function bbloomer_product_add_on_display_order( $cart_item, $order_item ){
    if( isset( $order_item['tshirt_add_on'] ) ){
        $cart_item_meta['tshirt_add_on'] = $order_item['tshirt_add_on'];
    }
    return $cart_item;
}
 
// -----------------------------------------
// 7. Display custom input field value into order emails
 
add_filter( 'woocommerce_email_order_meta_fields', 'bbloomer_product_add_on_display_emails' );
 
function bbloomer_product_add_on_display_emails( $fields ) { 
    $fields['tshirt_add_on'] = 'T-Shirt Size';
    return $fields; 
}

以上是关于php 产品附加组件的主要内容,如果未能解决你的问题,请参考以下文章

php [WooCommerce Ninja Forms产品附加组件]隐藏所有子价格

php 重力特权//条件逻辑日期//修复冲突与WC GF产品附加组件

php 产品属性属性获取产品属性加载产品属性附加产品属性\

从站点策略产品附加组件的设置配置文件安装克隆首页内容

php base functions.php附加组件

php 推荐短信和vc附加组件