问题描述
我有一个名为receive_printer的Django应用,当使用信号接收订单时,该应用应该打印订单。这里有两个应用程序:订单和 receipt_printer 。
但是,每当处理订单并充满电时,我都无法正常工作。为了简单起见,下面的示例只是向控制台输出了一些内容。如果我在“订单”应用中的models.py内部进行所有操作,则效果很好。
receipt_printer / apps.py
$ echo foo > foo # making test files
$ echo bar > bar
$ mv -i --backup foo bar # mving foo
mv: overwrite 'bar'? y # -i caused this
$ cat bar
foo # mv caused this
$ cat bar~ # --backup caused this
bar
receipt_printer / signals.py
from django.apps import AppConfig
class ReceiptPrinterConfig(AppConfig):
name = 'receipt_printer'
def ready(self):
from . import signals
receipt_printer / 初始化 .py
from django.db import models
from saleor.order.models import Order
from django.db.models.signals import post_save
def order_fully_paid_signal(sender,instance,created,**kwargs):
if instance.get_payment_status() == "fully-charged":
print("This signal is working for fully paid order")
else:
print("This signal won't working for fully paid order")
post_save.connect(order_fully_paid_signal,sender=Order)
更新,也尝试了以下方法-无效:
default_app_config = 'receipt_printer.apps.ReceiptPrinterConfig'
解决方法
不能完全确定这里是什么问题,但下面是我使用的post_save模式可以跨应用程序运行,而无需对apps.py或init.py进行任何更改:
from django.db.models.signals import post_save
from django.dispatch import receiver
@receiver(post_save,sender=<Model>,dispatch_uid='<model>_post_save')
def <model>_post_save(sender,instance,**kwargs):
...
,
我需要将以下行添加到我的订单应用程序的models.py底部:
function filter_woocommerce_add_to_cart_validation( $passed,$product_id,$quantity,$variation_id = null,$variations = null ) {
// Get product object
$product = wc_get_product( $product_id );
// Flag
$flag = false;
// Is a WC product
if ( is_a( $product,'WC_Product' ) ) {
// Get the product attribute value
$max_qty = $product->get_attribute( 'quantite_max' );
// NOT empty
if ( ! empty( $max_qty ) ) {
// If new added quantity greater than max quantity
if ( $quantity > $max_qty ) {
$flag = true;
// WC Cart NOT null & Cart is NOT empty
} elseif ( ! is_null( WC()->cart ) && ! WC()->cart->is_empty() ) {
// Get cart items quantities
$cart_item_quantities = WC()->cart->get_cart_item_quantities();
// Product quantity in cart
$product_qty_in_cart = isset( $cart_item_quantities[ $product_id ] ) ? $cart_item_quantities[ $product_id ] : null;
// New added quantity + product quantity in cart greater than max quantity
if ( ( $quantity + $product_qty_in_cart ) > $max_qty ) {
$flag = true;
}
}
}
}
// True
if ( $flag ) {
wc_add_notice( sprintf( __( 'Cant have more than %s x %s in cart','woocommerce' ),$max_qty,$product->get_name() ),'error' );
$passed = false;
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation','filter_woocommerce_add_to_cart_validation',10,5 );