php woocommerce自定义添加到购物车
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php woocommerce自定义添加到购物车相关的知识,希望对你有一定的参考价值。
/**
* Hook to Single Page
*/
add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 );
function remove_add_to_cart_buttons() {
if( is_product_category() || is_shop()) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
}
}
add_action('woocommerce_before_add_to_cart_button','ld_choose_delivery_date');
function ld_choose_delivery_date(){
$value = isset( $_POST['delivery_date'] ) ? sanitize_text_field( $_POST['delivery_date'] ) : '';
?>
<input type="hidden" name="from_date" id="from_date" value="<?php echo get_post_meta( get_the_ID(), '_ld_sell_from', true ); ?>" readonly>
<input type="hidden" name="max_date" id="max_date" value="<?php echo get_post_meta( get_the_ID(), '_ld_sell_to', true ); ?>" readonly>
<?php printf('Select Pickup Date: <input class="mb-5" type="text" name="delivery_date" id="delivery_date" value="%s"><br>',esc_attr( $value )) ?>
<?php
function ld_enq_date(){
wp_enqueue_script('product-single', LD_JS_URL . '/woocommerce/product-single.js', array('jquery-ui-datepicker'), true);
}
add_action('wp_footer','ld_enq_date');
}
function ld_add_to_cart_validation($passed, $product_id, $qty){
if( isset( $_POST['delivery_date'] ) && sanitize_text_field( $_POST['delivery_date'] ) == '' ){
$product = wc_get_product( $product_id );
wc_add_notice( sprintf( 'please select your pickup time before adding to cart.', $product->get_title() ), 'error' );
return false;
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'ld_add_to_cart_validation', 10, 3 );
function ld_add_cart_item_data( $cart_item, $product_id ){
if( isset( $_POST['delivery_date'] ) ) {
$cart_item['delivery_date'] = sanitize_text_field( $_POST['delivery_date'] );
}
return $cart_item;
}
add_filter( 'woocommerce_add_cart_item_data', 'ld_add_cart_item_data', 10, 2 );
function ld_get_cart_item_from_session( $cart_item, $values ) {
if ( isset( $values['delivery_date'] ) ){
$cart_item['delivery_date'] = $values['delivery_date'];
}
return $cart_item;
}
add_filter( 'woocommerce_get_cart_item_from_session', 'ld_get_cart_item_from_session', 20, 2 );
function ld_add_order_item_meta( $item_id, $values ) {
if ( ! empty( $values['delivery_date'] ) ) {
woocommerce_add_order_item_meta( $item_id, 'delivery_date', $values['delivery_date'] );
}
}
add_action( 'woocommerce_add_order_item_meta', 'ld_add_order_item_meta', 10, 2 );
function ld_get_item_data( $other_data, $cart_item ) {
if ( isset( $cart_item['delivery_date'] ) ){
$other_data[] = array(
'name' => 'Pickup on',
'value' => sanitize_text_field( $cart_item['delivery_date'] )
);
}
return $other_data;
}
add_filter( 'woocommerce_get_item_data', 'ld_get_item_data', 10, 2 );
以上是关于php woocommerce自定义添加到购物车的主要内容,如果未能解决你的问题,请参考以下文章
php [WooCommerce Core]自定义添加到购物车URL
woocommerce自定义“添加到购物车”文本/链接
基于产品类型的 WooCommerce 的“添加到购物车”按钮旁边的自定义按钮
基于产品类型的 WooCommerce 的“添加到购物车”按钮旁边的自定义按钮
php 用于向woocommerce产品属性添加自定义复选框以隐藏购物车按钮的功能
Woocommerce 添加到自定义产品的购物车按钮