php 活动门票加 - 当WooCommerce Ticket添加到购物车时,将其他WooCommerce产品添加到购物车

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 活动门票加 - 当WooCommerce Ticket添加到购物车时,将其他WooCommerce产品添加到购物车相关的知识,希望对你有一定的参考价值。

<?php
/**
 * Event Tickets Plus - Add other WooCommerce Product to Cart when WooCommerce Ticket is added to cart
 *
 * @link https://wordpress.org/plugins/woocommerce-product-dependencies/ May be a better and easier solution !!! !!! !!! (But I haven't tried it.)
 *
 * !!! Before using, edit the variables in this function according to your needs!!!
 * Could maybe also do the inverse (if ticket product is removed from cart, also remove the chained product).
 *
 * From https://gist.github.com/cliffordp/30481ca323012e298418095476d49337
 * For https://theeventscalendar.com/support/forums/topic/adding-product-to-community-tickets-through-woo-commerce/#post-1295497
 *
 * @link https://docs.woocommerce.com/document/automatically-add-product-to-cart-on-visit/
 * @link https://woocommerce.com/products/chained-products/
 */
add_action( 'woocommerce_add_to_cart', 'cliff_woo_ticket_add_chained_product_to_cart', 10, 6 );
function cliff_woo_ticket_add_chained_product_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {

	//
	// !!! CHANGE THESE THREE VARIABLES BEFORE USING!!!
	//
	$woo_ticket_product_id = 1234; // If this ticket gets added to cart...
	$woo_product_id_to_add = 2747; // ...then add this product to cart as well...
	$quantity_to_add_per_ticket = 1; // ... in this quantity PER TICKET being added to cart.
	//
	// !!! STOP EDITING HERE !!!
	//

	// bail if the product being added to cart is not the targeted ticket
	if ( $woo_ticket_product_id !== $product_id ) {
		return false;
	}

	$quantity_to_add_to_cart = $quantity * $quantity_to_add_per_ticket;

	WC()->cart->add_to_cart( $woo_product_id_to_add, $quantity_to_add_to_cart );
}

以上是关于php 活动门票加 - 当WooCommerce Ticket添加到购物车时,将其他WooCommerce产品添加到购物车的主要内容,如果未能解决你的问题,请参考以下文章

php 活动门票加:WooCommerce:强制所有门票“单独出售”。

php 活动门票加:WooCommerce:强制所有门票“单独出售”。

php 活动门票加:WooCommerce门票:将门票购买限制限制为1。

php 活动门票加:WooCommerce门票:将门票购买限制限制为1。

php 活动门票加:WooCommerce购物车:在每个门票名称前加上适用的活动的帖子标题(如果附加到

php 活动门票加:WooCommerce购物车:在每个门票名称前加上适用的活动的帖子标题(如果附加到