Basically I am interested in using woocommerce to sell a product . This product is a Print Order of a external printing service that I have implemented in a brand new plugin.
What I want now is after the order, is to be able to put that "order" in the buy cart, and buy it normally as just another woocommerce product.
The product has to be created on the fly, manually by a way of some function that I can use to create a product during a certain workflow point.
Can you help me to find a solution? Using woocommerce or not!
答案 0 :(得分:0)
我从您的要求/评论中理解的是,您希望能够动态创建产品,这是个坏主意。但这是我的建议。
假设您有一个名为“打印作业”的简单产品,价格为1美元,ID为10.我假设您的外部打印服务会发回订单和价格的回复印刷,让我们说64美元。
收到回复后,您只需拨打 add_product_to_cart(10),这会自动将打印作业产品添加到购物车。
/************* functions.php ***************/
function add_product_to_cart($product_id) {
if ( ! is_admin() ) {
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id );
}
}
}
您还需要在您的functions.php中添加此功能,此功能将覆盖产品的价格,即“打印作业”,实际价格为64美元。
/******************* Functions.php ****************/
add_action( 'woocommerce_before_calculate_totals', 'override_printing_price' );
function override_printing_price( $cart_object ) {
$custom_price = 64;
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->price = $custom_price;
}
}